package HandGUI; import java.awt.*; // older GUI library, has layout stuff for components import java.awt.event.*; // events import javax.swing.*; // swing is a library of GUI components // example of buttons with actionlisteners // class extends JFrame, so it is its own window public class Outer extends JFrame { // keep count of how many times each button was clicked private int times1; private int times2; private int times1or3; private JButton button1; private JButton button2; private JButton button3; // constructor does all setup of window and buttons public Outer() { // call the JFrame constructor super("I have a button"); times1 = 0; times2 = 0; times1or3 = 0; // using methods inherited from JFrame // close the program if we close the window setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // width, height in pixels // usually better to use pack() // but if want to keep window at same size // make it big enough! //setSize(500, 100); // put new added widgets in from left to right setLayout(new FlowLayout()); // button using an inner class as actionlistener button1 = new JButton("Button 1 - " + times1); button1.addActionListener( // the anonymous inner class to respond to clicks from button 1 // since it it inner class, it can access method of outer class new ActionListener() { // implementing this interface public void actionPerformed(ActionEvent e) { update1(); } } ); // button using an anonymous inner class as actionlistener button2 = new JButton("Button 2 - " + times2); button2.addActionListener( // the anonymous inner class to respond to clicks from button 2 // since it it inner class, it can access times2 and button2 new ActionListener() { public void actionPerformed(ActionEvent e) { times2++; button2.setText("Button 2 - " + times2); // might have made text on button longer // then button won't fit anymore // need to repack -- changes size of window pack(); } } ); // button using inner class as actionlistener button3 = new JButton("Button 1 or 3 - " + times1or3); button3.addActionListener(new InnerActionListener1and3()); // also use this actionlistener if button 1 is pushed // so button 1 has two actionlisteners triggered with every click button1.addActionListener(new InnerActionListener1and3()); // put the buttons in the window add(button1); add(button2); add(button3); // size based on widgets // if flaky, use setsize instead // have to pack again if sizes change (e.g. 1->2 digit number) pack(); // make the window visible setVisible(true); } // class to respond to clicks from button 1 or 3 // Inner class has access to instance vars of outer class private class InnerActionListener1and3 implements ActionListener { public void actionPerformed(ActionEvent e) { times1or3++; button3.setText("Button 1 or 3 - " + times1or3); pack(); } } // what we do to respond to clicks from button 1 or 3 // method called in anonymous inner class ActionListener public void update1() { times1++; button1.setText("Button 1 - " + times1); pack(); } // main to launch the GUI public static void main(String[] args) { // two separate windows, with their own variables // and own inner classes separate from each other Outer outerEx1 = new Outer(); Outer outerEx2 = new Outer(); } }