java 图形编程问题

来源:百度知道 编辑:UC知道 时间:2024/07/05 20:40:37
这段代码中,如果我继承的是Frame,背景颜色就可以显示出来,如果继承的是JFrame就显示不出来,为什么?
class MyFrame extends JFrame
{
public MyFrame()
{
this.setBackground(Color.BLUE);
this.setAlwaysOnTop(true);
this.setResizable(false);
this.setSize(200, 200);
}
}

package com.baidu;

import java.awt.Color;
import javax.swing.JFrame;

public class MyFrame extends JFrame {
private static final long serialVersionUID = 1L;

public MyFrame() {
this.getContentPane().setBackground(Color.BLUE);
this.setAlwaysOnTop(true);
this.setResizable(false);
this.setSize(200, 200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String[] args) {
new MyFrame();
}
}
这样能行。解释的话,你可以去看看关于JFrame和Frame的区别。我没有看懂不过

class MyFrame extends JFrame
{
public MyFrame()
{
Container c = getContentPane();
c.setBackground(Color.BLUE);
this.setBackground(Color.BLUE);
this.setAlwaysOnTop(true);
this.setResizable(false);
this.setSize(200, 200);
}

这样就可以了。
Container c 是JFrame里面的一个容器,用来放各类组件

LZ可以参考一下Java文档的JRootPane类和Roo