Java by API/javax.swing/KeyStroke — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 14:19, 31 мая 2010
Содержание
KeyStroke.getKeyStroke(char ch)
import java.awt.GridLayout;
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.ActionMap;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.KeyStroke;
public class MainClass {
public static void main(String args[]) {
String ACTION_KEY = "The Action";
JFrame frame = new JFrame("KeyStroke Sample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton buttonA = new JButton("FOCUSED (control alt 7)");
JButton buttonB = new JButton("FOCUS/RELEASE (VK_ENTER)");
JButton buttonC = new JButton("ANCESTOR (VK_F4+SHIFT_MASK)");
JButton buttonD = new JButton("WINDOW (" ")");
Action actionListener = new AbstractAction() {
public void actionPerformed(ActionEvent actionEvent) {
JButton source = (JButton) actionEvent.getSource();
System.out.println("Activated: " + source.getText());
}
};
KeyStroke controlAlt7 = KeyStroke.getKeyStroke("control alt 7");
InputMap inputMap = buttonA.getInputMap();
inputMap.put(controlAlt7, ACTION_KEY);
ActionMap actionMap = buttonA.getActionMap();
actionMap.put(ACTION_KEY, actionListener);
KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true);
inputMap = buttonB.getInputMap();
inputMap.put(enter, ACTION_KEY);
buttonB.setActionMap(actionMap);
KeyStroke shiftF4 = KeyStroke.getKeyStroke(KeyEvent.VK_F4,
InputEvent.SHIFT_MASK);
inputMap = buttonC
.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
inputMap.put(shiftF4, ACTION_KEY);
buttonC.setActionMap(actionMap);
KeyStroke space = KeyStroke.getKeyStroke(" ");
inputMap = buttonD.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
inputMap.put(space, ACTION_KEY);
buttonD.setActionMap(actionMap);
frame.setLayout(new GridLayout(2, 2));
frame.add(buttonA);
frame.add(buttonB);
frame.add(buttonC);
frame.add(buttonD);
frame.setSize(400, 200);
frame.setVisible(true);
}
}
KeyStroke.getKeyStroke("control alt 7")
import java.awt.GridLayout;
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.ActionMap;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.KeyStroke;
public class MainClass {
public static void main(String args[]) {
String ACTION_KEY = "The Action";
JFrame frame = new JFrame("KeyStroke Sample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton buttonA = new JButton("FOCUSED (control alt 7)");
JButton buttonB = new JButton("FOCUS/RELEASE (VK_ENTER)");
JButton buttonC = new JButton("ANCESTOR (VK_F4+SHIFT_MASK)");
JButton buttonD = new JButton("WINDOW (" ")");
Action actionListener = new AbstractAction() {
public void actionPerformed(ActionEvent actionEvent) {
JButton source = (JButton) actionEvent.getSource();
System.out.println("Activated: " + source.getText());
}
};
KeyStroke controlAlt7 = KeyStroke.getKeyStroke("control alt 7");
InputMap inputMap = buttonA.getInputMap();
inputMap.put(controlAlt7, ACTION_KEY);
ActionMap actionMap = buttonA.getActionMap();
actionMap.put(ACTION_KEY, actionListener);
KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true);
inputMap = buttonB.getInputMap();
inputMap.put(enter, ACTION_KEY);
buttonB.setActionMap(actionMap);
KeyStroke shiftF4 = KeyStroke.getKeyStroke(KeyEvent.VK_F4,
InputEvent.SHIFT_MASK);
inputMap = buttonC
.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
inputMap.put(shiftF4, ACTION_KEY);
buttonC.setActionMap(actionMap);
KeyStroke space = KeyStroke.getKeyStroke(" ");
inputMap = buttonD.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
inputMap.put(space, ACTION_KEY);
buttonD.setActionMap(actionMap);
frame.setLayout(new GridLayout(2, 2));
frame.add(buttonA);
frame.add(buttonB);
frame.add(buttonC);
frame.add(buttonD);
frame.setSize(400, 200);
frame.setVisible(true);
}
}
KeyStroke: getKeyStroke(int keyCode,int modifiers,boolean onKeyRelease)
import java.awt.GridLayout;
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.ActionMap;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.KeyStroke;
public class MainClass {
public static void main(String args[]) {
String ACTION_KEY = "The Action";
JFrame frame = new JFrame("KeyStroke Sample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton buttonA = new JButton("FOCUSED (control alt 7)");
JButton buttonB = new JButton("FOCUS/RELEASE (VK_ENTER)");
JButton buttonC = new JButton("ANCESTOR (VK_F4+SHIFT_MASK)");
JButton buttonD = new JButton("WINDOW (" ")");
Action actionListener = new AbstractAction() {
public void actionPerformed(ActionEvent actionEvent) {
JButton source = (JButton) actionEvent.getSource();
System.out.println("Activated: " + source.getText());
}
};
KeyStroke controlAlt7 = KeyStroke.getKeyStroke("control alt 7");
InputMap inputMap = buttonA.getInputMap();
inputMap.put(controlAlt7, ACTION_KEY);
ActionMap actionMap = buttonA.getActionMap();
actionMap.put(ACTION_KEY, actionListener);
KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true);
inputMap = buttonB.getInputMap();
inputMap.put(enter, ACTION_KEY);
buttonB.setActionMap(actionMap);
KeyStroke shiftF4 = KeyStroke.getKeyStroke(KeyEvent.VK_F4,
InputEvent.SHIFT_MASK);
inputMap = buttonC
.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
inputMap.put(shiftF4, ACTION_KEY);
buttonC.setActionMap(actionMap);
KeyStroke space = KeyStroke.getKeyStroke(" ");
inputMap = buttonD.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
inputMap.put(space, ACTION_KEY);
buttonD.setActionMap(actionMap);
frame.setLayout(new GridLayout(2, 2));
frame.add(buttonA);
frame.add(buttonB);
frame.add(buttonC);
frame.add(buttonD);
frame.setSize(400, 200);
frame.setVisible(true);
}
}
KeyStroke.getKeyStroke(KeyEvent.VK_P,InputEvent.CTRL_MASK)
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);
}
}
KeyStroke.getKeyStroke("N", Event.CTRL_MASK)
import java.awt.Event;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.KeyStroke;
public class MainClass extends JFrame {
public MainClass() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setJMenuBar(menuBar);
JMenu fileMenu = new JMenu("File");
JMenu elementMenu = new JMenu("Elements");
fileMenu.setMnemonic("F");
elementMenu.setMnemonic("E");
// Construct the file pull down menu
newItem = fileMenu.add("New");
openItem = fileMenu.add("Open");
closeItem = fileMenu.add("Close");
fileMenu.addSeparator();
saveItem = fileMenu.add("Save");
saveAsItem = fileMenu.add("Save As...");
JMenu colorMenu = new JMenu("Color");
fileMenu.addSeparator();
printItem = fileMenu.add("Print");
newItem.setAccelerator(KeyStroke.getKeyStroke("N", Event.CTRL_MASK));
openItem.setAccelerator(KeyStroke.getKeyStroke("O", Event.CTRL_MASK));
saveItem.setAccelerator(KeyStroke.getKeyStroke("S", Event.CTRL_MASK));
printItem.setAccelerator(KeyStroke.getKeyStroke("P", Event.CTRL_MASK));
elementMenu.add(lineItem = new JRadioButtonMenuItem("Line", true));
elementMenu
.add(rectangleItem = new JRadioButtonMenuItem("Rectangle", false));
elementMenu.add(circleItem = new JRadioButtonMenuItem("Circle", false));
elementMenu.add(curveItem = new JRadioButtonMenuItem("Curve", false));
ButtonGroup types = new ButtonGroup();
types.add(lineItem);
types.add(rectangleItem);
types.add(circleItem);
types.add(curveItem);
lineItem.setAccelerator(KeyStroke.getKeyStroke("L", Event.CTRL_MASK));
rectangleItem.setAccelerator(KeyStroke.getKeyStroke("E", Event.CTRL_MASK));
circleItem.setAccelerator(KeyStroke.getKeyStroke("I", Event.CTRL_MASK));
curveItem.setAccelerator(KeyStroke.getKeyStroke("V", Event.CTRL_MASK));
elementMenu.addSeparator();
elementMenu.add(colorMenu); // Add the sub-menu
colorMenu.add(redItem = new JCheckBoxMenuItem("Red", false));
colorMenu.add(yellowItem = new JCheckBoxMenuItem("Yellow", false));
colorMenu.add(greenItem = new JCheckBoxMenuItem("Green", false));
colorMenu.add(blueItem = new JCheckBoxMenuItem("Blue", true));
// Add element color accelerators
redItem.setAccelerator(KeyStroke.getKeyStroke("R", Event.CTRL_MASK));
yellowItem.setAccelerator(KeyStroke.getKeyStroke("Y", Event.CTRL_MASK));
greenItem.setAccelerator(KeyStroke.getKeyStroke("G", Event.CTRL_MASK));
blueItem.setAccelerator(KeyStroke.getKeyStroke("B", Event.CTRL_MASK));
menuBar.add(fileMenu);
menuBar.add(elementMenu);
}
private JMenuBar menuBar = new JMenuBar();
// File menu items
private JMenuItem newItem, openItem, closeItem, saveItem, saveAsItem,
printItem;
// Element menu items
private JRadioButtonMenuItem lineItem, rectangleItem, circleItem, curveItem,
textItem;
private JCheckBoxMenuItem redItem, yellowItem, greenItem, blueItem;
public static void main(String[] args) {
JFrame aWindow = new MainClass();
aWindow.setSize(200,200);
aWindow.setVisible(true);
}
}