当前位置: 技术问答>java相关
这种问题实在不该问啊,可我得把java进行到底的呀!
来源: 互联网 发布时间:2015-09-09
本文导语: 代码的目的只是为了用方法来更换窗体的背景,可是为什么背景色会一闪就不在呢?唉,无奈啊! import java.awt.*; import javax.swing.JFrame; public class Frame1 extends JFrame { public Frame1() { Color c = Color.blue; this....
代码的目的只是为了用方法来更换窗体的背景,可是为什么背景色会一闪就不在呢?唉,无奈啊!
import java.awt.*;
import javax.swing.JFrame;
public class Frame1 extends JFrame
{
public Frame1()
{
Color c = Color.blue;
this.setBackground(c);
this.repaint();
setSize(600,800);
show();
}
public static void main(String[] args)
{
Frame1 frame1 = new Frame1();
}
}
import java.awt.*;
import javax.swing.JFrame;
public class Frame1 extends JFrame
{
public Frame1()
{
Color c = Color.blue;
this.setBackground(c);
this.repaint();
setSize(600,800);
show();
}
public static void main(String[] args)
{
Frame1 frame1 = new Frame1();
}
}
|
简单,你只要把this.setBackground(c);改为以下就OK了, 哈哈..........
this.getContentPane().setBackground(c);
this.getContentPane().setBackground(c);
|
Javadoc里讲的很清楚。
The JFrame class is slightly incompatible with Frame. Like all other JFC/Swing top-level containers, a JFrame contains a JRootPane as its only child. The content pane provided by the root pane should, as a rule, contain all the non-menu components displayed by the JFrame. This is different from the AWT Frame case. For example, to add a child to an AWT frame you'd write:
frame.add(child);
However using JFrame you need to add the child to the JFrame's content pane instead:
frame.getContentPane().add(child);
The same is true for setting layout managers, removing components, listing children, and so on. All these methods should normally be sent to the content pane instead of the JFrame itself. The content pane will always be non-null. Attempting to set it to null will cause the JFrame to throw an exception. The default content pane will have a BorderLayout manager set on it.
The JFrame class is slightly incompatible with Frame. Like all other JFC/Swing top-level containers, a JFrame contains a JRootPane as its only child. The content pane provided by the root pane should, as a rule, contain all the non-menu components displayed by the JFrame. This is different from the AWT Frame case. For example, to add a child to an AWT frame you'd write:
frame.add(child);
However using JFrame you need to add the child to the JFrame's content pane instead:
frame.getContentPane().add(child);
The same is true for setting layout managers, removing components, listing children, and so on. All these methods should normally be sent to the content pane instead of the JFrame itself. The content pane will always be non-null. Attempting to set it to null will cause the JFrame to throw an exception. The default content pane will have a BorderLayout manager set on it.
|
为什么要在构造函数中调用repaint()方法??
应该有点问题吧,show()的时候系统会调用的啊。。
应该有点问题吧,show()的时候系统会调用的啊。。