📄 e855. creating an action.txt
字号:
An action is used by a Swing component to invoke a method. To create an action, the actionPerformed() method must be overridden. The action is then attached to a component such as a button or bound to a keystroke in a text component. When the button is activated or the keystroke is pressed, the action's actionPerformed() method is called. Actions can be attached to more than one component or keystroke.
Actions can also contain other optional information, such as a label, icon, or tool tip text. When the action is attached to a component, the component may use this information if present. For example, if the action has a label and icon, a button created using that action will use that label and icon.
This example defines an action and creates a button using the action.
// Create an action object
public Action action = new AbstractAction("Action Name") {
// This is an instance initializer; it is executed just after the
// constructor of the superclass is invoked
{
// The following values are completely optional
// Set tool tip text
putValue(Action.SHORT_DESCRIPTION, "Tool Tip Text");
// This text is not directly used by any Swing component;
// however, this text could be used in a help system
putValue(Action.LONG_DESCRIPTION, "Context-Sensitive Help Text");
// Set an icon
Icon icon = new ImageIcon("icon.gif");
putValue(Action.SMALL_ICON, icon);
// Set a mnemonic character. In most look and feels, this causes the
// specified character to be underlined This indicates that if the component
// using this action has the focus and In some look and feels, this causes
// the specified character in the label to be underlined and
putValue(Action.MNEMONIC_KEY, new Integer(java.awt.event.KeyEvent.VK_A));
// Set an accelerator key; this value is used by menu items
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("control F2"));
}
// This method is called when the action is invoked
public void actionPerformed(ActionEvent evt) {
// Perform action
}
};
Create a button using the action object:
JButton button = new JButton(action);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -