我有一个简单的程序来改变JFrame的背景和JLabel的前景。 JFrame上有一个JLabel和2个JButtons,一个用于更改单词,另一个用于更改背景。
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class ColorChooserDemo extends JPanel {
static JFrame frame;
JLabel label;
JButton button1, button2;
public ColorChooserDemo() {
this.setBackground(new Color(255, 0, 255));
setLayout(null);
label = new JLabel("This is Text");
button1 = new JButton("Set Word Color");
button2 = new JButton("Set Background Color");
add(label);
add(button1);
add(button2);
label.setBounds(130, 10, 111, 15);
button1.setBounds(100, 40, 200, 35);
button2.setBounds(70, 90, 250, 35);
button1.addActionListener(buttonPressed);
button2.addActionListener(buttonPressed);
button1.setActionCommand("words");
button2.setActionCommand("back");
label.setFont(new Font("Helvetica", Font.BOLD, 20));
button1.setFont(new Font("Helvetica", Font.BOLD, 20));
button2.setFont(new Font("Helvetica", Font.BOLD, 20));
}
AbstractAction buttonPressed = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("words")) {
Color c = JColorChooser.showDialog(null, "Choose a Color", label.getForeground());
if (c != null)
label.setForeground(c);
}
if (e.getActionCommand().equals("back")) {
Color c = JColorChooser.showDialog(null, "Choose a Color", label.getForeground());
if (c != null)
frame.getContentPane().setBackground(c);
}
repaint();
}
};
public static void main(String[] args) {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
frame = new JFrame("ColorChooserDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new ColorChooserDemo());
frame.setSize(400, 200);
frame.setLocation((int) (screenSize.getWidth() - 400) / 2, (int) (screenSize.getHeight() - 200) / 2);
frame.setResizable(false);
frame.setVisible(true);
}
}
无论出于何种原因,frame.getContentPane().setBackground(c);
都不起作用。
我寻找解决方案,但没有任何工作,我不明白为什么。请帮忙。
- EDIT--
我试过了repaint();
但它仍然不起作用。它可能与frame
是静态的吗?
答案 0 :(得分:1)
这是一个很好的例子,例如static
之类的东西会刺伤你......
所以,在你的代码中,你正在做......
frame.getContentPane().setBackground(c);
当你想改变背景颜色时,你必须问自己这个问题,contentPane
是什么?由于您的组件从JPanel
(不透明)延伸并设置了自己的背景颜色(this.setBackground(new Color(255, 0, 255));
)(并且因为默认情况下JFrame
使用了BorderLayout
),因此您的组件覆盖了框架的整个内容区域,因此任何对frame.getContentPane().setBackground(c)
的调用都不可见(因为您的面板覆盖了它)。
相反,你应该简单地使用......
setBackground(c);
澄清......
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class ColorChooserDemo extends JPanel {
JLabel label;
JButton button1, button2;
public ColorChooserDemo() {
this.setBackground(new Color(255, 0, 255));
setLayout(new GridBagLayout());
label = new JLabel("This is Text");
button1 = new JButton("Set Word Color");
button2 = new JButton("Set Background Color");
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(label, gbc);
add(button1, gbc);
add(button2, gbc);
button1.addActionListener(buttonPressed);
button2.addActionListener(buttonPressed);
button1.setActionCommand("words");
button2.setActionCommand("back");
label.setFont(new Font("Helvetica", Font.BOLD, 20));
button1.setFont(new Font("Helvetica", Font.BOLD, 20));
button2.setFont(new Font("Helvetica", Font.BOLD, 20));
}
AbstractAction buttonPressed = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("words")) {
Color c = JColorChooser.showDialog(null, "Choose a Color", label.getForeground());
if (c != null) {
label.setForeground(c);
}
}
if (e.getActionCommand().equals("back")) {
System.out.println("...");
Color c = JColorChooser.showDialog(null, "Choose a Color", getBackground());
if (c != null) {
setBackground(c);
}
}
repaint();
}
};
public static void main(String[] args) {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
JFrame frame = new JFrame("ColorChooserDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new ColorChooserDemo());
frame.setSize(400, 200);
frame.setLocation((int) (screenSize.getWidth() - 400) / 2, (int) (screenSize.getHeight() - 200) / 2);
frame.setResizable(false);
frame.setVisible(true);
}
}
我还强烈建议您查看Laying Out Components Within a Container,How to Use GridBagLayout和How to Use BorderLayout,了解有关layou管理API如何运作的详细信息