gpt4 book ai didi

java - 键入按钮时 JButton 不会改变颜色

转载 作者:行者123 更新时间:2023-12-01 22:47:56 25 4
gpt4 key购买 nike

我有一个很小的问题。现在我编写了创建虚拟键盘的代码。我希望按钮的颜色在键入时改变。这是我的代码:

public class ButtonColor implements KeyListener {

@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub

}

@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub

}

@Override
public void keyTyped(KeyEvent e) {
if (e.getKeyChar()=='a') {
A.setBackground(Color.red);
}

}

}

]

每当我按 A 时,什么也没有发生。当我添加这一行时:

JOptionPane.showMessageDialog(null, "A was typed");

然后输入 a,将出现该消息,单击“确定”后按钮会更改颜色。为什么会发生这种情况?我该如何解决这个问题?

enter image description here

最佳答案

可能有很多原因导致这对您不起作用,对于初学者来说,按钮可能是透明的(opaque == false)

我强烈建议不要使用KeyListener而选择Key Bindings因为 KeyListener 存在焦点问题...

例如...

下面使用键绑定(bind)API来响应给定的击键,根据它是按键还是释放事件,它将相应地设置背景颜色和不透明度状态,甚至设置按钮的按下状态。 .

Keyboard

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class KeyboardTest {

public static void main(String[] args) {
new KeyboardTest();
}

public KeyboardTest() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel {

public TestPane() {
JButton btnA = createButton("A");
JButton btnB = createButton("B");
JButton btnC = createButton("C");
JButton btnD = createButton("D");
JButton btnE = createButton("E");

add(btnA);
add(btnB);
add(btnC);
add(btnD);
add(btnE);

addKeyBinding(btnA, "A", KeyEvent.VK_A);
addKeyBinding(btnB, "B", KeyEvent.VK_B);
addKeyBinding(btnC, "C", KeyEvent.VK_C);
addKeyBinding(btnD, "D", KeyEvent.VK_D);
addKeyBinding(btnE, "E", KeyEvent.VK_E);

}

protected JButton createButton(String text) {
JButton btn = new JButton(text);
btn.setFocusable(false);
return btn;
}

protected void addKeyBinding(JButton btn, String name, int virtualKey) {
ActionMap am = getActionMap();
InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);

im.put(KeyStroke.getKeyStroke(virtualKey, 0, false), name + ".pressed");
im.put(KeyStroke.getKeyStroke(virtualKey, 0, true), name + ".released");

am.put(name + ".pressed", new KeyAction(btn, true));
am.put(name + ".released", new KeyAction(btn, false));
}

}

public class KeyAction extends AbstractAction {

private JButton btn;
private boolean highlight;

public KeyAction(JButton btn, boolean highlight) {
this.btn = btn;
this.highlight = highlight;
}

@Override
public void actionPerformed(ActionEvent e) {
if (highlight) {
btn.getModel().setPressed(true);
btn.setBackground(Color.RED);
btn.setOpaque(true);
} else {
btn.getModel().setPressed(false);
btn.setBackground(null);
btn.setOpaque(false);
}
}

}

}

已更新

如果您还使用 btn.getModel().setArmed(...); 您将得到更加“粗体”的响应,从而产生更好的视觉反馈...恕我直言

关于java - 键入按钮时 JButton 不会改变颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25073094/

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