0

当按下具有相同字母/符号的键时,我试图更改按钮的背景颜色,但问题是此代码不适用于符号和空格,我做错了什么,如何解决?

这是代码:

    private String names[]={"`"," 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 "," , "," . "," / "," ^ "
        ,"                             "," < "," ! "," > "};


public void keyPressed(KeyEvent event)
    {
        int code= event.getKeyCode();
        String x;

        for(int i=0;i<names.length;i++)
        {

            s=names[i];
            x=s.trim();

            if(x.contentEquals(event.getKeyText(code)) )
            {
                backColor=but[i].getBackground();
                but[i].setBackground(Color.RED);
                break;
            }
        }
    }

    public void keyReleased(KeyEvent event)
    {
        String x;
        int code= event.getKeyCode();

        for(int i=0;i<names.length;i++)
        {
            s=names[i];
            x=s.trim();

            if(x.contentEquals(event.getKeyText(code)))
            {System.out.println("outside");
                but[i].setBackground(backColor);
                break;
            }
        }
    }

问候

4

3 回答 3

1

我会看看你的names数组并将其与结果进行比较KeyEvent#getKeyText

我的小测试程序输出了这个......

[PRESSED ] "Space"
[TYPED   ] "Unknown keyCode: 0x0"
[RELEASED] "Space"
[PRESSED ] "Enter"
[TYPED   ] "Unknown keyCode: 0x0"
[RELEASED] "Enter"

你的names数组有存储空间" Space ",这是永远不会匹配的。

您永远不应该依赖结果,KeyEvent#getKetText因为它没有考虑键盘的当前扩展状态(即如果按下 shift),这可能会改变键的状态。

例如,如果我输入 ['],我的测试程序将输出Quote,但如果我输入 [shift+'](应该产生 ["]),它仍然会显示Quote. 甚至不要让我开始使用 { 和 [ :P

如果您对显示击键文本不感兴趣,则最好依靠虚拟键值

public class PaintMyKeys {

    public static void main(String[] args) {
        new    PaintMyKeys();
    }

    public PaintMyKeys() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new PaintMyKeysPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class PaintMyKeysPane extends JPanel {

        private JLabel label;

        public PaintMyKeysPane() {

            setLayout(new BorderLayout());

            label = new JLabel("Press space to change colors", JLabel.CENTER);
            label.setOpaque(true);

            add(label);

            addKeyListener(new KeyAdapter() {

                protected int random() {
                    return (int)Math.round(Math.random() * 255);
                }

                protected Color randomColor() {

                    int r = random();
                    int g = random();
                    int b = random();
                    int a = random();

                    return new Color(r, g, b, a);

                }

                @Override
                public void keyReleased(KeyEvent e) {
                    if (e.getKeyCode() == KeyEvent.VK_SPACE) {
                        label.setBackground(randomColor());
                        label.setForeground(randomColor());

                    }
                }
            });

            setFocusable(true);
            requestFocusInWindow();        
        }
    }
}
于 2012-11-16T21:04:44.013 回答
0

作为一般规则,getKeyText()浏览器的结果并不统一,并且与实际上是文本(字母和数字)的键无关。

我认为你应该重新考虑你的使用方法getKeyCode()

有用的链接:

  • 此页面可用于测试哪个 keyCode 与某个键相关联。
  • 此页面为您提供密钥和密钥代码之间的映射。
于 2012-11-16T19:54:05.743 回答
0

据我所知,getKeyText() 方法返回单词而不是大多数符号(例如,“.”的代码产生“PERIOD”)。不要在名称数组中使用“.”等类似名称,而是使用“PERIOD”和“SPACE”。

是 getKeyText() 方法的代码,它准确显示了哪些字符将返回键的名称。

于 2012-11-16T19:57:57.027 回答