我在試圖構建屏幕鍵盤,我需要一點幫助。 這個想法:來自外部源的文本複製(複製和粘貼)將用於學習觸摸打字。
當您鍵入正確的字符,字符被突出顯示在JTextArea中,以綠色,而如果你不正確的字符按鈕的顏色會變爲綠色, ,所以按鈕將顏色更改爲紅色KeyListener和keyPressed
當我按下我的電腦鍵盤時,屏幕鍵盤上的按鈕不會將其背景更改爲綠色。
public class Box extends JFrame {
JFrame frame = new JFrame("Box Value & Area");
GridLayout Form_Grig = new GridLayout(3,0);
GridLayout Panel_Grid = new GridLayout(1,18);
JPanel ans = new JPanel(new GridLayout(1,2));
final JLabel label =new JLabel("Points : ");
final JLabel label2 = new JLabel("answer ");
final JLabel label3 = new JLabel("length : ");
final JLabel label4 = new JLabel("Volume");
public int index=0;
public int counter=0;
public int press_count=-1;
JPanel panel = new JPanel();
private JPanel parent = new JPanel(new GridLayout(0, 1));
final JTextArea text1 = new JTextArea();
private JButton[][] button;
private JPanel[] Panel = new JPanel[6];
public JButton start = new JButton("Start game");
private static final String[][] key = {
{"Esc", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12", "Num", "Insert", "Delete", "Pause"},
{"~\n`", "1", "2", "3", "4", "5","6", "7", "8", "9", "0", "-", "=", "Backspace "},
{" Tab ", "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "{", "}", "|"},
{"Caps Lock","A", "S", "D", "F", "G", "H", "J", "K", "L", ";", "'", " Enter "},
{" Shift ", "Z", "X", "C", "V", "B","N", "M", "<", ">", "?", "Shift"},
{"Ctrl", "Fn", "Alt", " Space ", "Alt","Ctrl"}};
public Box(){
frame.setLayout(Form_Grig);
frame.add(text1);
ans.add(label);
ans.add(label2);
button = new JButton[20][20];
for (int row = 0; row < key.length; row++) {
Panel[row] = new JPanel();
for (int column = 0; column < key[row].length; column++) {
button[row][column] = new JButton(key[row][column]);
button[row][column].putClientProperty("column", column);
button[row][column].putClientProperty("row", row);
button[row][column].putClientProperty("key", key[row][column]);
button[row][column].addActionListener(new MyActionListener());
Panel[row].add(button[row][column]);
}
parent.add(Panel[row]);
}
frame.add(ans);
frame.add(parent);
frame.pack();
frame.setResizable(false);
frame.setVisible(true);
Font font = new Font("Verdana", Font.BOLD, 56);
label2.setFont(font);
label2.setForeground(Color.gray);
label.setFont(font);
label.setForeground(Color.gray);
}
public class MyActionListener implements ActionListener {
String te = text1.getText();
@Override
public void actionPerformed(ActionEvent e) {
final JButton btn =(JButton) e.getSource();
text1.addKeyListener(new KeyListener(){
@Override
public void keyPressed(KeyEvent e) {
if (!text1.getText().equals("")){
text1.setEditable(false);
}
btn.setBackground(Color.green);
System.out.println("this :"+ btn.getName());
}
@Override
public void keyReleased(KeyEvent e) { //dsd
}
@Override
public void keyTyped(KeyEvent e) {//sdsd
// TODO Auto-generated method stub
}
});
String te = text1.getText();
String Size = null;
if(press_count >= te.length() ){
JOptionPane.showMessageDialog(null, "from "+te.length() +" characters you guess "+index);
}
press_count++;
Size = Integer.toString(te.length());//adad
if (index==0)
counter=te.length();
label2.setText(Size); //setText
String bbb = (String) btn.getClientProperty("key") ;
if(bbb.equals(String.valueOf(te.charAt(index)))){
btn.setBackground(Color.green);
label2.setText(Integer.toString(counter));
}
else{
btn.setBackground(Color.red);//setting background
counter--;
label2.setText(Integer.toString(counter));
}
index++;//
}
}
爲什麼當我按下屏幕鍵盤不改變它的背景色在pc上的按鈕,按鈕?
爲什麼當我按下pc按鈕時,屏幕鍵盤上的按鈕不會改變其背景顏色? – David
你究竟想通過顏色變化來達到什麼目的?我認爲你會以錯誤的方式去做這件事。 –
這個想法:來自外部源的文本複製(複製和粘貼)將用於學習觸摸打字。 當您輸入正確的字符時,字符在JTextArea中突出顯示爲綠色,並且如果鍵入不正確的字符,則按鈕顏色將更改爲綠色,因此按鈕會將其顏色更改爲紅色。 – David