00001 package jpl.util;
00002
00003 import java.awt.BorderLayout;
00004 import java.awt.GridLayout;
00005 import java.awt.event.ActionEvent;
00006 import java.awt.event.ActionListener;
00007 import java.awt.event.WindowAdapter;
00008 import java.awt.event.WindowEvent;
00009 import javax.swing.BorderFactory;
00010 import javax.swing.JButton;
00011 import javax.swing.JFrame;
00012 import javax.swing.JLabel;
00013 import javax.swing.JPanel;
00014 import javax.swing.UIManager;
00015
00016 public class SwingGadget extends JFrame
00017 {
00018 private static final long serialVersionUID = 1L;
00019
00020 public int numClicks = 0;
00021
00022 private static String labelPrefix = "Number of button clicks: ";
00023 final JLabel label = new JLabel(labelPrefix + "0 ");
00024
00025 public SwingGadget(String caption)
00026 {
00027 super(caption);
00028
00029 JButton button = new JButton("I'm a Swing button!");
00030
00031 button.addActionListener(
00032 new ActionListener()
00033 {
00034 public void actionPerformed(ActionEvent e)
00035 {
00036 inc();
00037 }
00038 }
00039 );
00040
00041 label.setLabelFor(button);
00042
00043 JPanel pane = new JPanel();
00044 pane.setBorder(BorderFactory.createEmptyBorder(30,30,10,30));
00045 pane.setLayout(new GridLayout(0, 1));
00046 pane.add(button);
00047 pane.add(label);
00048
00049 try {
00050 UIManager.setLookAndFeel(
00051 UIManager.getCrossPlatformLookAndFeelClassName()
00052 );
00053 }
00054 catch (Exception e)
00055 {
00056 }
00057
00058 getContentPane().add(pane, BorderLayout.CENTER);
00059
00060 addWindowListener(new WindowAdapter()
00061 {
00062 public void windowClosing(WindowEvent e)
00063 {
00064
00065 setVisible(false);
00066 }
00067 }
00068 );
00069 pack();
00070 setVisible(true);
00071 }
00072
00073 public synchronized void inc()
00074 {
00075 numClicks++;
00076 label.setText(labelPrefix + numClicks);
00077 notifyAll();
00078 }
00079
00080 public synchronized boolean dec()
00081 {
00082 try {
00083 while ( numClicks <= 0 )
00084 {
00085 wait();
00086 }
00087 numClicks--;
00088 label.setText(labelPrefix + numClicks);
00089 return true;
00090 }
00091 catch ( InterruptedException e )
00092 {
00093 return false;
00094 }
00095 }
00096 }
00097
00098
00099