//SwingDemo //Demonstrates basic elements of the JFC library //Andy Harris, 06/00 import java.awt.*; import javax.swing.*; import java.awt.event.*; public class SwingDemo extends JFrame { JButton btnColor = new JButton("Choose a color"); Icon theIcon = new ImageIcon("thing.gif"); JLabel lblThing = new JLabel(); EventHandler evt = new EventHandler(); public static void main(String args[]){ SwingDemo sd = new SwingDemo(); } // end main public SwingDemo(){ super("Whee, I'm in Swing!!"); setSize(200,200); show(); Container pane = getContentPane(); pane.add(lblThing, BorderLayout.CENTER); pane.add(btnColor, BorderLayout.SOUTH); lblThing.setText("Whoo Hoo!"); lblThing.setIcon(theIcon); lblThing.setToolTipText("This is a tool tip!!"); btnColor.setToolTipText("Choose a color for the text"); btnColor.addActionListener(evt); addWindowListener(evt); } // end constructor private class EventHandler extends WindowAdapter implements ActionListener { public void actionPerformed(ActionEvent e){ JOptionPane.showMessageDialog(null, "This is a modal dialog"); Color myColor = JColorChooser.showDialog(lblThing, "Choose a color", Color.red); lblThing.setForeground(myColor); btnColor.setForeground(myColor); repaint(); } // end actionPerformed public void windowClosing(WindowEvent e){ //The JFrame closes itself //So just exit the system System.exit(0); } // end windowClosing } // end EventHandler class def } // end SwingPractice class def