................................................ ...................................................... ...................................................... …………
1)
JFrame
不能这样做,您必须更改内容窗格的 Color
,例如
JFrame.getContentPane().setBackground(myColor)
2) 您需要将 GUI 相关代码(在
main
方法中)包装到 invokeLater
例如:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GUI {
public GUI() {
JFrame frame = new JFrame();
frame.setTitle("Test Background");
frame.setLocation(200, 100);
frame.setSize(600, 400);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.getContentPane().setBackground(Color.BLUE);
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
GUI gUI = new GUI();
}
});
}
}
而不是
f.setBackground(Color.RED);
致电
f.getContentPane().setBackground(Color.RED);
内容窗格就是显示的内容。
顺便说一句,这里有一个
JFrame
提示:您可以拨打 f.add(child)
,孩子将被添加到内容窗格中。