- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我用java编写了一个基本的控制板GUI。当按下箭头键时,文本将显示按下键盘上的哪个按钮,并且按钮将改变颜色。
问题是这个程序在 Windows 中运行良好,但是当我在运行名为 raspbian 的 Linux 版本的树莓派上尝试它时,这似乎不起作用。当我按下按钮时,程序不执行任何操作。
package finalRobotControl;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.xml.bind.Marshaller.Listener;
public class Main implements KeyListener{
public static JButton buttonLeft;
public static JButton buttonRight;
public static JButton buttonUp;
public static JButton buttonDown;
public static JLabel stage;
public static void main(String [] args){
JFrame window = new JFrame("RobotController");
window.setVisible(true);
window.setSize(200, 200);
window.setPreferredSize(new Dimension(400, 200));
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(null);
//add panel to window
window.add(panel);
stage = new JLabel("");
stage.setBounds(85, 65, 50, 35);
panel.add(stage);
buttonLeft = new JButton("←");
buttonLeft.setBounds(10, 65, 50, 35);
//buttonLeft.setBackground(Color.BLUE);
panel.add(buttonLeft);
buttonRight = new JButton("→");
buttonRight.setBounds(130, 65, 50, 35);
panel.add(buttonRight);
buttonUp = new JButton("↑");
buttonUp.setBounds(70, 25, 50, 35);
panel.add(buttonUp);
buttonDown = new JButton("↓");
buttonDown.setBounds(70, 105, 50, 35);
panel.add(buttonDown);
window.addKeyListener(new Main());
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if(e.getKeyCode() == KeyEvent.VK_LEFT){
buttonLeft.setBackground(Color.WHITE);
System.out.println("Left");
stage.setText("Left");
}
if(e.getKeyCode() == KeyEvent.VK_RIGHT){
buttonRight.setBackground(Color.WHITE);
System.out.println("Right");
stage.setText("Right");
}
if(e.getKeyCode() == KeyEvent.VK_UP){
buttonUp.setBackground(Color.WHITE);
System.out.println("Up");
stage.setText("Up");
}
if(e.getKeyCode() == KeyEvent.VK_DOWN){
buttonDown.setBackground(Color.WHITE);
System.out.println("Down");
stage.setText("Down");
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
JButton lol = new JButton();
if(e.getKeyCode() == KeyEvent.VK_LEFT){
buttonLeft.setBackground(lol.getBackground());
stage.setText("");
}
if(e.getKeyCode() == KeyEvent.VK_RIGHT){
buttonRight.setBackground(lol.getBackground());
stage.setText("");
}
if(e.getKeyCode() == KeyEvent.VK_UP){
buttonUp.setBackground(lol.getBackground());
stage.setText("");
}
if(e.getKeyCode() == KeyEvent.VK_DOWN){
buttonDown.setBackground(lol.getBackground());
stage.setText("");
}
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
}
最佳答案
KeyListener
众所周知,它对何时生成 KeyEvent
非常挑剔。仅当注册到的组件可聚焦且具有焦点时,KeyListener
才会生成 KeyEvent
。
将 KeyListener 直接添加到窗口会使事情变得更加困难,因为窗口和使用之间可以存在任意数量的组件,这可能会窃取焦点。
相反,您应该使用 Key Bindings API ,它允许您控制生成关键事件所需的焦点级别
关于带有按键监听器的 Java swing gui 程序在 Linux 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26494615/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!