import javax.swing.*; //This is the final package name. //import com.sun.java.swing.*; //Used by JDK 1.2 Beta 4 and all //Swing releases before Swing 1.1 Beta 3. import java.awt.*; import java.awt.event.*; public class HelloSwing extends JFrame implements ActionListener { private JLabel label; private static String labelPrefix = "Number of button clicks: "; private int numClicks = 0; public HelloSwing() { super("HelloSwing"); //XXX: In 1.0.2 only, must use FixedJButton instead of JButton //XXX: if you want mnemonics to work. //XXX: See ui/swing/workaround.html for details. JButton button = new JButton("I'm a Swing button!"); button.setMnemonic('i'); button.addActionListener(this); button.getAccessibleContext().setAccessibleDescription( "When you click this button, the label is updated " + "to display the total number of button clicks."); label = new JLabel(labelPrefix + "0 "); JPanel pane = new JPanel(); pane.setBorder(BorderFactory.createEmptyBorder(30, 30, 10, 30)); pane.setLayout(new GridLayout(0, 1)); pane.add(button); pane.add(label); setContentPane(pane); } public void actionPerformed(ActionEvent e) { numClicks++; label.setText(labelPrefix + numClicks); } public static void main(String[] args) { try { UIManager.setLookAndFeel( UIManager.getCrossPlatformLookAndFeelClassName()); } catch (Exception e) { System.err.println("Couldn't use the cross-platform " + "look and feel: " + e); } JFrame frame = new HelloSwing(); WindowListener l = new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }; frame.addWindowListener(l); frame.pack(); frame.setVisible(true); } }