//package ex1; import javax.swing.*; import java.awt.*; import java.awt.event.*; // this program has a listener public class CanYouHearMe implements ActionListener { // step 1 of three private static String labelPrefix = "Number of button clicks: "; private int numClicks = 0; final JLabel label = new JLabel(labelPrefix + "0 "); private JButton button, knob; final static String LOOKANDFEEL = null; // ... or: "Metal", "System", "Motif", "GTK+" public Component createComponents() { button = new JButton("How fast can you push me?"); knob = new JButton("This will sap my value :("); button.setMnemonic(KeyEvent.VK_I); // ... so "alt-i" is a keystroke shorthand button.addActionListener(this); // step 2 of three knob.addActionListener(this); label.setLabelFor(button); JPanel pane = new JPanel(new GridLayout(1, 0)); // ie 0 #rows and 1 #cols pane.setBorder( BorderFactory.createEmptyBorder( 30, 30, 10, 30 ) ); pane.add(button); // T L B R pane.add(knob); pane.add(label); return pane; } public void actionPerformed(ActionEvent e) { if (e.getSource()==button) { numClicks++; label.setText(labelPrefix + numClicks); } if (e.getSource()==knob) { numClicks--; label.setText(labelPrefix + numClicks); } } // step 3 of three private static void initLookAndFeel() { String lookAndFeel = null; if (LOOKANDFEEL != null) { if (LOOKANDFEEL.equals("Metal")) lookAndFeel = UIManager.getCrossPlatformLookAndFeelClassName(); else if (LOOKANDFEEL.equals("System")) lookAndFeel = UIManager.getSystemLookAndFeelClassName(); else if (LOOKANDFEEL.equals("Motif")) lookAndFeel = "com.sun.java.swing.plaf.motif.MotifLookAndFeel"; else if (LOOKANDFEEL.equals("GTK+")) lookAndFeel = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel"; else { // ... oops! -- to handle the unexpected System.err.println("Unexpected value of LOOKANDFEEL specified: " + LOOKANDFEEL); lookAndFeel = UIManager.getCrossPlatformLookAndFeelClassName(); } try { UIManager.setLookAndFeel(lookAndFeel); } catch (ClassNotFoundException e) { System.err.println("Couldn't find class for specified look and feel:" + lookAndFeel); System.err.println("Did you include the L&F library in the class path?"); System.err.println("Using the default look and feel."); } catch (UnsupportedLookAndFeelException e) { System.err.println("Can't use the specified look and feel (" + lookAndFeel + ") on this platform."); System.err.println("Using the default look and feel."); } catch (Exception e) { System.err.println("Couldn't get specified look and feel (" + lookAndFeel + "), for some reason."); System.err.println("Using the default look and feel."); e.printStackTrace(); } } } private static void createAndShowGUI() { initLookAndFeel(); // Set the look and feel. JFrame.setDefaultLookAndFeelDecorated(true); // Make it so! JFrame frame = new JFrame("Button Listening"); // build frame with exit option! frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); CanYouHearMe app = new CanYouHearMe(); Component contents = app.createComponents(); frame.getContentPane().add(contents, BorderLayout.CENTER); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } } ); // end of 'invokeLater' } // end of Main }