62,620
社区成员
发帖
与我相关
我的任务
分享
public class MonitorText {
static JFrame f = new JFrame();
static JTextArea text = new JTextArea(10, 50);
void init() {
text.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e){
if(e.isControlDown() && e.getKeyCode() == e.VK_Z){
System.out.println("ctrl + z");
}
}
});
f.add(text);
f.pack();
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new MonitorText().init();
}
}
text.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e){
Boolean ctrl = false;
Boolean z= false;
if (ctrl&&z ) {
//to do sth
}
if(e.getKeyCode() == KeyEvent.VK_Z){
z= true;
}
if(e.getKeyCode() == KeyEvent.VK_CONTROL){
ctrl = true;
}
}
});
试试这个,我没运行过,应该可以满足你的要求addKeyListener(new KeyAdapter(){
@Override public void keyPressed(KeyEvent e){
if(e.isControlDown() && (e.getKeyCode() == 'Z'))
System.out.println("Ctrl + Z pressed");
}
});Action printAction = new AbstractAction("Print"){
@Override public void actionPerformed(ActionEvent e){
...; // print sth.
}
};
printAction.put(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("control Z"));
fileMenu.add(printAction);
toolbar.add(printAction);