Java - 在jTextArea中键入时,将焦点设置为相应的jButton

时间:2012-12-12 06:05:52

标签: java swing awt jbutton jtextarea

所以我必须为作业创建一个jTextbox和一个键盘。当我输入时,我需要相应的jButton来改变颜色。

所以目前,当我运行时,焦点在于jTextbox,我可以输入,但我无法将焦点设置为按钮。

非常感谢一些帮助。谢谢

private void aButtonKeyPressed(java.awt.event.KeyEvent evt) {
    if (evt.getKeyCode() == KeyEvent.VK_A)
    {
       aButton.setBackground(Color.red);           
    } 
}

private void aButtonKeyReleased(java.awt.event.KeyEvent evt) {
    if (evt.getKeyCode() == KeyEvent.VK_A)
    {
        aButton.setBackground(Color.LIGHT_GRAY);
    }
}

private void sButtonKeyPressed(java.awt.event.KeyEvent evt) {
    if (evt.getKeyCode() == KeyEvent.VK_S)
    {
       sButton.setBackground(Color.red);           
    }
}

private void sButtonKeyReleased(java.awt.event.KeyEvent evt) {
    if (evt.getKeyCode() == KeyEvent.VK_S)
    {
       sButton.setBackground(Color.LIGHT_GRAY);           
    }
}

3 个答案:

答案 0 :(得分:2)

有一个实现KeyListener的公共类,并将该类提供给JButtons。

有一个地图来存储密钥代码和JButton

Map<Integer, JButton> keyCodeButtonMap = new HashMap<Integer, JButton>();
keyCodeButtonMap.put(KeyEvent.VK_A, aButton); // Example

将所有KeyEvent虚拟密钥与正确的按钮一起添加到地图中。

private void buttonKeyPressed(java.awt.event.KeyEvent evt) {
    keyCodeButtonMap.get(evt.getKeyCode()).setBackground(Color.RED);
    keyCodeButtonMap.get(evt.getKeyCode()).setForeground(Color.RED);
}

private void buttonKeyReleased(java.awt.event.KeyEvent evt) {
    keyCodeButtonMap.get(evt.getKeyCode()).setBackground(Color.LIGHT_GRAY);
    keyCodeButtonMap.get(evt.getKeyCode()).setForeground(Color.LIGHT_GRAY);
}

这应该是理想的工作。

答案 1 :(得分:1)

我认为您不想将焦点设置为JButton(否则您无法输入JTextArea / JTextField等),也许您希望它改变颜色并在输入某个字符时单击它?

  • 使用Swing组件时,请按照其他人的建议使用KeyBindings

这是我做的一个例子:

enter image description here

当按下时,按钮背景将变为蓝色(并且将调用按钮方法以在'。'之后自动插入 SPACE ):

enter image description here

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;

public class Test {

    final JFrame frame = new JFrame();
    final JTextField jtf = new JTextField(15);
    final JButton button = new JButton("SPACE");

    public Test() {
        initComponents();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Test();
            }
        });
    }

    private void initComponents() {

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                insertSpace();
            }
        });

        final Color defaultColor = button.getBackground();

        jtf.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_PERIOD, 0, true), "period rel");
        jtf.getActionMap().put("period rel", new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                //button.doClick(); //I dont like this as it makes JBUtton look like its being clicked where as we want a color change
                insertSpace();
                button.setBackground(defaultColor);
            }
        });

        jtf.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_PERIOD, 0), "period pressed");
        jtf.getActionMap().put("period pressed", new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                button.setBackground(Color.CYAN);
            }
        });

        frame.add(jtf, BorderLayout.NORTH);
        frame.add(button, BorderLayout.SOUTH);

        frame.pack();
        frame.setVisible(true);
    }

    private void insertSpace() {
        String s = jtf.getText();
        jtf.setText(s + " ");
    }
}

答案 2 :(得分:0)

您可能希望看到KeyBindings 但是如果你真的想要KeyListener,试试这个。

    yourJtextField.addKeyListener(new KeyAdapter() {

       @Override
       public void keyPressed(KeyEvent e){
          if( e.getKeyCode() == KeyEvent.VK_A ){
             aButton.setBackground(yourColor);
          }
       }

       @Override
       public void keyReleased(KeyEvent e){
          aButton.setBackground(yourDefaultColor);
       }

    });

yourJtextField.addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e){ if( e.getKeyCode() == KeyEvent.VK_A ){ aButton.setBackground(yourColor); } } @Override public void keyReleased(KeyEvent e){ aButton.setBackground(yourDefaultColor); } });