import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class Main {
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
JFrame frame = new JFrame();
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.setLocation (100, 100);
frame.setSize (300, 300);
frame.setLayout (new FlowLayout());
// Make various widgets and add them
Button b1 = new Button ("Push me"); //*1 Widgets are local variables, no need to save
frame.add (b1); //*1
// Some are local variables, we don't save
Button b2 = new Button ("Push me too"); //*1
frame.add (b2); //*1
// Some we save in ivars for showData()
c1 = new ComboBox (); //*2
frame.add (c1);
l1 = new List (); //*2
frame.add (l1);
Radio r1 = new Radio (); //*1
frame.add (r1); //*1
s1 = new ScrollBar (); //*2
frame.add (s1);
TextField t1 = new TextField (); //*1
frame.add (t1); //*1
// This button reports the data from several other widgets
GetButton b = new GetButton (this); //*3 From callback from GetButton
frame.add (b);
// A label widget, no interaction
JLabel label = new JLabel ("This is a label"); //*1
frame.add (label); //*1
// Finally, put our window on the screen
frame.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
}
}