gpt4 book ai didi

带有按键监听器的 Java swing gui 程序在 Linux 中不起作用

转载 作者:行者123 更新时间:2023-12-01 12:23:50 27 4
gpt4 key购买 nike

我用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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com