java当窗体获得焦点时,重画,怎么搞
2个回答
展开全部
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class MyPanel extends JPanel{
protected void paintComponent(Graphics g){
//绘图代码。在面板上绘图
g.drawOval(30,30,60,100);
}
}
public class MyFrame extends JFrame{
MyPanel p = new MyPanel();
MyFrame(){
super("窗口焦点监听");
setBounds(200,200,600,400);
add(p);
addWindowFocusListener(new WindowAdapter(){//注册窗口焦点监听器
public void windowGainedFocus(WindowEvent e){
repaint();//重绘窗口,将使得窗口内的每个组件也重绘
}
public void windowLostFocus(WindowEvent e) {}//看你是否需要
});
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String []s){
new MyFrame();
}
}
import java.awt.*;
import java.awt.event.*;
class MyPanel extends JPanel{
protected void paintComponent(Graphics g){
//绘图代码。在面板上绘图
g.drawOval(30,30,60,100);
}
}
public class MyFrame extends JFrame{
MyPanel p = new MyPanel();
MyFrame(){
super("窗口焦点监听");
setBounds(200,200,600,400);
add(p);
addWindowFocusListener(new WindowAdapter(){//注册窗口焦点监听器
public void windowGainedFocus(WindowEvent e){
repaint();//重绘窗口,将使得窗口内的每个组件也重绘
}
public void windowLostFocus(WindowEvent e) {}//看你是否需要
});
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String []s){
new MyFrame();
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-01-22
展开全部
paint
update
paintComponent
这几个方法,覆盖其中一个
update
paintComponent
这几个方法,覆盖其中一个
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询