谁能告诉我 win10 +java1.8+use键盘 swing的键盘监听事件无效
下面是我的代码
@SuppressWarnings("static-access") public static void main(String[] args) { JFrame jFrame = new JFrame("计算机"); Container c = jFrame.getContentPane(); JPanel jp2 = new JPanel(); JTextArea jtf = new JTextArea(10, 40); jtf.setFocusable(false);//禁止文本框被选中 JScrollPane jScrollPane = new JScrollPane(jtf); jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); jp2.add(jScrollPane); JPanel jPanel = new JPanel(); c.setLayout(new GridLayout(4, 1)); jPanel.setLayout(new GridLayout(4, 3)); for (int i = 0; i < 10; i++) { JButton jb = new JButton(9 - i + ""); jb.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jtf.append(jb.getText()); } }); jPanel.add(jb); } JButton jb3 = new JButton("←/退格"); jb3.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String newStr = jtf.getText().substring(0, jtf.getText().length() - 1); jtf.setText(newStr); } }); jPanel.add(jb3); JPanel jPanel1 = new JPanel(); jPanel1.setLayout(new GridLayout(2, 3)); String[] ss = { "+", "-", "*", "/" }; for (String string : ss) { JButton jb = new JButton(string); jb.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jtf.append(jb.getText()); } }); jPanel1.add(jb); } JButton jb = new JButton("="); jb.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String te = jtf.getText().replaceAll("\r|\n", ""); if (null == te || te.matches("^[+,[-],*,/].*") || te.replaceAll("\r|\n", "").length() == 0 || te.matches(".*[+,[-],*,/]$")) { JOptionPane.showMessageDialog(jFrame, "输入有误,请检查"); return; } String s = te; if (te.contains("=")) { te.lastIndexOf("="); s = te.substring(te.lastIndexOf("=") + 1, te.length()); } Integer count = caclu(s); jtf.append(jb.getText() + "\r\n"); jtf.append(count.toString()); } }); jPanel1.add(jb); JButton jb1 = new JButton("C/清空"); jb1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jtf.setText(""); } }); jPanel1.add(jb1); c.add(jp2); c.add(jPanel); c.add(jPanel1); keyLi(jFrame, jtf);//调用监听方法 jFrame.setDefaultCloseOperation(jFrame.EXIT_ON_CLOSE); jFrame.setVisible(true); jFrame.setLocation(40, 40); jFrame.setSize(500, 500); jFrame.pack(); } //监听事件 public static void keyLi(JFrame jFrame, JTextArea jtf) { jFrame.addKeyListener(new KeyListener() {//监听事件加到整个jframe上面 @Override public void keyTyped(KeyEvent e) { // TODO Auto-generated method stub } @Override public void keyReleased(KeyEvent e) { // TODO Auto-generated method stub } @Override public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_0) { jtf.append("0"); } if (e.getKeyCode() == KeyEvent.VK_1) { jtf.append("1"); } if (e.getKeyCode() == KeyEvent.VK_2) { jtf.append("2"); } if (e.getKeyCode() == KeyEvent.VK_3) { jtf.append("3"); } if (e.getKeyCode() == KeyEvent.VK_4) { jtf.append("4"); } if (e.getKeyCode() == KeyEvent.VK_5) { jtf.append("5"); } if (e.getKeyCode() == KeyEvent.VK_6) { jtf.append("6"); } if (e.getKeyCode() == KeyEvent.VK_7) { jtf.append("7"); } if (e.getKeyCode() == KeyEvent.VK_8) { jtf.append("8"); } if (e.getKeyCode() == KeyEvent.VK_9) { jtf.append("9"); } if (e.getKeyCode() == KeyEvent.VK_MULTIPLY) { jtf.append("*"); } if (e.getKeyCode() == KeyEvent.VK_MINUS) { jtf.append("-"); } if (e.getKeyCode() == KeyEvent.VK_PLUS) { jtf.append("+"); } if (e.getKeyCode() == KeyEvent.VK_DIVIDE) { jtf.append("/"); } if (e.getKeyCode() == KeyEvent.VK_ENTER) { String te = jtf.getText().replaceAll("\r|\n", ""); if (null == te || te.matches("^[+,[-],*,/].*") || te.replaceAll("\r|\n", "").length() == 0 || te.matches(".*[+,[-],*,/]$")) { JOptionPane.showMessageDialog(jFrame, "输入有误,请检查"); return; } String s = te; if (te.contains("=")) { te.lastIndexOf("="); s = te.substring(te.lastIndexOf("=") + 1, te.length()); } Integer count = caclu(s); jtf.append("=\r\n"); jtf.append(count.toString()); } } }); }
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。