JButton – Container Layout & Icons Jack Tompkins Department of Computer Science tompkinsj@uncw.edu
JButton Constructor new ImageIcon(imagePath) JButton(String text, Icon icon) Creates a button with initial text and an icon. But the Icon is an interface (not instantiable) Icon - All Known Implementing Classes: IconUIResource, ImageIcon,… new ImageIcon(imagePath) Say the component is using a 3x2 GridLayout
JButton Container Instantiate the Container (say a JPanel) Set the Layout Manager Say the component is using a 3x2 GridLayout setLayout(new GridLayout(3,2)); add(new Button("1")); add(new Button("2")); add(new Button("3")); add(new Button("4")); add(new Button("5")); add(new Button("6")); Add the Container to a JFrame
addButton(String text, Container container, String imagePath) { JButton button = null; if (imagePath != null) button = new JButton(text, new ImageIcon(imagePath)); else button = new JButton(text); button.addActionListener(new MyActionListener()); container.add(button); }