所以我一直在努力为Java设置一个非常基本的菜单,有几种不同的方式来选择MenuItems,我遇到了一个奇怪的情况。如果我尝试使用KeyListener和KeyEvent的 F1 - F12 键,它会根据字母表中的最后一个字母读取备用键。特别是它:
F1 = P F2 = Q F3 = R F4 = S F1 = T F6 = U F7 = V F8 = W F9 = X F10 = Y F11 = Z
我无法弄清楚是什么触发了F12功能。
在我的代码中,我在创建KeyEvent.VK_Fnum
时使用JMenuItems
方法,所以我真的不确定是什么。如果有一些奇怪的规则阻止我直接使用 F1 - F12 我还没有找到它用于Java,所以任何帮助都表示赞赏。
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.event.MenuEvent;
import java.awt.*;
import java.awt.event.*;
public class MathGameMenuBar implements ActionListener, KeyListener{
public String fileName1;
public String fileName2;
public String fileName3;
public MathGameMenuBar(){
fileName1 = "Image1.jpg";
fileName2 = "Image2.jpg";
fileName3 = "Image3.jpg";
}
public JMenuBar MenuBarCreator(){
JMenuBar menuBar = new JMenuBar();
JMenu ImageMenu = new JMenu("Image");
JMenu ProblemNumMenu = new JMenu("Problem #");
JMenu NumSelectMenu = new JMenu("Base Number Select");
JMenu ImageSubMenu = new JMenu("Image List");
JMenuItem Image1 = new JMenuItem("Image 1", KeyEvent.VK_F1);
JMenuItem Image2 = new JMenuItem("Image 2", KeyEvent.VK_F2);
JMenuItem Image3 = new JMenuItem("Image 3", KeyEvent.VK_F3);
JMenuItem Split4 = new JMenuItem("4 Problems", KeyEvent.VK_F1);
JMenuItem Split9 = new JMenuItem("9 Problems", KeyEvent.VK_F2);
JMenuItem Split16 = new JMenuItem("16 Problems", KeyEvent.VK_F3);
JMenuItem Select1 = new JMenuItem("1", KeyEvent.VK_F1);
JMenuItem Select2 = new JMenuItem("2", KeyEvent.VK_F2);
JMenuItem Select3 = new JMenuItem("3", KeyEvent.VK_F3);
JMenuItem Select4 = new JMenuItem("4", KeyEvent.VK_F4);
JMenuItem Select5 = new JMenuItem("5", KeyEvent.VK_F5);
JMenuItem Select6 = new JMenuItem("6", KeyEvent.VK_F6);
JMenuItem Select7 = new JMenuItem("7", KeyEvent.VK_F7);
JMenuItem Select8 = new JMenuItem("8", KeyEvent.VK_F8);
JMenuItem Select9 = new JMenuItem("9", KeyEvent.VK_F9);
JMenuItem Select10 = new JMenuItem("10", KeyEvent.VK_F10);
JMenuItem Select11 = new JMenuItem("11", KeyEvent.VK_F11);
JMenuItem Select12 = new JMenuItem("12", KeyEvent.VK_F12);
Image1.addActionListener(this);
Image1.setActionCommand("i1");
Image2.addActionListener(this);
Image2.setActionCommand("i2");
Image3.addActionListener(this);
Image3.setActionCommand("i3");
Split4.addActionListener(this);
Split4.setActionCommand("s4");
Split9.addActionListener(this);
Split9.setActionCommand("s9");
Split16.addActionListener(this);
Split16.setActionCommand("s16");
Select1.addActionListener(this);
Select1.setActionCommand("sl1");
Select2.addActionListener(this);
Select2.setActionCommand("sl2");
Select3.addActionListener(this);
Select3.setActionCommand("sl3");
Select4.addActionListener(this);
Select4.setActionCommand("sl4");
Select5.addActionListener(this);
Select5.setActionCommand("sl5");
Select6.addActionListener(this);
Select6.setActionCommand("sl6");
Select7.addActionListener(this);
Select7.setActionCommand("sl7");
Select8.addActionListener(this);
Select8.setActionCommand("sl8");
Select9.addActionListener(this);
Select9.setActionCommand("sl9");
Select10.addActionListener(this);
Select10.setActionCommand("sl10");
Select11.addActionListener(this);
Select11.setActionCommand("sl11");
Select12.addActionListener(this);
Select12.setActionCommand("sl12");
menuBar.add(ImageMenu);
menuBar.add(ProblemNumMenu);
menuBar.add(NumSelectMenu);
ImageMenu.add(ImageSubMenu);
ImageSubMenu.add(Image1);
ImageSubMenu.add(Image2);
ImageSubMenu.add(Image3);
ProblemNumMenu.add(Split4);
ProblemNumMenu.add(Split9);
ProblemNumMenu.add(Split16);
NumSelectMenu.add(Select1);
NumSelectMenu.add(Select2);
NumSelectMenu.add(Select3);
NumSelectMenu.add(Select4);
NumSelectMenu.add(Select5);
NumSelectMenu.add(Select6);
NumSelectMenu.add(Select7);
NumSelectMenu.add(Select8);
NumSelectMenu.add(Select9);
NumSelectMenu.add(Select10);
NumSelectMenu.add(Select11);
NumSelectMenu.add(Select12);
return menuBar;
}
private int returnNum(int num){
return num;
}
private String getFileName1(){
return fileName1;
}
private String getFileName2(){
return fileName2;
}
private String getFileName3(){
return fileName3;
}
public void actionPerformed(ActionEvent e) {
if ("i1".equals(e.getActionCommand())){
getFileName1();
System.out.println("yupI1");
}
if ("i2".equals(e.getActionCommand())){
getFileName2();
System.out.println("yupI2");
}
if ("i3".equals(e.getActionCommand())){
getFileName3();
System.out.println("yupI3");
}
if ("s4".equals(e.getActionCommand())){
returnNum(4);
System.out.println("yupS4");
}
if ("s9".equals(e.getActionCommand())){
returnNum(9);
System.out.println("yupS9");
}
if ("s16".equals(e.getActionCommand())){
returnNum(16);
System.out.println("yupS16");
}
if ("sl1".equals(e.getActionCommand())){
returnNum(1);
System.out.println("yup1");
}
if ("sl2".equals(e.getActionCommand())){
returnNum(2);
System.out.println("yup2");
}
if ("sl3".equals(e.getActionCommand())){
returnNum(3);
System.out.println("yup3");
}
if ("sl4".equals(e.getActionCommand())){
returnNum(4);
System.out.println("yup4");
}
if ("sl5".equals(e.getActionCommand())){
returnNum(5);
System.out.println("yup5");
}
if ("sl6".equals(e.getActionCommand())){
returnNum(6);
System.out.println("yup6");
}
if ("sl7".equals(e.getActionCommand())){
returnNum(7);
System.out.println("yup7");
}
if ("sl8".equals(e.getActionCommand())){
returnNum(8);
System.out.println("yup8");
}
if ("sl9".equals(e.getActionCommand())){
returnNum(9);
System.out.println("yup9");
}
if ("sl10".equals(e.getActionCommand())){
returnNum(10);
System.out.println("yup10");
}
if ("sl11".equals(e.getActionCommand())){
returnNum(11);
System.out.println("yup11");
}
if ("sl12".equals(e.getActionCommand())){
returnNum(12);
System.out.println("yup12");
}
}
public void main(String[] args){
MathGameMenuBar menuBar = new MathGameMenuBar();
menuBar.MenuBarCreator();
}
@Override
public void keyPressed(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
}