Action.NAME
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
public class MainClass extends JFrame {
public static final int MIN_NUMBER = 2;
public static final int MAX_NUMBER = 13;
private int currentNumber = MIN_NUMBER;
private int favoriteNumber = 9;
private JLabel numberLabel = new JLabel();
private Action upAction = new UpAction();
private Action downAction = new DownAction();
private GotoFavoriteAction gotoFavoriteAction = new GotoFavoriteAction();
private Action setFavoriteAction = new SetFavoriteAction();
public class UpAction extends AbstractAction {
public UpAction() {
putValue(NAME, "Number Add");
putValue(SMALL_ICON, new ImageIcon("images/up.gif"));
putValue(SHORT_DESCRIPTION, "Increment the number");
putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_A));
}
public void actionPerformed(ActionEvent ae) {
setChannel(currentNumber + 1);
}
}
public class DownAction extends AbstractAction {
public DownAction() {
putValue(NAME, "Number Down");
putValue(SMALL_ICON, new ImageIcon("images/down.gif"));
putValue(SHORT_DESCRIPTION, "Decrement the number");
putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_D));
}
public void actionPerformed(ActionEvent ae) {
setChannel(currentNumber - 1);
}
}
public class GotoFavoriteAction extends AbstractAction {
public GotoFavoriteAction() {
putValue(SMALL_ICON, new ImageIcon("images/fav.gif"));
putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_G));
updateProperties();
}
public void updateProperties() {
putValue(NAME, "Go to number " + favoriteNumber);
putValue(SHORT_DESCRIPTION, "Change the number to " + favoriteNumber);
}
public void actionPerformed(ActionEvent ae) {
setChannel(favoriteNumber);
}
}
public class SetFavoriteAction extends AbstractAction {
public SetFavoriteAction() {
putValue(NAME, "Set 'Go to' number");
putValue(SMALL_ICON, new ImageIcon("images/set.gif"));
putValue(SHORT_DESCRIPTION, "Make current number the Favorite number");
putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_S));
}
public void actionPerformed(ActionEvent ae) {
favoriteNumber = currentNumber;
gotoFavoriteAction.updateProperties();
setEnabled(false);
gotoFavoriteAction.setEnabled(false);
}
}
public MainClass() {
super();
setChannel(currentNumber);
numberLabel.setHorizontalAlignment(JLabel.CENTER);
numberLabel.setFont(new Font("Serif", Font.PLAIN, 32));
getContentPane().add(numberLabel, BorderLayout.NORTH);
JPanel buttonPanel = new JPanel(new GridLayout(2, 2, 16, 6));
buttonPanel.setBorder(BorderFactory.createEmptyBorder(6, 16, 16, 16));
getContentPane().add(buttonPanel, BorderLayout.CENTER);
buttonPanel.add(new JButton(upAction));
buttonPanel.add(new JButton(gotoFavoriteAction));
buttonPanel.add(new JButton(downAction));
buttonPanel.add(new JButton(setFavoriteAction));
JMenuBar mb = new JMenuBar();
JMenu menu = new JMenu("Number");
menu.add(new JMenuItem(upAction));
menu.add(new JMenuItem(downAction));
menu.addSeparator();
menu.add(new JMenuItem(gotoFavoriteAction));
menu.add(new JMenuItem(setFavoriteAction));
mb.add(menu);
setJMenuBar(mb);
}
public void setChannel(int chan) {
currentNumber = chan;
numberLabel.setText("Now tuned to number: " + currentNumber);
downAction.setEnabled(currentNumber > MIN_NUMBER);
upAction.setEnabled(currentNumber < MAX_NUMBER);
gotoFavoriteAction.setEnabled(currentNumber != favoriteNumber);
setFavoriteAction.setEnabled(currentNumber != favoriteNumber);
}
public static void main(String argv[]) {
JFrame f = new MainClass();
f.setSize(400, 180);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
Related examples in the same category