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

2022-01-24 00:00:00 user-interface java swing jframe

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

I am trying to make a program with buttons, that when you click them, change the background color of the frame

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);       


}

}

然而,当我点击按钮时,似乎什么都没有发生,我认为这可能与监听器没有被激活有关

Yet when I click the buttons, nothing seems to happen and I think it might have something to do with the listeners not being activated for reason

推荐答案

花点时间可视化您的设置...

Take a moment to visualise you setup...

你有一个 JFrame.此窗口有一个 JRootPane,其中包含一个 JLayerdPane,其中包含一个内容窗格".

You have a JFrame. This window has a JRootPane, which contains a JLayerdPane, which contains a the "content pane".

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

The content pane is generally the most top level component of a basic window.

在此基础上添加一个 JPanel.JPanel 默认是不透明的.默认情况下,内容窗格使用 BorderLayout,这意味着添加到默认位置的任何内容都将放置在 CENTER 位置,填充可用空间...

On to this, you add a JPanel. JPanel is opaque by default. By default, the content pane uses a BorderLayout, this means that anything added to the default position will be placed in the CENTER position, filling the available space...

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

This means, you frame is covered by a JLayeredPane, content pane AND your JPanel. setBackground does not delegate to the content pane like some of the other methods, but, in your case, it wouldn't help, as the JPanel you add is now covering it...

除了 LadyRacheya 的建议,你还有两个选择.

In addition to LadyRacheya suggestions, you have two choices.

您可以使 JPanel 透明...

You can make the JPanel transparent...

JPanel panel = new JPanel();
panel.setOpaque(false);

并更改内容窗格的背景颜色...

And change the background color of the content pane...

getContentPane().setBackground(color);

或者您可以简单地更改 JPanel....

OR you can simply change the background color of the JPanel....

final JPanel panel = new JPanel();

//...

panel.setBackground(color);

相关文章