Java by API/javax.swing/Action — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 20:43, 31 мая 2010
Содержание
Action.MNEMONIC_KEY
<source lang="java">
import java.awt.BorderLayout; import java.awt.Color; import java.awt.ruponent; import java.awt.Container; import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import javax.swing.AbstractAction; import javax.swing.Action; import javax.swing.Icon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JToolBar; import javax.swing.plaf.metal.MetalIconFactory; public class MainClass {
public static void main(String[] a) { final int STRING_POSITION = 1; Object buttonColors[][] = { { Color.RED, "RED" }, { Color.BLUE, "BLUE" }, { Color.GREEN, "GREEN" }, { Color.BLACK, "BLACK" }, null, // separator { Color.CYAN, "CYAN" } }; JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ActionListener actionListener = new TheActionListener(); JToolBar toolbar = new JToolBar(); toolbar.setRollover(true); for (Object[] color : buttonColors) { if (color == null) { toolbar.addSeparator(); } else { Icon icon = MetalIconFactory.getTreeComputerIcon(); JButton button = new JButton(icon); button.setActionCommand((String) color[STRING_POSITION]); button.addActionListener(actionListener); toolbar.add(button); } } Action action = new ShowAction(toolbar); JButton button = new JButton(action); toolbar.add(button); Container contentPane = frame.getContentPane(); contentPane.add(toolbar, BorderLayout.NORTH); JTextArea textArea = new JTextArea(); JScrollPane pane = new JScrollPane(textArea); contentPane.add(pane, BorderLayout.CENTER); frame.setSize(350, 150); frame.setVisible(true); } public static class TheActionListener implements ActionListener { public void actionPerformed(ActionEvent actionEvent) { System.out.println(actionEvent.getActionCommand()); } };
} class ShowAction extends AbstractAction {
Component parentComponent; public ShowAction(Component parentComponent) { super("About"); putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_A)); putValue(Action.SMALL_ICON, MetalIconFactory.getFileChooserHomeFolderIcon()); this.parentComponent = parentComponent; } public void actionPerformed(ActionEvent actionEvent) { JOptionPane.showMessageDialog(parentComponent, "About Swing", "About Box V2.0", JOptionPane.INFORMATION_MESSAGE); }
}
</source>
Action.NAME
<source lang="java">
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); }
}
</source>
Action: setEnabled(boolean b)
<source lang="java">
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); }
}
</source>
Action.SHORT_DESCRIPTION
<source lang="java">
import java.awt.BorderLayout; import java.awt.ruponent; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.InputEvent; import java.awt.event.KeyEvent; import javax.swing.AbstractAction; import javax.swing.Action; import javax.swing.Icon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.KeyStroke; public class MainClass {
public static void main(String args[]) { JFrame frame = new JFrame("Mnemonic/Accelerator Sample"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final Action printAction = new PrintHelloAction(); JMenuBar menuBar = new JMenuBar(); JMenu menu = new JMenu("File"); menuBar.add(menu); JMenuItem menuItem = new JMenuItem("Print"); KeyStroke ctrlP = KeyStroke.getKeyStroke(KeyEvent.VK_P, InputEvent.CTRL_MASK); menuItem.setAccelerator(ctrlP); menuItem.addActionListener(printAction); menu.add(menuItem); JButton fileButton = new JButton("About"); fileButton.setMnemonic(KeyEvent.VK_A); fileButton.addActionListener(printAction); frame.setJMenuBar(menuBar); frame.add(fileButton, BorderLayout.SOUTH); frame.setSize(300, 100); frame.setVisible(true); }
} class PrintHelloAction extends AbstractAction {
private static final Icon printIcon = new MyIcon(); PrintHelloAction() { super("Print", printIcon); putValue(Action.SHORT_DESCRIPTION, "Hello, World"); } public void actionPerformed(ActionEvent actionEvent) { System.out.println("Hello, World"); }
} class MyIcon implements Icon {
public int getIconWidth() { return 32; } public int getIconHeight() { return 32; } public void paintIcon(Component c, Graphics g, int x, int y) { g.drawString("jexp.ru", 0, 20); }
}
</source>
Action.SMALL_ICON
<source lang="java">
import java.awt.BorderLayout; import java.awt.Color; import java.awt.ruponent; import java.awt.Container; import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import javax.swing.AbstractAction; import javax.swing.Action; import javax.swing.Icon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JToolBar; import javax.swing.plaf.metal.MetalIconFactory; public class MainClass {
public static void main(String[] a) { final int STRING_POSITION = 1; Object buttonColors[][] = { { Color.RED, "RED" }, { Color.BLUE, "BLUE" }, { Color.GREEN, "GREEN" }, { Color.BLACK, "BLACK" }, null, // separator { Color.CYAN, "CYAN" } }; JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ActionListener actionListener = new TheActionListener(); JToolBar toolbar = new JToolBar(); toolbar.setRollover(true); for (Object[] color : buttonColors) { if (color == null) { toolbar.addSeparator(); } else { Icon icon = MetalIconFactory.getTreeComputerIcon(); JButton button = new JButton(icon); button.setActionCommand((String) color[STRING_POSITION]); button.addActionListener(actionListener); toolbar.add(button); } } Action action = new ShowAction(toolbar); JButton button = new JButton(action); toolbar.add(button); Container contentPane = frame.getContentPane(); contentPane.add(toolbar, BorderLayout.NORTH); JTextArea textArea = new JTextArea(); JScrollPane pane = new JScrollPane(textArea); contentPane.add(pane, BorderLayout.CENTER); frame.setSize(350, 150); frame.setVisible(true); } public static class TheActionListener implements ActionListener { public void actionPerformed(ActionEvent actionEvent) { System.out.println(actionEvent.getActionCommand()); } };
} class ShowAction extends AbstractAction {
Component parentComponent; public ShowAction(Component parentComponent) { super("About"); putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_A)); putValue(Action.SMALL_ICON, MetalIconFactory.getFileChooserHomeFolderIcon()); this.parentComponent = parentComponent; } public void actionPerformed(ActionEvent actionEvent) { JOptionPane.showMessageDialog(parentComponent, "About Swing", "About Box V2.0", JOptionPane.INFORMATION_MESSAGE); }
}
</source>