Java 设置动作侦听器和更改背景

Java 设置动作侦听器和更改背景,java,swing,Java,Swing,我已经创建了3个按钮。它们在JFrame中分别显示两次。我无法更改帧的背景。我已经设置了ActionListeners,但单击后,什么都没有改变。我可以请求一些帮助吗 public class MyButtons extends JPanel { public static JFrame frame; private JButton Red = new JButton("Red"); private JButton Green = new JButton("Green")

我已经创建了3个按钮。它们在JFrame中分别显示两次。我无法更改帧的背景。我已经设置了ActionListeners,但单击后,什么都没有改变。我可以请求一些帮助吗

public class MyButtons extends JPanel {
    public static JFrame frame;
    private JButton Red = new JButton("Red");
    private JButton Green = new JButton("Green");
    private JButton Blue = new JButton("Blue");

    public void InitializeButton()
    {       
        Blue.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            frame.setBackground(Color.BLUE);
          }
        });

        Green.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            frame.setBackground(Color.GREEN);
          }
        });

        Red.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            frame.setBackground(Color.RED);
          }
        });
    }

    public MyButtons()  {       
        InitializeButton();
        add(Red);
        add(Green);
        add(Blue);      
    }   

    public static void main(String[] args) {
        frame = new JFrame();


        JPanel row1 = new MyButtons();
        JPanel row2 = new MyButtons();

        row1.setPreferredSize(new Dimension(250, 100));
        row2.setPreferredSize(new Dimension(250, 100));

        frame.setLayout(new GridLayout(3,2));
        frame.add(row1);
        frame.add(row2);
        frame.pack();        
        frame.setVisible(true);
    }

   }

这段代码可以工作,但可能不像您预期的那样:


注意:请学习常见的Java命名法(命名约定-例如,
EachWordUpperCaseClass
firstWordLowerCaseMethod()
firstWordLowerCaseAttribute
,除非它是一个
大写常量
),并一致地使用它。

此代码有效,但可能不像您预期的那样:


注意:请学习常见的Java命名法(命名约定-例如,
EachWordUpperCaseClass
firstWordLowerCaseMethod()
firstWordLowerCaseAttribute
,除非它是一个
大写常量
),并始终如一地使用它。

看起来1)框架完全由面板覆盖,或2)对
setBackground(..)
的调用未传递到框架的内容窗格。-更好的策略是将两个面板添加到第三个面板中,并保留一个引用,然后更改…的背景。ActionListeners没有问题。问题在于ContentPane的背景被GridLayout覆盖。通过运行您的程序,按下任意按钮,然后调整JFrame的大小来尝试。您将看到背景色的一小部分时间,直到它调整大小。如果您的图形卡速度太快,请添加如下内容:System.out.println(frame.getBackground().getBlue());给你的蓝色听众。每次按Blue键时,它都会打印255。我会在GridLayout中添加一个空的第三个面板,并更改其颜色,而不是更改框架的颜色。1)框架似乎完全被面板覆盖,或者2)对
setBackground(…)
的调用没有传递到框架的内容窗格。-更好的策略是将两个面板添加到第三个面板中,并保留一个引用,然后更改…的背景。ActionListeners没有问题。问题在于ContentPane的背景被GridLayout覆盖。通过运行您的程序,按下任意按钮,然后调整JFrame的大小来尝试。您将看到背景色的一小部分时间,直到它调整大小。如果您的图形卡速度太快,请添加如下内容:System.out.println(frame.getBackground().getBlue());给你的蓝色听众。每次按Blue键时,它都会打印255。我会在GridLayout中添加一个空的第三个面板,并更改其颜色,而不是框架的颜色。嗨@Andrew Thompson,如果我希望它是分层颜色呢。例如,如果在顶部Jpanel中按下红色,则上半部分显示红色,下半部分显示蓝色,然后下半部分显示蓝色。有没有办法让按钮相互影响?例如,如果我选择红色按钮,整个背景应该变为红色。你明白为什么这个例子只显示一个色带吗?你知道如何改变它的大小吗?嗨,安德鲁·汤普森,如果我想要它是分层的颜色呢。例如,如果在顶部Jpanel中按下红色,则上半部分显示红色,下半部分显示蓝色,然后下半部分显示蓝色。有没有办法让按钮相互影响?例如,如果我选择红色按钮,整个背景应该变为红色。你明白为什么这个例子只显示一个色带吗?你知道如何改变它的大小吗?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MyButtons extends JPanel {

    //public static JFrame frame;
    // static is rarely a solution of problems in a GUI. ToDo! Change!
    static JPanel ui = new JPanel(new GridLayout(2, 0, 20, 20));

    private JButton Red = new JButton("Red");
    private JButton Green = new JButton("Green");
    private JButton Blue = new JButton("Blue");

    public void InitializeButton() {
        Blue.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ui.setBackground(Color.BLUE);
            }
        });

        Green.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ui.setBackground(Color.GREEN);
            }
        });

        Red.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ui.setBackground(Color.RED);
            }
        });
    }

    public MyButtons() {
        InitializeButton();
        add(Red);
        add(Green);
        add(Blue);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(250, 100);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();

        JPanel row1 = new MyButtons();
        JPanel row2 = new MyButtons();

        //row1.setPreferredSize(new Dimension(250, 100));
        //row2.setPreferredSize(new Dimension(250, 100));

        //frame.setLayout(new GridLayout(3, 2,10,10));
        ui.add(row1);
        ui.add(row2);
        frame.add(ui);
        frame.pack();
        frame.setVisible(true);
    }
}