Java by API/javax.swing/AbstractAction — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 14:18, 31 мая 2010
AbstractAction: putValue(String key, Object newValue)
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JToolBar;
public class MainClass {
public static void main(String args[]) {
JFrame frame = new JFrame("Action Sample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final Action printAction = new PrintHelloAction();
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("File");
menuBar.add(menu);
menu.add(new JMenuItem(printAction));
JToolBar toolbar = new JToolBar();
toolbar.add(new JButton(printAction));
JButton enableButton = new JButton("Enable");
ActionListener enableActionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
printAction.setEnabled(true);
}
};
enableButton.addActionListener(enableActionListener);
JButton disableButton = new JButton("Disable");
ActionListener disableActionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
printAction.setEnabled(false);
}
};
disableButton.addActionListener(disableActionListener);
JButton relabelButton = new JButton("Relabel");
ActionListener relabelActionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
printAction.putValue(Action.NAME, "Hello, World");
}
};
relabelButton.addActionListener(relabelActionListener);
JPanel buttonPanel = new JPanel();
buttonPanel.add(enableButton);
buttonPanel.add(disableButton);
buttonPanel.add(relabelButton);
frame.setJMenuBar(menuBar);
frame.add(toolbar, BorderLayout.SOUTH);
frame.add(buttonPanel, BorderLayout.NORTH);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
class PrintHelloAction extends AbstractAction {
PrintHelloAction() {
super("Print");
putValue(Action.SHORT_DESCRIPTION, "Hello, World");
}
public void actionPerformed(ActionEvent actionEvent) {
System.out.println("Hello, World");
}
}
AbstractAction: setEnabled(boolean newValue)
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JToolBar;
public class MainClass {
public static void main(String args[]) {
JFrame frame = new JFrame("Action Sample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final Action printAction = new PrintHelloAction();
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("File");
menuBar.add(menu);
menu.add(new JMenuItem(printAction));
JToolBar toolbar = new JToolBar();
toolbar.add(new JButton(printAction));
JButton enableButton = new JButton("Enable");
ActionListener enableActionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
printAction.setEnabled(true);
}
};
enableButton.addActionListener(enableActionListener);
JButton disableButton = new JButton("Disable");
ActionListener disableActionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
printAction.setEnabled(false);
}
};
disableButton.addActionListener(disableActionListener);
JButton relabelButton = new JButton("Relabel");
ActionListener relabelActionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
printAction.putValue(Action.NAME, "Hello, World");
}
};
relabelButton.addActionListener(relabelActionListener);
JPanel buttonPanel = new JPanel();
buttonPanel.add(enableButton);
buttonPanel.add(disableButton);
buttonPanel.add(relabelButton);
frame.setJMenuBar(menuBar);
frame.add(toolbar, BorderLayout.SOUTH);
frame.add(buttonPanel, BorderLayout.NORTH);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
class PrintHelloAction extends AbstractAction {
PrintHelloAction() {
super("Print");
putValue(Action.SHORT_DESCRIPTION, "Hello, World");
}
public void actionPerformed(ActionEvent actionEvent) {
System.out.println("Hello, World");
}
}
extends AbstractAction
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);
}
}