我的项目是关于创建虚拟键盘,当用户键入键盘上的相应按钮时,键盘上的按钮必须更改颜色。 即如果我输入'e',我的虚拟键盘上的字母'e'必须改变它的颜色。
为了绘制我的键盘,我使用了一个2D JButton数组和一个2D字符串数组来命名按钮并分配它们各自的标签,然后我在每个按钮上添加了一个KeyListener接口,并在该接口中指示程序进行更改按钮的颜色。编译它对我来说很好,但实际的颜色变化不起作用。有人可以帮忙吗? 这是代码
private JButton spButton, oneButton, twoButton, threeButton, fourButton, fiveButton, sixButton, sevenButton, eightButton,
nineButton, zeroButton, dashButton, equalsButton, bspButton, tabButton, qButton, wButton, eButton,
rButton, tButton, yButton, uButton, iButton, oButton, pButton, rBracketButton, lBracketButton, slashButton,
capsButton, aButton, sButton, dButton, fButton, gButton, hButton, jButton, kButton, lButton, scButton,
sqButton, enterButton, lShiftButton, zButton, xButton, cButton, vButton, bButton, nButton, mButton,
commaButton, periodButton, bSlashButton, rShiftButton, upButton, spaceButton, leftButton, downButton,
rightButton;
private JButton [][] buttonArray = {{spButton, oneButton, twoButton, threeButton, fourButton, fiveButton,
sixButton,sevenButton, eightButton, nineButton, zeroButton, dashButton, equalsButton, bspButton},
{tabButton, qButton, wButton, eButton, rButton, tButton, yButton, uButton,
iButton, oButton, pButton, rBracketButton, lBracketButton, slashButton},
{capsButton, aButton, sButton, dButton, fButton, gButton, hButton,
jButton, kButton, lButton, scButton, sqButton, enterButton},
{lShiftButton, zButton, xButton, cButton, vButton, bButton, nButton, mButton,
commaButton, periodButton, bSlashButton, rShiftButton, upButton},
{spaceButton, leftButton, downButton, rightButton}};
private char pressed;
public constructor(){
addKeyListener(new KeyEventsHandler());
}
private class KeyEventsHandler implements KeyListener {
@Override
public void keyTyped(KeyEvent e) {
pressed = e.getKeyChar();
for (int row = 0; row < keyBoard.length; row ++){
for (int column = 0; column < keyBoard[row].length; column ++){
if (pressed==buttonArray[row][column].getName().toLowerCase().charAt(0))
buttonArray[row][column].setForeground(Color.RED);
keyboardPanel.repaint();
}
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
}
}
先谢谢你们
答案 0 :(得分:3)
我没有注意,但正如@mKobel通知的那样:因为你正在使用来自控制组件家族的JButton
组件的数组执行操作,因此我们应该使用ActionListener
函数注册addActionListener(ActionListener)
或使用更高级别实现的Action
。
对于其他容器组件,Try using Key Binding而不是KeyListener
S:键侦听器可以作为键盘输入的低级接口,但是对于响应单个键,键绑定更多适当的,往往会导致更容易维护的代码。如果在组件没有焦点时激活键绑定,则键监听器也很困难。
尝试使用e.getSource()
来获取事件的源组件,而不是使用for
循环来检测此类事件,
修改强>
回复以下@predi的评论:
e.getSource()将在OP中返回键盘焦点的组件 案例,这不是很有帮助。他需要提供一些 映射,他做了
相反,使用长句子,我决定制作一个示例代码,以便可以复制和粘贴它,以便立即看到键绑定和event.getSource()
的强大功能。还有更多其他理想的选择来做事。
GridLayout layout = new GridLayout(0, 4);
jPanel1.setLayout(layout);
Action action = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
System.err.println("Event generated for source: "+((JButton)e.getSource()).getName());
}
};
for(int i=0 ;i<4; i++)
for(int j=0; j<4; j++)
{
String key = ""+(char)('A' + i * 4 + j);
JButton button = new JButton(key);
button.setName(key);
button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key), "doAction");
button.getActionMap().put("doAction", action);
jPanel1.add(button);
}
答案 1 :(得分:1)
以下是您可以使用Key Bindings执行操作的示例,以支持我对您的问题的评论。虽然这有点不确定。不确定使用我在示例中使用的地图是个好主意......但是你总是可以实现每键击动作代码以使其类型安全。它应该让你开始。
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.util.HashMap;
import java.util.Map;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class VirtualKeyboard extends JFrame {
private final JButton jbQ = new JButton("Q");
private final JButton jbW = new JButton("W");
private final JButton jbE = new JButton("E");
private final JButton jbR = new JButton("R");
private final JButton jbT = new JButton("T");
private final JButton jbZ = new JButton("Z");
private final Map<String, JButton> keymap = new HashMap<>();
public VirtualKeyboard() {
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(jbQ);
add(jbW);
add(jbE);
add(jbR);
add(jbT);
add(jbZ);
pack();
setLocationRelativeTo(null);
keymap.put("q", jbQ);
keymap.put("w", jbW);
keymap.put("e", jbE);
keymap.put("r", jbR);
keymap.put("t", jbT);
keymap.put("z", jbZ);
InputMap inputMap
= getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
ActionMap actionMap = getRootPane().getActionMap();
actionMap.put("key-pressed-action", new KeyPressedAction());
actionMap.put("key-released-action", new KeyReleasedAction());
inputMap.put(KeyStroke.getKeyStroke("pressed Q"), "key-pressed-action");
inputMap.put(KeyStroke.getKeyStroke("released Q"), "key-released-action");
inputMap.put(KeyStroke.getKeyStroke("pressed W"), "key-pressed-action");
inputMap.put(KeyStroke.getKeyStroke("released W"), "key-released-action");
inputMap.put(KeyStroke.getKeyStroke("pressed E"), "key-pressed-action");
inputMap.put(KeyStroke.getKeyStroke("released E"), "key-released-action");
inputMap.put(KeyStroke.getKeyStroke("pressed R"), "key-pressed-action");
inputMap.put(KeyStroke.getKeyStroke("released R"), "key-released-action");
inputMap.put(KeyStroke.getKeyStroke("pressed T"), "key-pressed-action");
inputMap.put(KeyStroke.getKeyStroke("released T"), "key-released-action");
inputMap.put(KeyStroke.getKeyStroke("pressed Z"), "key-pressed-action");
inputMap.put(KeyStroke.getKeyStroke("released Z"), "key-released-action");
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new VirtualKeyboard().setVisible(true);
}
});
}
private class KeyPressedAction extends AbstractAction {
@Override
public void actionPerformed(ActionEvent e) {
JButton button = keymap.get(e.getActionCommand());
if (button != null) {
button.setForeground(Color.RED);
}
}
}
private class KeyReleasedAction extends AbstractAction {
@Override
public void actionPerformed(ActionEvent e) {
JButton button = keymap.get(e.getActionCommand());
if (button != null) {
button.setForeground(UIManager.getColor("Button.foreground"));
}
}
}
}