【问题标题】:Java - while typing in a jTextArea, set focus to the corresponding jButtonJava - 在输入 jTextArea 时,将焦点设置到相应的 jButton
【发布时间】:2012-11-29 19:19:53
【问题描述】:

所以我必须为作业创建一个 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);           
    }
}

【问题讨论】:

  • 我不明白你为什么要把焦点放在 JButton 上。
  • 可能我的措辞不正确,每次按下按钮时我都需要将颜色从红色变为灰色。
  • 我明白了。所以你需要 JTextField 来输入吗?如果我错了,请纠正我。
  • 我的意思是,当你在 JTextField 上键入时,JButton 背景会改变?
  • 是的...所以就像我在这里有评论框,我输入它,想象一下如果它下面有一个“虚拟键盘”,当我输入时,相应的按钮会改变颜色 onpress 并返回到原来的颜色 onrelease

标签: java swing awt jbutton jtextarea


【解决方案1】:

有一个实现 KeyListener 的通用类并将该类提供给 JButtons。

有一个 Map 来存储 Key Code 和 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。
【解决方案2】:

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

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

这是我做的一个例子:

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

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 + " ");
    }
}

【讨论】:

    【解决方案3】:

    您可能想查看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);
           }
    
        });
    

    【讨论】:

    • 谢谢你的帮助,我也试试这个。
    • 因为在他的声明中'但我无法将焦点设置在按钮上'。他可能在特定 JButton 上执行 keyPressed 事件,而不是 JTextField。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-22
    • 1970-01-01
    相关资源
    最近更新 更多