Java 更改窗口背景颜色的JFrame按钮

Java 更改窗口背景颜色的JFrame按钮,java,swing,user-interface,jframe,Java,Swing,User Interface,Jframe,我正在尝试制作一个带有按钮的程序,当你点击它们时,改变框架的背景色 import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class ColorFrame { public st

我正在尝试制作一个带有按钮的程序,当你点击它们时,改变框架的背景色

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


public class ColorFrame {

public static void main(String[] args){

    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    frame.setSize(300, 200);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final JButton redButton = new JButton ("Red");
    final JButton greenButton = new JButton ("Green");
    final JButton blueButton = new JButton ("Blue");

    class Listener extends JPanel implements ActionListener{

        public void actionPerformed(ActionEvent event) {
            Color color;
            if (event.getSource() == redButton){
                color = Color.red;                  
            } else if (event.getSource() == greenButton){
                color = Color.green;
            } else {
                color = Color.blue;
            }
            setBackground(color);
            System.out.println(color);
            repaint();
        }           
    }

    redButton.addActionListener(new Listener());
    greenButton.addActionListener(new Listener());
    blueButton.addActionListener(new Listener());

    panel.add(new JButton ("Red")); 
    panel.add(new JButton ("Green"));
    panel.add(new JButton ("Blue"));
    frame.add(panel);       


}

}

但是,当我单击按钮时,似乎什么也没有发生,我认为这可能与侦听器没有被激活有关,原因是

您在此处定义了按钮:

 final JButton redButton = new JButton ("Red");
 final JButton greenButton = new JButton ("Green");
 final JButton blueButton = new JButton ("Blue");
但是,然后将全新的按钮添加到实际面板中,这样就永远不会添加带有侦听器的按钮:

panel.add(new JButton ("Red")); 
panel.add(new JButton ("Green"));
panel.add(new JButton ("Blue"));
您应该添加如下按钮:

 panel.add(redButton);
 panel.add(greenButton);
 panel.add(blueButton);

您可以在此处定义按钮:

 final JButton redButton = new JButton ("Red");
 final JButton greenButton = new JButton ("Green");
 final JButton blueButton = new JButton ("Blue");
但是,然后将全新的按钮添加到实际面板中,这样就永远不会添加带有侦听器的按钮:

panel.add(new JButton ("Red")); 
panel.add(new JButton ("Green"));
panel.add(new JButton ("Blue"));
您应该添加如下按钮:

 panel.add(redButton);
 panel.add(greenButton);
 panel.add(blueButton);

花点时间想象一下你的设置

您有一个
JFrame
。此窗口有一个
JRootPane
,其中包含一个
JLayerdPane
,其中包含一个“内容窗格”

内容窗格通常是基本窗口的最顶层组件

在此基础上,添加一个
JPanel
<默认情况下,code>JPanel是不透明的。默认情况下,内容窗格使用
边框布局
,这意味着添加到默认位置的任何内容都将放置在
中心
位置,填充可用空间

这意味着,您的框架由
JLayeredPane
、内容窗格和
JPanel
覆盖
setBackground
不像其他一些方法那样委托给内容窗格,但是,在您的情况下,它不会有帮助,因为您添加的
JPanel
正在覆盖它

除了LadyRacheya的建议外,您还有两个选择

您可以使
JPanel
透明

JPanel panel = new JPanel();
panel.setOpaque(false);
并更改内容窗格的背景色

getContentPane().setBackground(color);
或者您可以简单地更改
JPanel
的背景色

final JPanel panel = new JPanel();

//...

panel.setBackground(color);

花点时间想象一下你的设置

您有一个
JFrame
。此窗口有一个
JRootPane
,其中包含一个
JLayerdPane
,其中包含一个“内容窗格”

内容窗格通常是基本窗口的最顶层组件

在此基础上,添加一个
JPanel
<默认情况下,code>JPanel是不透明的。默认情况下,内容窗格使用
边框布局
,这意味着添加到默认位置的任何内容都将放置在
中心
位置,填充可用空间

这意味着,您的框架由
JLayeredPane
、内容窗格和
JPanel
覆盖
setBackground
不像其他一些方法那样委托给内容窗格,但是,在您的情况下,它不会有帮助,因为您添加的
JPanel
正在覆盖它

除了LadyRacheya的建议外,您还有两个选择

您可以使
JPanel
透明

JPanel panel = new JPanel();
panel.setOpaque(false);
并更改内容窗格的背景色

getContentPane().setBackground(color);
或者您可以简单地更改
JPanel
的背景色

final JPanel panel = new JPanel();

//...

panel.setBackground(color);
试试这个:

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


public class ColorFrame {

public static void main(String[] args){

    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    frame.setSize(300, 200);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final JButton redButton = new JButton ("Red");
    final JButton greenButton = new JButton ("Green");
    final JButton blueButton = new JButton ("Blue");

    class Listener extends JPanel implements ActionListener{

        public void actionPerformed(ActionEvent event) {
            Color color;
            if (event.getSource() == redButton){
                color = Color.red;
                redButton.setBackground(color);
                panel.setBackground(color);//To set panel background instead of frames background
            } else if (event.getSource() == greenButton){
                color = Color.green;
                greenButton.setBackground(color);
                panel.setBackground(color);
            } else {
                color = Color.blue;

                blueButton.setBackground(color);
                panel.setBackground(color);  
            }
            setBackground(color);
            System.out.println(color);
            repaint();
        }           
    }

    redButton.addActionListener(new Listener());
    greenButton.addActionListener(new Listener());
    blueButton.addActionListener(new Listener());

    panel.add(redButton); 
    panel.add(greenButton);
    panel.add(blueButton);
    frame.add(panel);       
frame.setVisible(true);


}

}
试试这个:

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


public class ColorFrame {

public static void main(String[] args){

    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    frame.setSize(300, 200);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final JButton redButton = new JButton ("Red");
    final JButton greenButton = new JButton ("Green");
    final JButton blueButton = new JButton ("Blue");

    class Listener extends JPanel implements ActionListener{

        public void actionPerformed(ActionEvent event) {
            Color color;
            if (event.getSource() == redButton){
                color = Color.red;
                redButton.setBackground(color);
                panel.setBackground(color);//To set panel background instead of frames background
            } else if (event.getSource() == greenButton){
                color = Color.green;
                greenButton.setBackground(color);
                panel.setBackground(color);
            } else {
                color = Color.blue;

                blueButton.setBackground(color);
                panel.setBackground(color);  
            }
            setBackground(color);
            System.out.println(color);
            repaint();
        }           
    }

    redButton.addActionListener(new Listener());
    greenButton.addActionListener(new Listener());
    blueButton.addActionListener(new Listener());

    panel.add(redButton); 
    panel.add(greenButton);
    panel.add(blueButton);
    frame.add(panel);       
frame.setVisible(true);


}

}

监听器现在被激活了,但是背景颜色仍然没有改变。我认为问题是你在监听器中设置了背景,实际上设置了监听器的背景颜色,而不是按钮。你应该看看以前是怎么做的,或者保留对按钮的引用,你可以从监听器访问该按钮,然后执行
redButton.setBackground()
greenButton.setBackground()
setBackground
(在此上下文中)将设置“frames”背景。框架上有一个
JRootPane
,作为
JLayerdPane
,上面有一个“内容窗格”,最后是OP的面板。因此,虽然框架的背景颜色可能会发生变化,但用户不可能看到它。相反,他们应该更改放置在内容窗格上的
JPanel
的颜色…(顺便说一下,+1;)监听器现在被激活了,但是背景颜色仍然没有改变。我认为问题是你在监听器中设置了背景,实际上设置了监听器的背景颜色,而不是按钮。你应该看看以前是怎么做的,或者保留对按钮的引用,你可以从监听器访问该按钮,然后执行
redButton.setBackground()
greenButton.setBackground()
setBackground
(在此上下文中)将设置“frames”背景。框架上有一个
JRootPane
,作为
JLayerdPane
,上面有一个“内容窗格”,最后是OP的面板。因此,虽然框架的背景颜色可能会发生变化,但用户不可能看到它。相反,他们应该更改放置在内容窗格上的
JPanel
的颜色…(顺便说一下,+1;)