我如何改变按钮颜色(突出显示它)后,我按下我的键盘键?就像标签说:“键入一些文本使用您的键盘。您按下的键将突出显示和文本将显示。”
这是我的代码,我应该在代码中添加什么,这样当我按下键盘上的键时,JFrame上的按钮将更改颜色?例如,我在键盘上按下A,JFrame上的按钮A将从灰色更改为红色(例如),在我释放它后,颜色(红色)将更改回我设置的默认颜色(灰色
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
/**
*
* @author frint6
*/
public class GUITyping extends JFrame implements KeyListener {
private final JLabel lFp = new JLabel("Type some text using your keyboard. The keys you press will be highlighted and the text will be displayed.");
private final JLabel lSp = new JLabel("Note: Clicking the buttons with your mouse will not perform any action.");
private final JTextArea taL = new JTextArea();
private final String firstRow[] = {"~","1","2","3","4","5","6","7","8","9","0","-","+","Backspace"};
private final String secondRow[] = {"Tab","Q","W","E","R","T","Y","U","I","O","P","[","]","\\"};
private final String thirdRow[] = {"Caps","A","S","D","F","G","H","J","K","L",":","\"","Enter"};
private final String fourthRow[] = {"Shift","Z","X","C","V","B","N","M",",",".","?","^"};
private final String fifthRow[] = {" ","<","\\/",">"};
private JButton first[];
private JButton second[];
private JButton third[];
private JButton fourth[];
private JButton fifth[];
private final Container cont = getContentPane();
GUITyping(){
super("Typing Application");
initWidget();
}
private void initWidget(){
cont.setLayout(null);
lFp.setBounds(10, 0, 600, 30);
lSp.setBounds(10, 20, 400, 30);
taL.setBounds(10,50,765,230);
taL.setBorder(BorderFactory.createLoweredBevelBorder());
cont.add(lFp);
cont.add(lSp);
cont.add(taL);
first = new JButton[firstRow.length];
for(int i = 0; i < firstRow.length; ++i){
JButton a = new JButton(firstRow[i]);
first[i] = a;
first[i].addKeyListener(this);
first[i].setBorder(BorderFactory.createRaisedBevelBorder());
first[i].setBackground(Color.LIGHT_GRAY);
cont.add(first[i]);
}
second = new JButton[secondRow.length];
for(int i = 0; i < secondRow.length; ++i){
JButton b = new JButton(secondRow[i]);
second[i] = b;
second[i].addKeyListener(this);
second[i].setBorder(BorderFactory.createRaisedBevelBorder());
second[i].setBackground(Color.LIGHT_GRAY);
cont.add(second[i]);
}
third = new JButton[thirdRow.length];
for(int i = 0; i < thirdRow.length; ++i){
JButton c = new JButton(thirdRow[i]);
third[i] = c;
third[i].addKeyListener(this);
third[i].setBorder(BorderFactory.createRaisedBevelBorder());
third[i].setBackground(Color.LIGHT_GRAY);
cont.add(third[i]);
}
fourth = new JButton[fourthRow.length];
for(int i = 0; i < fourthRow.length; ++i){
JButton d = new JButton(fourthRow[i]);
fourth[i] = d;
fourth[i].addKeyListener(this);
fourth[i].setBorder(BorderFactory.createRaisedBevelBorder());
fourth[i].setBackground(Color.LIGHT_GRAY);
cont.add(fourth[i]);
}
fifth = new JButton[fifthRow.length];
for(int i = 0; i < fifthRow.length; ++i){
JButton e = new JButton(fifthRow[i]);
fifth[i] = e;
fifth[i].addKeyListener(this);
fifth[i].setBorder(BorderFactory.createRaisedBevelBorder());
fifth[i].setBackground(Color.LIGHT_GRAY);
cont.add(fifth[i]);
}
first[0].setBounds(10, 300, 45, 45);
first[1].setBounds(60, 300, 45, 45);
first[2].setBounds(110, 300, 45, 45);
first[3].setBounds(160, 300, 45, 45);
first[4].setBounds(210, 300, 45, 45);
first[5].setBounds(260, 300, 45, 45);
first[6].setBounds(310, 300, 45, 45);
first[7].setBounds(360, 300, 45, 45);
first[8].setBounds(410, 300, 45, 45);
first[9].setBounds(460, 300, 45, 45);
first[10].setBounds(510, 300, 45, 45);
first[11].setBounds(560, 300, 45, 45);
first[12].setBounds(610, 300, 45, 45);
first[13].setBounds(660, 300, 115, 45);
second[0].setBounds(10, 350, 75, 45);
second[1].setBounds(90, 350, 45, 45);
second[2].setBounds(140, 350, 46, 45);
second[3].setBounds(190, 350, 45, 45);
second[4].setBounds(240, 350, 45, 45);
second[5].setBounds(290, 350, 45, 45);
second[6].setBounds(340, 350, 45, 45);
second[7].setBounds(390, 350, 45, 45);
second[8].setBounds(440, 350, 45, 45);
second[9].setBounds(490, 350, 45, 45);
second[10].setBounds(540, 350, 45, 45);
second[11].setBounds(590, 350, 45, 45);
second[12].setBounds(640, 350, 45, 45);
second[13].setBounds(690, 350, 85, 45);
third[0].setBounds(10, 400, 75, 45);
third[1].setBounds(90, 400, 45, 45);
third[2].setBounds(140, 400, 46, 45);
third[3].setBounds(190, 400, 45, 45);
third[4].setBounds(240, 400, 45, 45);
third[5].setBounds(290, 400, 45, 45);
third[6].setBounds(340, 400, 45, 45);
third[7].setBounds(390, 400, 45, 45);
third[8].setBounds(440, 400, 45, 45);
third[9].setBounds(490, 400, 45, 45);
third[10].setBounds(540, 400, 45, 45);
third[11].setBounds(590, 400, 45, 45);
third[12].setBounds(640, 400, 135, 45);
fourth[0].setBounds(10, 450, 105, 45);
fourth[1].setBounds(120, 450, 45, 45);
fourth[2].setBounds(170, 450, 45, 45);
fourth[3].setBounds(220, 450, 45, 45);
fourth[4].setBounds(270, 450, 45, 45);
fourth[5].setBounds(320, 450, 45, 45);
fourth[6].setBounds(370, 450, 45, 45);
fourth[7].setBounds(420, 450, 45, 45);
fourth[8].setBounds(470, 450, 45, 45);
fourth[9].setBounds(520, 450, 45, 45);
fourth[10].setBounds(570, 450, 45, 45);
fourth[11].setBounds(680, 450, 45, 45);
fifth[0].setBounds(215, 500, 305, 45);
fifth[1].setBounds(630, 500, 45, 45);
fifth[2].setBounds(680, 500, 45, 45);
fifth[3].setBounds(730, 500, 45, 45);
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
private class Actions implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
}
}
}
2条答案
按热度按时间flmtquvp1#
直截了当的做法是增加以下四项内容:
initWidget
方法中,将KeyListener
添加到JTextArea
(而不是JButton
),以便在键入内容时实际调用keyPressed
和keyReleased
方法。Map
,用于存储从键控代码到按钮的Map。initWidget
方法中,当添加所有按钮时,将存储来自键码的Map(以常量KeyEvent.VK_*
的形式提供)添加到上面提到的map中的按钮。我还建议通过将重复的部分替换成它自己的方法来使这个方法更容易阅读。这样就不需要字符串数组了(firstRow
、secondRow
、...)和按钮数组(first
、second
、...)。keyPressed
和keyReleased
方法中,通过查找键控代码的相应按钮并突出显示该按钮来作出React。3pvhb19x2#
Normally you click on the button. When the button is pressed the background of the button is painted in the selected state. When you release the mouse button the Action is invoked, and the button is repainted in its normal state.
For just using the keyboard you will need to manually set the state of the
ButtonModel
so the button can be painted correctly. This is done by addingKey Bindings
to the button to handle the "pressed/released" state:If you really want to control the background color, then you can just use:
and
in the pressed/released actions respectively and remove the "model" logic.