import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class Main extends JFrame { private ComboBox c1; //*2 But save some widgets cause showData needs them private List l1; //*2 private ScrollBar s1; //*2 public static void main (String [] args) { java.awt.EventQueue.invokeLater (new Runnable() { public void run() { new Main (); } }); } public Main () { // Window setup setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); setLocation (100, 100); setSize (300, 300); setLayout (new FlowLayout()); // Make various widgets and add them Button b1 = new Button ("Push me"); //*1 Widgets are local variables, no need to save add (b1); //*1 // Some are local variables, we don't save Button b2 = new Button ("Push me too"); //*1 add (b2); //*1 // Some we save in ivars for showData() c1 = new ComboBox (); //*2 add (c1); l1 = new List (); //*2 add (l1); Radio r1 = new Radio (); //*1 add (r1); //*1 s1 = new ScrollBar (); //*2 add (s1); TextField t1 = new TextField (); //*1 add (t1); //*1 // This button reports the data from several other widgets GetButton b = new GetButton (this); //*3 From callback from GetButton add (b); // A label widget, no interaction JLabel label = new JLabel ("This is a label"); //*1 add (label); //*1 // Finally, put our window on the screen setVisible (true); } // GetButton calls back to us cause we have the objects with the data public void showData() { //*3 System.out.println ("Values: " + c1.getSelectedItem() + //*3 ", " + l1.getSelectedValue() + //*3 ", " + s1.getValue()); //*3 } }