-1
所以我在尝试更改按键的背景颜色时遇到了问题,并且我不完全确定如何去解决它。 它可以作为一个键盘,我唯一的问题是要改变背景颜色的方法,当任何按键输入如何动态更改JButtom背景?
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.AbstractAction;
public abstract class OnScreenKeyboard extends JFrame implements KeyListener
{
String firstRow[] = {"~","1","2","3","4","5","6","7","8","9","0","-","+","Back\nSpace"};
String secondRow[] = {"Tab","Q","W","E","R","T","Y","U","I","O","P","[","]","|"};
String thirdRow[] = {"Caps\nLock","A","S","D","F","G","H","J","K","L",";","'","Enter"};
String fourthRow[] = {"Shift","Z","X","C","V","B","N","M",",",".","?","Space"};
JButton first[] = new JButton[14];
JButton second[] = new JButton[14];
JButton third[] = new JButton[13];
JButton fourth[] = new JButton[12];
Panel keys = new Panel();
Panel text = new Panel();
TextArea textArea = new TextArea();
String strText = "";
private JLabel label1;
private JLabel label2;
private JTextField textField;
public OnScreenKeyboard()
{
super("Typing Application");
label1 = new JLabel("Type some text using your keyboard. The keys you press will be "
+ "highlighed and the text will be displayed");
add(label1);
label2 = new JLabel("Note: clicking the buttons with your mouse will not perform any action");
add(label2);
textField = new JTextField(80);
textField.setEditable(true);
TextFieldHandler handler = new TextFieldHandler();
this.setLayout(new BorderLayout(1,1));
keys.setLayout(new GridLayout(4,14));
text.setLayout(new BorderLayout(1,1));
text.add(textArea);
for(int i=0; i<14; i++)
{
first[i] = new JButton(firstRow[i]);
first[i].setBackground(Color.white);
keys.add(first[i]);
first[i].addKeyListener(this);
}
for(int i=0; i<14; i++)
{
second[i] = new JButton(secondRow[i]);
second[i].setBackground(Color.white);
keys.add(second[i]);
second[i].addKeyListener(this);
}
for(int i=0; i<13; i++)
{
third[i] = new JButton(thirdRow[i]);
third[i].setBackground(Color.white);
keys.add(third[i]);
third[i].addKeyListener(this);
}
for(int i=0; i<12; i++)
{
fourth[i] = new JButton(fourthRow[i]);
fourth[i].setBackground(Color.white);
keys.add(fourth[i]);
fourth[i].addKeyListener(this);
}
add(text, BorderLayout.NORTH);
add(keys,BorderLayout.CENTER);
}
private class TextAreaHandler implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String string = ""; // declare string to display
if (event.getSource() == textField)
string = String.format("%s",
event.getActionCommand());
}
}
public void actionPerformed(ActionEvent event) {
if (event.getSource() instanceof JButton) {
System.out.println((((JButton) event.getSource()).getActionCommand()));
((JButton) event.getSource()).setBackground(Color.BLUE);
((JButton) event.getSource()).setContentAreaFilled(false);
((JButton) event.getSource()).setOpaque(true);
}
}
@Override
public void keyTyped(KeyEvent event)
{
int keyCode = event.getKeyCode();
strText = String.format("%s", event.getKeyCode());
}
private class TextFieldHandler implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String string = ""; // declare string to display
// user pressed Enter in JTextField textField1
if (event.getSource() == textField)
string = String.format("%s", event.getActionCommand());
}
}
}
欢迎来到SO。 TL; DR。这是最短的代码来证明问题吗?请发布一个[MCVE] – c0der
我对java或编码不是很有经验,这是我可能想到的最短代码。如果有更短的路,请让我知道!我非常渴望从错误中学习,并学习不同的编码方式来高效地编写代码。 – ThePlum
请参阅:http://stackoverflow.com/help/someone-answers – c0der