开发者社区> 问答> 正文

java swing键盘监听事件无效 : 配置报错

谁能告诉我     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());
				}

			}
		});
	}

展开
收起
kun坤 2020-06-03 16:14:42 1144 0
1 条回答
写回答
取消 提交回答
  • keyPressed 事件只能在当前获得焦点的控件中触发,在你的这个程序中焦点始终位于 JButton 上,其他控件无法获取焦点,因此你应当将  keyPressed 事件添加到 JButton 上。######回复 @太黑的忧郁情怀 : 不行,事件只能在当前获得焦点的控件中触发。######回复 @struct : 我直接吧事件加到了最外层的jframe上 这样也不行?

    2020-06-04 10:55:11
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载