标题:关于监听器的一个小问题!
只看楼主
HCL
Rank: 1
等 级:新手上路
帖 子:32
专家分:0
注 册:2007-6-13
 问题点数:0 回复次数:3 
关于监听器的一个小问题!

代码是这样的:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.image.*;

public class SwingConsole extends JFrame{
public static void run(JFrame j,String name,int width,int height){
j.setTitle(name);
j.setSize(width,height);
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j.setLocation(200,200);
j.setVisible(true);

}
}

class test extends JFrame{

public test(){
addWindowListener(new WindowAdapter(){
void windowActived(WindowEvent e){
repaint();
}
});
}
public void paint(Graphics g){
super.paint(g);

Graphics2D g2d = (Graphics2D)g;
g2d.setPaint(new GradientPaint(50,50,Color.RED,120,50,Color.GREEN,true));
g2d.fill(new Ellipse2D.Double(50,50,100,130));
g2d.setPaint(Color.red);
g2d.setStroke(new BasicStroke(20.0f));
g2d.draw( new Rectangle2D.Double(180,50,140,100));
}


public static void main(String[] argv)
{
SwingConsole.run(new test(),"come on!",400,400);
}
}

这是代码,运行没有问题,但是在使用Tab+Alt切换进程的时候图像就不能重画了,
请问该怎么做才可以使的再切换回来是能够重画呢!谢谢了

搜索更多相关主题的帖子: 监听 
2007-11-06 16:43
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
得分:0 
你的窗体每次从不可见到可见的时候,都会强制重画的

可惜不是你,陪我到最后
2007-11-07 09:25
netstriker
Rank: 1
等 级:新手上路
威 望:1
帖 子:257
专家分:0
注 册:2007-3-24
得分:0 

或者你这样做就可以了
import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;

public class test extends JFrame{
public void run(JFrame j,String name,int width,int height){
j.add(new google());
j.setTitle(name);
j.setSize(width,height);
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j.setLocation(200,200);
j.setVisible(true);
}
public static void main(String args[])
{
new test().run(new JFrame(), "come on", 400, 400);
}
}

class google extends JPanel{

JPanel panel;
public google(){
/* addWindowListener(new WindowAdapter(){
public void windowActived(WindowEvent e){
repaint();
}
});*/

/*this.addWindowStateListener(new WindowStateListener(){
public void windowStateChanged(WindowEvent e)
{
repaint();
}
});*/
}
/*public void paint(Graphics g){
super.paint(g);

/* Graphics2D g2d = (Graphics2D)g;
g2d.setPaint(new GradientPaint(50,50,Color.RED,120,50,Color.GREEN,true));
g2d.fill(new Ellipse2D.Double(50,50,100,130));
g2d.setPaint(Color.red);
g2d.setStroke(new BasicStroke(20.0f));
g2d.draw( new Rectangle2D.Double(180,50,140,100));
*/
//g.drawLine(10, 10, 100, 52);
/* g.setColor(Color.BLACK);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
}*/
public void paintComponent(Graphics g)
{
Graphics2D g2d = (Graphics2D)g;
g2d.setPaint(new GradientPaint(50,50,Color.RED,120,50,Color.GREEN,true));
g2d.fill(new Ellipse2D.Double(50,50,100,130));
g2d.setPaint(Color.red);
g2d.setStroke(new BasicStroke(20.0f));
g2d.draw( new Rectangle2D.Double(180,50,140,100));
}

}

2007-11-07 12:44
HCL
Rank: 1
等 级:新手上路
帖 子:32
专家分:0
注 册:2007-6-13
得分:0 

怪了,昨天用JCreator编译的时候可以用,改用CMD命令好像就不能了,但是把调用Main()方法的类改成Public后又行了,

而且会自己重绘,不管最小化或者切换之后!

是规定Main()必须放在public类里么?还是用JCreator的编译器有问题!

2007-11-07 16:45



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-183955-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.180535 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved