当前位置: 代码迷 >> Java相关 >> 关于监听器的一个小问题!
  详细解决方案

关于监听器的一个小问题!

热度:126   发布时间:2007-11-06 16:43:39.0
关于监听器的一个小问题!

代码是这样的:

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切换进程的时候图像就不能重画了,
请问该怎么做才可以使的再切换回来是能够重画呢!谢谢了

搜索更多相关的解决方案: 监听  

----------------解决方案--------------------------------------------------------
你的窗体每次从不可见到可见的时候,都会强制重画的
----------------解决方案--------------------------------------------------------

或者你这样做就可以了
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));
}

}


----------------解决方案--------------------------------------------------------

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

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

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


----------------解决方案--------------------------------------------------------
  相关解决方案