KeyListener和keyPressed

时间:2014-04-14 10:51:35

标签: java swing user-interface

我正在尝试构建屏幕键盘,我需要一些帮助。 想法:从外部源复制(复制和粘贴)文本将用于学习触摸输入 当您键入正确的字符时,字符在JTextArea中突出显示为绿色,按钮颜色将更改为绿色, 如果键入不正确的字符,则按钮会将其颜色更改为红色

当我按下我的电脑键盘时,屏幕键盘上的按钮不会将其背景变为绿色。

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++;//
    }

}
  

为什么当我按下电脑按钮时,屏幕键盘上的按钮不会改变其背景颜色?

1 个答案:

答案 0 :(得分:1)

  

“为什么当我按下电脑按钮时,屏幕键盘上的按钮不会改变其背景颜色”

他们确实改变了,但看看你的逻辑:

只有在文本区域按下某个键时才会改变颜色。这意味着您需要将注意力集中回文本区域并输入内容。

您还会在每次按下按钮时向文本区域添加多个KeyListener。我不认为这是你想要做的。

实际上。您不应该使用KeyListener )来处理文本组件。应通过DocumentListener收听文本组件的基础文档的更改。 DocumentListener只应添加一次ActionListener之外的文字区域。原因是一个组件可能有一个以上的监听器注册,即使它是同一个监听器(就像你正在做的那样)。发生的事情是每个侦听器回调都被放入队列中。因此,如果您按下10个按钮,则文本区域将有10个侦听器回调,每次按下一个键,回调将被调用10次。

您应该在How To Use DocumentListner

上阅读更多内容

另外,对于按钮将颜色更改为按钮,您可能需要查看this question