如何动态更改JButtom背景?

时间:2017-03-18 04:57:31

标签: 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.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());

       }

    }


}

1 个答案:

答案 0 :(得分:1)

知道您不需要4x14按钮,标签和文本文件来证明您在一个JButton中更改背景颜色不需要Java知识。 这个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,但您从未使用它。
如果您需要更多帮助,请不要犹豫。