【问题标题】:JFrame buttons that change background color of window更改窗口背景颜色的 JFrame 按钮
【发布时间】:2023-03-25 06:13:01
【问题描述】:

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

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


}

}

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

【问题讨论】:

    标签: java swing user-interface jframe


    【解决方案1】:

    您在此处定义按钮:

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

    【讨论】:

    • 监听器现在被激活了,但是背景颜色仍然没有改变
    • 我认为问题在于您在侦听器中设置背景并实际设置侦听器的背景颜色 - 而不是按钮。您应该查看link 以了解它之前是如何完成的,或者保留对您可以从侦听器访问的按钮的引用并执行redButton.setBackground()greenButton.setBackground()
    • setBackground(在此上下文中)将设置“框架”背景。框架上有一个JRootPane,上面有一个JLayerdPane,上面有一个“内容窗格”,最后是OP的面板。因此,虽然框架的背景颜色可能会发生变化,但用户永远不可能看到它。相反,他们应该更改他们放置在内容窗格上的JPanel 的颜色...顺便说一下+1 ;)
    【解决方案2】:

    花点时间想象一下您的设置...

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

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

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

    这意味着,您的框架被 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);
    

    【讨论】:

      【解决方案3】:

      试试这个:

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

      【讨论】:

        【解决方案4】:
        public class ColorFrame {
        
        public JPanel panel;
        public static void main(String[] args){
        
            JFrame frame = new JFrame();
            final 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){
        
                        redButton.setBackground(Color.RED);
                        panel.setBackground(Color.RED);
        
                    } else if (event.getSource() == greenButton){
        
                        greenButton.setBackground(Color.GREEN);
                        panel.setBackground(Color.GREEN);
        
                    } else {
        
                        blueButton.setBackground(Color.BLUE);
                        panel.setBackground(Color.BLUE);  
                    }
        
                    setBackground(Color.WHITE);
                    System.out.println(Color.WHITE);
                    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);
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-06-04
          • 1970-01-01
          • 1970-01-01
          • 2021-06-22
          • 2015-04-03
          相关资源
          最近更新 更多