我使用JFrame创建并显示一个窗口,并将其设置为非常基本的属性.
public FrameVertices( String sTitle, Graph mMap, int iMul ) {
super( sTitle );
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
setSize ( 300, 300 );
setLocation ( 600, 300 );
setVisible ( true);
this.iMul = iMul;
this.gGraph = mMap;
}
Run Code Online (Sandbox Code Playgroud)
然后我使用paint()方法在窗口内绘制一些东西.
问题是,当其他一些窗户覆盖我JFrame然后揭开它时,它的内容JFrame不会被重新绘制 - 除非我调整大小或最小/最大JFrame.
我错过了什么吗?
直接绘画是不好的做法JFrame.更好的方法是覆盖paintComponent()a JPanel 并添加JPanel 到JFrame:
Test.java:
public class Test extends JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Test().createUI();
}
});
}
void createUI() {
setSize(500,500);
getContentPane().add(new MyPanel());
setVisible(true);
}
}
Run Code Online (Sandbox Code Playgroud)
MyPanel.java:
class MyPanel extends JPanel {
@override
public void paintComponent(Graphics g) {
super.paintComponent(g);
//paint what you want here
g.drawString("Hello world",250,250);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,如果必须,我建议添加一个Window FocusListener并repaint()在JFrame实例引起关注时调用该实例:http://docs.oracle.com/javase/tutorial/uiswing/events/windowlistener.html
通过该方法windowGainedFocus(WindowEvent e)或windowStateChanged(WindowEvent e)或windowActivated(WindowEvent e)主叫repaint()在这些方法1然后将调用paint()方法.
| 归档时间: |
|
| 查看次数: |
637 次 |
| 最近记录: |