簡體   English   中英

如何使用 KeyListener 庫更改虛擬鍵盤 Java 上的按鈕顏色?

[英]How can i change the button colour on Virtual Keyboard Java using KeyListener library?

按下鍵盤鍵后如何更改按鈕顏色(以突出顯示它)? 就像 label 所說的“使用鍵盤鍵入一些文本。您按下的鍵將突出顯示並顯示文本。”

這是我的代碼,我應該在代碼中添加什么,以便當我按下鍵盤上的鍵時,JFrame 上的按鈕會改變顏色? 謝謝,例如,在我的鍵盤上我按下 A,然后在 JFrame 按鈕 A 上將顏色從灰色變為紅色(示例),在我釋放它后,顏色(紅色)變回我設置的默認顏色(灰色)

import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;

/**
 *
 * @author frint6
 */
public class GUITyping extends JFrame implements KeyListener {
    private final JLabel lFp = new JLabel("Type some text using your keyboard. The keys you press will be highlighted and the text will be displayed.");
    private final JLabel lSp = new JLabel("Note: Clicking the buttons with your mouse will not perform any action.");
    private final JTextArea taL = new JTextArea();
    private final String firstRow[] = {"~","1","2","3","4","5","6","7","8","9","0","-","+","Backspace"};
    private final String secondRow[] = {"Tab","Q","W","E","R","T","Y","U","I","O","P","[","]","\\"};
    private final String thirdRow[] = {"Caps","A","S","D","F","G","H","J","K","L",":","\"","Enter"};
    private final String fourthRow[] = {"Shift","Z","X","C","V","B","N","M",",",".","?","^"};
    private final String fifthRow[] = {"       ","<","\\/",">"};
    private JButton first[];
    private JButton second[];
    private JButton third[];
    private JButton fourth[];
    private JButton fifth[];
    private final Container cont = getContentPane();
    
    GUITyping(){
        super("Typing Application");
        initWidget();
    }
    
    private void initWidget(){
        cont.setLayout(null);
        lFp.setBounds(10, 0, 600, 30);
        lSp.setBounds(10, 20, 400, 30);
        taL.setBounds(10,50,765,230);
        taL.setBorder(BorderFactory.createLoweredBevelBorder());
        cont.add(lFp);
        cont.add(lSp);
        cont.add(taL);
        
        first = new JButton[firstRow.length];
        for(int i = 0; i < firstRow.length; ++i){
            JButton a = new JButton(firstRow[i]);
            first[i] = a;
            first[i].addKeyListener(this);
            first[i].setBorder(BorderFactory.createRaisedBevelBorder());
            first[i].setBackground(Color.LIGHT_GRAY);
            cont.add(first[i]);
        }
        
        second = new JButton[secondRow.length];
        for(int i = 0; i < secondRow.length; ++i){
            JButton b = new JButton(secondRow[i]);
            second[i] = b;
            second[i].addKeyListener(this);
            second[i].setBorder(BorderFactory.createRaisedBevelBorder());
            second[i].setBackground(Color.LIGHT_GRAY);
            cont.add(second[i]);
        }
        
        third = new JButton[thirdRow.length];
        for(int i = 0; i < thirdRow.length; ++i){
            JButton c = new JButton(thirdRow[i]);
            third[i] = c;
            third[i].addKeyListener(this);
            third[i].setBorder(BorderFactory.createRaisedBevelBorder());
            third[i].setBackground(Color.LIGHT_GRAY);
            cont.add(third[i]);
        }
        
        fourth = new JButton[fourthRow.length];
        for(int i = 0; i < fourthRow.length; ++i){
            JButton d = new JButton(fourthRow[i]);
            fourth[i] = d;
            fourth[i].addKeyListener(this);
            fourth[i].setBorder(BorderFactory.createRaisedBevelBorder());
            fourth[i].setBackground(Color.LIGHT_GRAY);
            cont.add(fourth[i]);
        }
        
        fifth = new JButton[fifthRow.length];
        for(int i = 0; i < fifthRow.length; ++i){
            JButton e = new JButton(fifthRow[i]);
            fifth[i] = e;
            fifth[i].addKeyListener(this);
            fifth[i].setBorder(BorderFactory.createRaisedBevelBorder());
            fifth[i].setBackground(Color.LIGHT_GRAY);
            cont.add(fifth[i]);
        }
        
        first[0].setBounds(10, 300, 45, 45);
        first[1].setBounds(60, 300, 45, 45);
        first[2].setBounds(110, 300, 45, 45);
        first[3].setBounds(160, 300, 45, 45);
        first[4].setBounds(210, 300, 45, 45);
        first[5].setBounds(260, 300, 45, 45);
        first[6].setBounds(310, 300, 45, 45);
        first[7].setBounds(360, 300, 45, 45);
        first[8].setBounds(410, 300, 45, 45);
        first[9].setBounds(460, 300, 45, 45);
        first[10].setBounds(510, 300, 45, 45);
        first[11].setBounds(560, 300, 45, 45);
        first[12].setBounds(610, 300, 45, 45);
        first[13].setBounds(660, 300, 115, 45);
        
        second[0].setBounds(10, 350, 75, 45);
        second[1].setBounds(90, 350, 45, 45);
        second[2].setBounds(140, 350, 46, 45);
        second[3].setBounds(190, 350, 45, 45);
        second[4].setBounds(240, 350, 45, 45);
        second[5].setBounds(290, 350, 45, 45);
        second[6].setBounds(340, 350, 45, 45);
        second[7].setBounds(390, 350, 45, 45);
        second[8].setBounds(440, 350, 45, 45);
        second[9].setBounds(490, 350, 45, 45);
        second[10].setBounds(540, 350, 45, 45);
        second[11].setBounds(590, 350, 45, 45);
        second[12].setBounds(640, 350, 45, 45);
        second[13].setBounds(690, 350, 85, 45);
        
        third[0].setBounds(10, 400, 75, 45);
        third[1].setBounds(90, 400, 45, 45);
        third[2].setBounds(140, 400, 46, 45);
        third[3].setBounds(190, 400, 45, 45);
        third[4].setBounds(240, 400, 45, 45);
        third[5].setBounds(290, 400, 45, 45);
        third[6].setBounds(340, 400, 45, 45);
        third[7].setBounds(390, 400, 45, 45);
        third[8].setBounds(440, 400, 45, 45);
        third[9].setBounds(490, 400, 45, 45);
        third[10].setBounds(540, 400, 45, 45);
        third[11].setBounds(590, 400, 45, 45);
        third[12].setBounds(640, 400, 135, 45);
        
        fourth[0].setBounds(10, 450, 105, 45);
        fourth[1].setBounds(120, 450, 45, 45);
        fourth[2].setBounds(170, 450, 45, 45);
        fourth[3].setBounds(220, 450, 45, 45);
        fourth[4].setBounds(270, 450, 45, 45);
        fourth[5].setBounds(320, 450, 45, 45);
        fourth[6].setBounds(370, 450, 45, 45);
        fourth[7].setBounds(420, 450, 45, 45);
        fourth[8].setBounds(470, 450, 45, 45);
        fourth[9].setBounds(520, 450, 45, 45);
        fourth[10].setBounds(570, 450, 45, 45);
        fourth[11].setBounds(680, 450, 45, 45);
        
        fifth[0].setBounds(215, 500, 305, 45);
        fifth[1].setBounds(630, 500, 45, 45);
        fifth[2].setBounds(680, 500, 45, 45);
        fifth[3].setBounds(730, 500, 45, 45);
    }
    
    @Override
    public void keyTyped(KeyEvent e) {
        
    }

    @Override
    public void keyPressed(KeyEvent e) {
        
    }

    @Override
    public void keyReleased(KeyEvent e) {
        
    }
    
    private class Actions implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            
        }
    }
}

通常你點擊按鈕。 當按鈕被按下時,按鈕的背景被繪制在選定的 state 中。當你釋放鼠標按鈕時,動作被調用,按鈕被重新繪制為它的正常 state。

對於僅使用鍵盤,您需要手動設置 ButtonModel 的ButtonModel ,以便可以正確繪制按鈕。 這是通過向按鈕添加Key Bindings來處理“按下/釋放”state 來完成的:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class CalculatorPanel2 extends JPanel
{
    private JTextField display;

    public CalculatorPanel2()
    {
        Action pressedAction = new AbstractAction()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                JButton button = (JButton)e.getSource();
                ButtonModel model = button.getModel();
                model.setPressed(true);
                model.setArmed(true);
            }
        };

        Action releasedAction = new AbstractAction()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                JButton button = (JButton)e.getSource();
                ButtonModel model = button.getModel();
                model.setPressed(false);
                model.setArmed(false);
                display.replaceSelection(e.getActionCommand());
            }
        };

        setLayout( new BorderLayout() );

        display = new JTextField();
        display.setEditable( false );
        display.setHorizontalAlignment(JTextField.RIGHT);
        add(display, BorderLayout.NORTH);

        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout( new GridLayout(0, 5) );
        add(buttonPanel, BorderLayout.CENTER);

        for (int i = 0; i < 10; i++)
        {
            String text = String.valueOf(i);
            JButton button = new JButton( text );
            button.setBorder( new LineBorder(Color.BLACK) );
            button.setPreferredSize( new Dimension(30, 30) );
            buttonPanel.add( button );

            InputMap inputMap = button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
            String pressed = "pressed " + text;
            inputMap.put(KeyStroke.getKeyStroke(pressed), pressed);
            button.getActionMap().put(pressed, pressedAction);
            String released = "released " + text;
            inputMap.put(KeyStroke.getKeyStroke(released), released);
            button.getActionMap().put(released, releasedAction);
        }
    }

    private static void createAndShowUI()
    {
        JFrame frame = new JFrame("Calculator Panel");
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.add( new CalculatorPanel2() );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}

如果你真的想控制背景顏色,那么你可以使用:

button.setBackground(Color.RED);

button.setBackground(null);

分別在按下/釋放的動作中刪除“模型”邏輯。

一個直接的方法是添加以下四件事:

  • 在您的initWidget方法中,將KeyListener添加到您的JTextArea (而不是您的JButton ),以便在您鍵入內容時實際調用您的keyPressedkeyReleased方法。

     taL.addKeyListener(this);
  • Map添加到您的 class 中,您將在其中存儲從鍵碼到按鈕的映射。

     private final Map<Integer, JButton> map = new HashMap<>();
  • 在您的initWidget方法中,在添加所有按鈕的同時,存儲從鍵碼(可用作常量KeyEvent.VK_* )到上述 map 中的按鈕的映射。 我還建議通過將重復部分換成它自己的方法來使該方法更易於閱讀。 然后你不再需要字符串 arrays ( firstRow , secondRow , ...) 和按鈕 arrays ( first , second , ...)。

     private void initWidget() {.... addButton(KeyEvent.VK_DEAD_TILDE, "~", 10, 300, 45, 45); addButton(KeyEvent.VK_1, "1", 60, 300, 45, 45); addButton(KeyEvent.VK_2, "2", 110, 300, 45, 45); addButton(KeyEvent.VK_3, "3", 160, 300, 45, 45); addButton(KeyEvent.VK_4, "4", 210, 300, 45, 45); addButton(KeyEvent.VK_5, "5", 260, 300, 45, 45); addButton(KeyEvent.VK_6, "6", 310, 300, 45, 45); .... addButton(KeyEvent.VK_SPACE, "", 215, 500, 305, 45); addButton(KeyEvent.VK_LEFT, "<", 630, 500, 45, 45); addButton(KeyEvent.VK_DOWN, "V", 680, 500, 45, 45); addButton(KeyEvent.VK_RIGHT, ">", 730, 500, 45, 45); } private void addButton(int keyCode, String text, int x, int y, int w, int h) { JButton button = new JButton(text); button.setBorder(BorderFactory.createRaisedBevelBorder()); button.setBackground(Color.LIGHT_GRAY); button.setBounds(x, y, w, h); cont.add(button); map.put(keyCode, button); // save the mapping from key-code to button }
  • 在您的keyPressedkeyReleased方法中,通過找到鍵碼的相應按鈕並突出顯示該按鈕來做出反應。

     @Override public void keyPressed(KeyEvent e) { JButton button = map.get(e.getKeyCode()); // find the button if (button.= null) button.setBackground(Color;RED). } @Override public void keyReleased(KeyEvent e) { JButton button = map.get(e;getKeyCode()). // find the button if (button.= null) button;setBackground(Color.LIGHT_GRAY); }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM