【发布时间】:2017-02-23 12:18:27
【问题描述】:
我无法让 JPanel 改变颜色。我也无法让 JFrame 改变颜色。我在网上看过......我还有另一个程序,它具有几乎相同的代码来设置 JPanel 和 JFrame。我就是无法让它工作。
这是我的主要方法:
public static void main(String[] args){
JFrame frame = new JFrame("title");
frame.getContentPane().setBackground(Color.WHITE);
Drawing drawing = new Drawing(2);
drawing.setBackground(Color.CYAN);
frame.add(drawing);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
...
编辑:稍后在我的主要方法中是
frame.setLocationRelativeTo(null);
frame.setVisible(true);
这里是 JPanel 的构造函数:
public class Drawing extends JPanel {
// instance variables
public Drawing(int n){
setOpaque(true);
setPreferredSize(new Dimension(300, 300));
setBackground(Color.PINK);
...
并且背景颜色保持默认的灰色。
【问题讨论】:
-
@SimonJensen Eclipse
-
如需尽快获得更好的帮助,请发帖minimal reproducible example 或Short, Self Contained, Correct Example。