6 个解决方案
#1
我给你举个例子:
MenuBar menubar = new MenuBar();
Menu menu = new Menu("游戏");
MenuShortcut paMsc = new MenuShortcut(KeyEvent.VK_P, false);
MenuShortcut stMsc = new MenuShortcut(KeyEvent.VK_S, false);//实现Ctrl+S 的快捷键 你去看文档
MenuItem pa = new MenuItem("暂停", paMsc);
MenuItem st = new MenuItem("继续", stMsc);
在Frame窗口中:
menubar.add(menu);
menu.add(pa);
menu.add(st);
pa.addActionListener(new MenuListener);
st.addActionListener(new MenuListener);
this.setMenuBar(menubar);
//监听器类
private class MenuListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("暂停")) {
System.out.println("你按下了暂停菜单");
} else if (e.getActionCommand().equals("继续")) {
System.out.println("你按下了继续菜单");
}
}
}
这样你按 Ctrl + p System.out.println("你按下了暂停菜单");
按 Ctrl +s System.out.println("你按下了继续菜单");
MenuBar menubar = new MenuBar();
Menu menu = new Menu("游戏");
MenuShortcut paMsc = new MenuShortcut(KeyEvent.VK_P, false);
MenuShortcut stMsc = new MenuShortcut(KeyEvent.VK_S, false);//实现Ctrl+S 的快捷键 你去看文档
MenuItem pa = new MenuItem("暂停", paMsc);
MenuItem st = new MenuItem("继续", stMsc);
在Frame窗口中:
menubar.add(menu);
menu.add(pa);
menu.add(st);
pa.addActionListener(new MenuListener);
st.addActionListener(new MenuListener);
this.setMenuBar(menubar);
//监听器类
private class MenuListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("暂停")) {
System.out.println("你按下了暂停菜单");
} else if (e.getActionCommand().equals("继续")) {
System.out.println("你按下了继续菜单");
}
}
}
这样你按 Ctrl + p System.out.println("你按下了暂停菜单");
按 Ctrl +s System.out.println("你按下了继续菜单");
#2
你先看看你的组合键打印出来的是什么字符。最后通过事件的标示符来判断是不是你想要的组合键。
比如我实现的Ctrl+X关闭一个面板。代码如下:
比如我实现的Ctrl+X关闭一个面板。代码如下:
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.KeyStroke;
public class MainFace extends JFrame {
public MainFace() {
super("Ideal GUI");
//添加默认的点击关闭按钮的动作实现
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
//下面的代码是为主面板添加键盘事件监听
addKeyListener(new KeyListener() {
@Override
public void keyPressed(KeyEvent e) {
//按下某个键时调用此方法
}
@Override
public void keyReleased(KeyEvent e) {
//释放某个键时调用此方法
}
@Override
public void keyTyped(KeyEvent e) {
//键入某个键时调用此方法
KeyStroke.getKeyStroke(KeyEvent.VK_Q, InputEvent.CTRL_MASK);
System.out.println(" code = " + e.getKeyCode());
if (e.getKeyChar() == '') {
System.exit(1);
}
}
});
}
public static void main(String args[]) {
MainFace mGUI = new MainFace();
mGUI.requestFocus();
}
}
#3
代码有点小错误,看不到效果,已经修正
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
public class MainFace extends JFrame {
public MainFace() {
super("Ideal GUI");
//添加默认的点击关闭按钮的动作实现
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
//下面的代码是为主面板添加键盘事件监听
addKeyListener(new KeyListener() {
@Override
public void keyPressed(KeyEvent e) {
//按下某个键时调用此方法
}
@Override
public void keyReleased(KeyEvent e) {
//释放某个键时调用此方法
}
@Override
public void keyTyped(KeyEvent e) {
//键入某个键时调用此方法
if (e.getKeyChar() == '') {
System.exit(1);
}
}
});
}
public static void main(String args[]) {
MainFace mGUI = new MainFace();
mGUI.setSize(400, 300);
mGUI.setVisible(true);
mGUI.requestFocus();
}
}
#4
public class TestEvent {
public static void main(String[] args) {
JFrame frame = new JFrame(" TestKeyPressed ");
frame.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if ((e.isControlDown() == true)
&& (e.getKeyCode() == KeyEvent.VK_UP)) {
System.out.println("ctrl + UP has pressed");
} else {
System.out.println("other key pressed");
}
}
});
frame.setSize(500, 500);
frame.setVisible(true);
}
}
#5
谢谢各位高手
#6
谢谢,对我帮助很大,
#1
我给你举个例子:
MenuBar menubar = new MenuBar();
Menu menu = new Menu("游戏");
MenuShortcut paMsc = new MenuShortcut(KeyEvent.VK_P, false);
MenuShortcut stMsc = new MenuShortcut(KeyEvent.VK_S, false);//实现Ctrl+S 的快捷键 你去看文档
MenuItem pa = new MenuItem("暂停", paMsc);
MenuItem st = new MenuItem("继续", stMsc);
在Frame窗口中:
menubar.add(menu);
menu.add(pa);
menu.add(st);
pa.addActionListener(new MenuListener);
st.addActionListener(new MenuListener);
this.setMenuBar(menubar);
//监听器类
private class MenuListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("暂停")) {
System.out.println("你按下了暂停菜单");
} else if (e.getActionCommand().equals("继续")) {
System.out.println("你按下了继续菜单");
}
}
}
这样你按 Ctrl + p System.out.println("你按下了暂停菜单");
按 Ctrl +s System.out.println("你按下了继续菜单");
MenuBar menubar = new MenuBar();
Menu menu = new Menu("游戏");
MenuShortcut paMsc = new MenuShortcut(KeyEvent.VK_P, false);
MenuShortcut stMsc = new MenuShortcut(KeyEvent.VK_S, false);//实现Ctrl+S 的快捷键 你去看文档
MenuItem pa = new MenuItem("暂停", paMsc);
MenuItem st = new MenuItem("继续", stMsc);
在Frame窗口中:
menubar.add(menu);
menu.add(pa);
menu.add(st);
pa.addActionListener(new MenuListener);
st.addActionListener(new MenuListener);
this.setMenuBar(menubar);
//监听器类
private class MenuListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("暂停")) {
System.out.println("你按下了暂停菜单");
} else if (e.getActionCommand().equals("继续")) {
System.out.println("你按下了继续菜单");
}
}
}
这样你按 Ctrl + p System.out.println("你按下了暂停菜单");
按 Ctrl +s System.out.println("你按下了继续菜单");
#2
你先看看你的组合键打印出来的是什么字符。最后通过事件的标示符来判断是不是你想要的组合键。
比如我实现的Ctrl+X关闭一个面板。代码如下:
比如我实现的Ctrl+X关闭一个面板。代码如下:
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.KeyStroke;
public class MainFace extends JFrame {
public MainFace() {
super("Ideal GUI");
//添加默认的点击关闭按钮的动作实现
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
//下面的代码是为主面板添加键盘事件监听
addKeyListener(new KeyListener() {
@Override
public void keyPressed(KeyEvent e) {
//按下某个键时调用此方法
}
@Override
public void keyReleased(KeyEvent e) {
//释放某个键时调用此方法
}
@Override
public void keyTyped(KeyEvent e) {
//键入某个键时调用此方法
KeyStroke.getKeyStroke(KeyEvent.VK_Q, InputEvent.CTRL_MASK);
System.out.println(" code = " + e.getKeyCode());
if (e.getKeyChar() == '') {
System.exit(1);
}
}
});
}
public static void main(String args[]) {
MainFace mGUI = new MainFace();
mGUI.requestFocus();
}
}
#3
代码有点小错误,看不到效果,已经修正
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
public class MainFace extends JFrame {
public MainFace() {
super("Ideal GUI");
//添加默认的点击关闭按钮的动作实现
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
//下面的代码是为主面板添加键盘事件监听
addKeyListener(new KeyListener() {
@Override
public void keyPressed(KeyEvent e) {
//按下某个键时调用此方法
}
@Override
public void keyReleased(KeyEvent e) {
//释放某个键时调用此方法
}
@Override
public void keyTyped(KeyEvent e) {
//键入某个键时调用此方法
if (e.getKeyChar() == '') {
System.exit(1);
}
}
});
}
public static void main(String args[]) {
MainFace mGUI = new MainFace();
mGUI.setSize(400, 300);
mGUI.setVisible(true);
mGUI.requestFocus();
}
}
#4
public class TestEvent {
public static void main(String[] args) {
JFrame frame = new JFrame(" TestKeyPressed ");
frame.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if ((e.isControlDown() == true)
&& (e.getKeyCode() == KeyEvent.VK_UP)) {
System.out.println("ctrl + UP has pressed");
} else {
System.out.println("other key pressed");
}
}
});
frame.setSize(500, 500);
frame.setVisible(true);
}
}
#5
谢谢各位高手
#6
谢谢,对我帮助很大,