Java 如何动态更改JButtom背景?

Java 如何动态更改JButtom背景?,java,swing,actionlistener,Java,Swing,Actionlistener,所以我在尝试改变按键的背景色时遇到了麻烦,我不知道该怎么做? 它就像一个键盘,我唯一的问题是在键入任何键时改变背景颜色的方法 import java.awt.BorderLayout; import java.awt.Color; import java.awt.GridLayout; import java.awt.Panel; import java.awt.TextArea; import java.awt.event.ActionEvent; import java.awt.event.

所以我在尝试改变按键的背景色时遇到了麻烦,我不知道该怎么做? 它就像一个键盘,我唯一的问题是在键入任何键时改变背景颜色的方法

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.AbstractAction;

public abstract class OnScreenKeyboard extends JFrame implements KeyListener

{
    String firstRow[] = {"~","1","2","3","4","5","6","7","8","9","0","-","+","Back\nSpace"};
    String secondRow[] = {"Tab","Q","W","E","R","T","Y","U","I","O","P","[","]","|"};
    String thirdRow[] = {"Caps\nLock","A","S","D","F","G","H","J","K","L",";","'","Enter"};
    String fourthRow[] = {"Shift","Z","X","C","V","B","N","M",",",".","?","Space"};
    JButton first[] = new JButton[14];
    JButton second[] = new JButton[14];
    JButton third[] = new JButton[13];
    JButton fourth[] = new JButton[12];
    Panel keys = new Panel();
    Panel text = new Panel();
    TextArea textArea = new TextArea();
    String strText = "";
    private JLabel label1;
    private JLabel label2;
    private JTextField textField;
    public OnScreenKeyboard()

{
   super("Typing Application");

    label1 = new JLabel("Type some text using your keyboard.  The keys you press will be "
             + "highlighed and the text will be displayed");
    add(label1);
    label2 = new JLabel("Note: clicking the buttons with your mouse will not perform any action");
    add(label2);
    textField = new JTextField(80);
    textField.setEditable(true);
    TextFieldHandler handler = new TextFieldHandler();
    this.setLayout(new BorderLayout(1,1));
    keys.setLayout(new GridLayout(4,14));
    text.setLayout(new BorderLayout(1,1));
    text.add(textArea);



    for(int i=0; i<14; i++)
     {
     first[i] = new JButton(firstRow[i]);
     first[i].setBackground(Color.white);
     keys.add(first[i]);
     first[i].addKeyListener(this);
     }


     for(int i=0; i<14; i++)
     {
     second[i] = new JButton(secondRow[i]);
     second[i].setBackground(Color.white);
     keys.add(second[i]);
     second[i].addKeyListener(this);
     }


     for(int i=0; i<13; i++)
     {
     third[i] = new JButton(thirdRow[i]);
     third[i].setBackground(Color.white);
     keys.add(third[i]);
     third[i].addKeyListener(this);
     }

     for(int i=0; i<12; i++)
     {
     fourth[i] = new JButton(fourthRow[i]);
     fourth[i].setBackground(Color.white);
     keys.add(fourth[i]);
     fourth[i].addKeyListener(this);
     }

     add(text, BorderLayout.NORTH);
     add(keys,BorderLayout.CENTER);
}


   private class TextAreaHandler implements ActionListener
    {

      public void actionPerformed( ActionEvent event )
      {
        String string = ""; // declare string to display
         if ( event.getSource() == textField )
            string = String.format( "%s",
               event.getActionCommand() );

    }
  }

      public void actionPerformed(ActionEvent event) {
            if (event.getSource() instanceof JButton) {
                System.out.println((((JButton) event.getSource()).getActionCommand()));
                ((JButton) event.getSource()).setBackground(Color.BLUE);
                ((JButton) event.getSource()).setContentAreaFilled(false);
                ((JButton) event.getSource()).setOpaque(true);
            }
      }


    @Override
    public void keyTyped( KeyEvent event )
    {
        int keyCode = event.getKeyCode();
        strText = String.format( "%s", event.getKeyCode() );
    }



    private class TextFieldHandler implements ActionListener
    {
      public void actionPerformed( ActionEvent event )
        {
         String string = ""; // declare string to display
                 // user pressed Enter in JTextField textField1
                if ( event.getSource() == textField )
            string = String.format("%s", event.getActionCommand());

       }

    }


}
导入java.awt.BorderLayout;
导入java.awt.Color;
导入java.awt.GridLayout;
导入java.awt.Panel;
导入java.awt.TextArea;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.awt.event.KeyEvent;
导入java.awt.event.KeyListener;
导入javax.swing.JFrame;
导入javax.swing.JButton;
导入javax.swing.JLabel;
导入javax.swing.JTextField;
导入javax.swing.AbstractAction;
ScreenkeyBoard上的公共抽象类扩展了JFrame,实现了KeyListener
{
字符串第一行[]={“~”,“1”,“2”,“3”,“4”,“5”,“6”,“7”,“8”,“9”,“0”,“-”,“+”,“Back\nSpace”};
字符串第二行[]={“Tab”,“Q”,“W”,“E”,“R”,“T”,“Y”,“U”,“I”,“O”,“P”,““,[”,“],“|”};
字符串thirdRow[]={“Caps\nLock”、“A”、“S”、“D”、“F”、“G”、“H”、“J”、“K”、“L”、““;”、“”、“Enter”};
字符串四掷[]={“移位”、“Z”、“X”、“C”、“V”、“B”、“N”、“M”、“M”、““、”、“?”和“空格”};
JButton first[]=新JButton[14];
JButton second[]=新JButton[14];
JButton third[]=新JButton[13];
JButton第四[]=新JButton[12];
面板键=新面板();
面板文本=新面板();
TextArea TextArea=新建TextArea();
字符串strText=“”;
私人JLabel标签1;
私人JLabel标签2;
私有JTextField textField;
公共屏幕黑板()
{
超级(“打字申请”);
label1=new JLabel(“使用键盘键入一些文本。您按下的键将是”
+“高亮显示,将显示文本”);
添加(标签1);
label2=new JLabel(“注意:用鼠标单击按钮将不会执行任何操作”);
添加(标签2);
textField=新的JTextField(80);
textField.setEditable(true);
TextFieldHandler=新的TextFieldHandler();
这个.setLayout(新的BorderLayout(1,1));
设置布局(新网格布局(4,14));
setLayout(新的BorderLayout(1,1));
text.add(textArea);

对于(int i=0;i不需要Java知识就可以知道,您不需要4x14按钮、标签和文本文件来演示一个
JButton
中背景颜色的变化。 这不是MCVE演示的吗

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JFrame;

public class OnScreenKeyboard extends JFrame{

    public OnScreenKeyboard()
    {
        super();
        JButton button = new JButton("T");
        add(button,BorderLayout.CENTER);
        pack();//"size to fit the preferred size and layouts of its subcomponents"
        setVisible(true);//make Jframe show
    }

    public void actionPerformed(ActionEvent event) {
        if (event.getSource() instanceof JButton) {
            System.out.println((((JButton) event.getSource()).getActionCommand()));
            ((JButton) event.getSource()).setBackground(Color.BLUE);
            ((JButton) event.getSource()).setContentAreaFilled(false);
            ((JButton) event.getSource()).setOpaque(true);
        }
    }

    //a main is needed to make your code runnable (google MCVE)
    public static void main(String[] args) {

        new OnScreenKeyboard();
    }
}
当它简短明了时,它还可以帮助您调试问题。
提示:您编写了一个
ActionListener
,但从未使用过它。

如果你需要更多的帮助,请尽管问。

欢迎来到TL;Dr.,这是最短的代码来证明这个问题吗?请发布一个我不是很有经验的java或编码,这是我能想到的最短的代码。如果有较短的方法请告诉我!我非常渴望从错误中学习并学习差异。请参阅:keybindings和/或get/putClientProperty