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

时间:2014-01-06 20:52:57

标签: 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 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);       


}

}

然而,当我点击按钮时,似乎没有任何事情发生,我认为它可能与未被激活的听众有关

4 个答案:

答案 0 :(得分:3)

您可以在此处定义按钮:

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

答案 1 :(得分:3)

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

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

RootPane

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

在此之后,您添加JPanel。默认情况下,JPanel是不透明的。默认情况下,内容窗格使用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);

答案 2 :(得分:0)

试试这个:

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


}

}

答案 3 :(得分:-2)

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