/* This applet demonstrates several GUI components that are available in Swing (java 1.2 and later). It is is direct modification of GUIDemo.java to use Swing instead of the AWT. Written May 14, 2002. David Eck */ import javax.swing.*; // make the Swing GUI classes available import java.awt.*; // used for Color and GridLayout classes import java.awt.event.*; // used for ActionEvent and ActionListener classes public class GUIDemo2 extends JApplet implements ActionListener{ private JTextArea transcript; // a message will be posted to this text area // each time an event is generated by some // some user action public void init() { // set up the applet setBackground(new Color(180,180,255)); getContentPane().setLayout(new GridLayout(1,2,5,5)); // I will put the transcript area in the right half of the // applet. The left half will be occupied by a grid of // four lines. Each line contains a component and // a label for that component. Note that for a JApplet, // any components added to the applet should be put into // its "contentPane" rather than directly into the applet. transcript = new JTextArea(); transcript.setEditable(false); transcript.setBackground(Color.white); JPanel left = new JPanel(); left.setLayout(new GridLayout(4,2,3,3)); getContentPane().add(left); getContentPane().add(new JScrollPane(transcript)); JLabel lab = new JLabel("Push Button: ", SwingConstants.RIGHT); lab.setBackground(Color.white); left.add(lab); JButton b = new JButton("Click Me!"); b.setBackground(Color.white); b.addActionListener(this); left.add(b); lab = new JLabel("Checkbox: ", SwingConstants.RIGHT); lab.setBackground(Color.white); left.add(lab); JCheckBox c = new JCheckBox("Click me!"); c.setBackground(Color.white); c.addActionListener(this); left.add(c); lab = new JLabel("Text Field: ", SwingConstants.RIGHT); lab.setBackground(Color.white); left.add(lab); JTextField t = new JTextField("Type here!"); t.setBackground(Color.white); t.addActionListener(this); left.add(t); lab = new JLabel("Pop-up Menu: ", SwingConstants.RIGHT); lab.setBackground(Color.white); left.add(lab); JComboBox m = new JComboBox(); m.setBackground(Color.white); m.addItem("First Option"); m.addItem("Second Option"); m.addItem("Third Option"); m.addItem("Fourth Option"); m.addActionListener(this); left.add(m); } // end init() private void post(String message) { // add a line to the transcript transcript.append(message + "\n"); } public Insets getInsets() { // Allow 3 pixels space around edges of applet return new Insets(3,3,3,3); } public void actionPerformed(ActionEvent evt) { // respond to an action event; this method is part of the // ActionListener interface. Object target = evt.getSource(); // which component produced this event? if (target instanceof JButton) { post("Button was clicked."); } else if (target instanceof JTextField) { post("Pressed return in TextField\n with contents:\n " + evt.getActionCommand()); } else if (target instanceof JCheckBox) { if ( ((JCheckBox)target).isSelected() ) post("Checkbox was turned on."); else post("Checkbox was turned off."); } else if (target instanceof JComboBox) { Object item = ((JComboBox)target).getSelectedItem(); post("Item \"" + item + "\" selected\n from pop-up menu."); } } } // end class GUIDemo