// 1.1 version of HelloSwing program. import java.awt.GridLayout; import java.awt.event.*; import com.sun.java.swing.*; 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"); 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); } }