简体   繁体   English

使用JButton更改JFrame中的背景颜色

[英]Using JButton to change background color in JFrame

I'm writing a program that uses JButton. 我正在编写一个使用JButton的程序。 When the user clicks a button, the background should change color, but the JFrame can't be accessed from the actionPerformed() method. 当用户单击按钮时,背景应更改颜色,但无法从actionPerformed()方法访问JFrame。 Can someone please tell me how to make it work? 有人可以告诉我如何使其工作吗?

import java.awt.event.*; 
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
public class HandlerClass implements ActionListener{

  public static void main(String[] args){
    HandlerClass handler = new HandlerClass();
     final JFrame f = new JFrame("Testing out these JPanels");
     f.setSize(400, 100); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.setLocationRelativeTo(null); 
     f.setLayout(new GridLayout(2, 3));
     JButton b = new JButton("button 1");
     b.addActionListener(new HandlerClass());
     JButton butt = new JButton("button 2");

     JButton bug = new JButton("button 3");

     JButton button = new JButton("button 4");

     JButton button5 = new JButton("button 5");

     JButton button6 = new JButton("button 6");

     JPanel p = new JPanel();
     p.setVisible(true);
     JPanel pnl = new JPanel();
     p.add(b);
     p.add(butt);
     p.add(bug);
     pnl.add(button);
     pnl.add(button5);
     pnl.add(button6);
     f.add(p, BorderLayout.CENTER);
     f.add(pnl, BorderLayout.SOUTH);
     f.setVisible(true);
     f.setBackground(Color.RED);
   }
     public void actionPerformed(ActionEvent e){
        f.setBackground(Color.WHITE);
       }

     }
  1. Don't write all your code in a single huge main method. 不要用一个庞大的main方法编写所有代码。 That's for rank beginner programs, and if you want your code to do more than hello world, you're going to have to make it a true OOPs program. 这是针对高级初学者程序的,如果您希望代码做的比hello world还要多,则必须使其成为真正的OOPs程序。
  2. Create a class with non-static fields and methods. 用非静态字段和方法创建一个类。
  3. Have your ActionListener call a method that in its body changes the background color of your main JPanel, the one that you add to the JFrame's contentPane. 让您的ActionListener调用一种方法,该方法在其主体中更改主JPanel的背景色,即您添加到JFrame的contentPane中的背景色。
  4. Consider using anonymous inner listener classes, and then off-load the meat of the listener code to a separate method, either in your GUI or in a control class. 考虑使用匿名内部侦听器类,然后将侦听器代码的内容卸载到GUI或控件类中的单独方法中。

You have few choices: 您有几种选择:

  1. Move the JFrame instance outside the main body so that it becomes an instance variable as such: JFrame实例移至主体外部,使其成为实例变量 ,如下所示:

     public class HandlerClass { private JFrame frame; public HandlerClass() { ... } } 
  2. Move the ActionListener inside the same method as such: ActionListener相同的方法中,如下所示:

     public class HandlerClass { public HandlerClass() { final JFrame frame = new JFrame(); JButton button = new JButton(); ... button.addActionListener(new ActionerListener() { @Override public void actionPerformed(ActionEvent e){ frame.setBackground(Color.WHITE); } } } } 

    The only issue with this second choice is that you must make the reference to frame with the final modifier which means you can't change the reference later . 第二种选择的唯一问题是,必须使用final修饰符对frame进行引用,这意味着以后无法更改引用

  3. Do both of the above. 同时执行以上两项。

Your problem: 你的问题:

JFrame can't be accessed from the actionPerformed()

The solution is, You cannot access a local variable from another method Take a look at here and here 解决的办法是,你不能从其他方法来访问一个局部变量,在看看这里这里

Now the important thing: You have two JPanel s over your JFrame . 现在重要的事情是: JFrame有两个JPanel So if you change the color of the JFrame , then you will not be able to see the color change. 因此,如果更改JFrame的颜色,则将看不到颜色变化。 So my suggestion is change the color of the JPanel . 因此,我的建议是更改JPanel的颜色。 Take the example of the code below: 请看下面的代码示例:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class HandlerClass extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    HandlerClass frame = new HandlerClass();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public HandlerClass() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        GridBagLayout gbl_contentPane = new GridBagLayout();
        gbl_contentPane.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0};
        gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0};
        gbl_contentPane.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
        gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
        contentPane.setLayout(gbl_contentPane);

        JButton btnRed = new JButton("Red");
        GridBagConstraints gbc_btnRed = new GridBagConstraints();
        gbc_btnRed.insets = new Insets(0, 0, 5, 5);
        gbc_btnRed.gridx = 3;
        gbc_btnRed.gridy = 1;
        contentPane.add(btnRed, gbc_btnRed);

        JButton btnBlue = new JButton("Blue");
        GridBagConstraints gbc_btnBlue = new GridBagConstraints();
        gbc_btnBlue.insets = new Insets(0, 0, 5, 0);
        gbc_btnBlue.gridx = 5;
        gbc_btnBlue.gridy = 1;
        contentPane.add(btnBlue, gbc_btnBlue);

        JButton btnWhite = new JButton("White");
        GridBagConstraints gbc_btnWhite = new GridBagConstraints();
        gbc_btnWhite.insets = new Insets(0, 0, 0, 5);
        gbc_btnWhite.gridx = 3;
        gbc_btnWhite.gridy = 2;
        contentPane.add(btnWhite, gbc_btnWhite);

        JButton btnGreen = new JButton("Green");
        GridBagConstraints gbc_btnGreen = new GridBagConstraints();
        gbc_btnGreen.gridx = 5;
        gbc_btnGreen.gridy = 2;
        contentPane.add(btnGreen, gbc_btnGreen);

        btnRed.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                contentPane.setBackground(Color.red);

            }
        });
        btnBlue.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                contentPane.setBackground(Color.blue);
            }
        });
        btnWhite.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                contentPane.setBackground(Color.WHITE);

            }
        });
        btnGreen.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                contentPane.setBackground(Color.green);
            }
        });

        pack();
    }

}

Screen shot: 屏幕截图: 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM