Java 更改窗口背景颜色的 JFrame 按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20959053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JFrame buttons that change background color of window
提问by manis
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
然而,当我点击按钮时,似乎什么也没发生,我认为这可能与听众没有被激活有关
回答by LadyBernkastel
You define the buttons here:
您在此处定义按钮:
final JButton redButton = new JButton ("Red");
final JButton greenButton = new JButton ("Green");
final JButton blueButton = new JButton ("Blue");
But then you add entirely new buttons to the actual panel so the buttons with the listeners attached are never added:
但是随后您将全新的按钮添加到实际面板中,因此永远不会添加带有侦听器的按钮:
panel.add(new JButton ("Red"));
panel.add(new JButton ("Green"));
panel.add(new JButton ("Blue"));
You should add the buttons like this:
您应该添加这样的按钮:
panel.add(redButton);
panel.add(greenButton);
panel.add(blueButton);
回答by MadProgrammer
Take a moment to visualise you setup...
花点时间想象一下您的设置...
You have a JFrame
. This window has a JRootPane
, which contains a JLayerdPane
, which contains a the "content pane".
你有一个JFrame
. 此窗口有一个JRootPane
,其中包含一个JLayerdPane
,其中包含一个“内容窗格”。
The content pane is generally the most top level component of a basic window.
内容窗格通常是基本窗口的最顶层组件。
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...
对此,您添加一个JPanel
. JPanel
默认情况下是不透明的。默认情况下,内容窗格使用 a BorderLayout
,这意味着添加到默认位置的任何内容都将放置在该CENTER
位置,填充可用空间...
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...
这意味着,您的框架被JLayeredPane
, 内容窗格和您的JPanel
. setBackground
不像其他一些方法那样委托给内容窗格,但是,在您的情况下,它无济于事,因为JPanel
您添加的现在覆盖了它......
In addition to LadyRacheya suggestions, you have two choices.
除了 LadyRacheya 的建议,您还有两个选择。
You can make the JPanel
transparent...
你可以把JPanel
透明...
JPanel panel = new JPanel();
panel.setOpaque(false);
And change the background color of the content pane...
并更改内容窗格的背景颜色...
getContentPane().setBackground(color);
OR you can simply change the background color of the JPanel
....
或者你可以简单地改变的背景颜色JPanel
....
final JPanel panel = new JPanel();
//...
panel.setBackground(color);
回答by ravibagul91
Try this:
尝试这个:
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);
}
}
回答by user3180441
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);
}