这是一个画点的程序,但是一旦失去焦点,被其他界面挡住,或者最小化,画好的点就会消失,为什么啊import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class D {
public static void main(String[] args){
new MyFrame("D");
}
}class MyFrame extends JFrame{
MyFrame(String s){
super(s);
this.setLayout(null);
this.setBounds(100,100,200,300); 
this.setBackground(Color.white );
this.setVisible(true);
this.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
int x= e.getX();
int y =e.getY();
Graphics g = getGraphics();
draw(g,x,y);
}
}
);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
setVisible(false);
System.exit(0);
}});
}public void draw(Graphics g,int x,int y){
g.setColor(Color.blue);
g.fillOval(x,y,10,10);
}
}
javaswing graphics

解决方案 »

  1.   

    因为每次被其他界面挡住,或者最小化的时候JFrame都要被从新画出来,从新画的时候,不会去调用你的draw方法所以没了
      

  2.   

    试试这个
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class D {
    public static void main(String[] args) {
    MyFrame panel = new MyFrame("D");
    JFrame frame = new JFrame();
    frame.add(panel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200, 300);
    frame.setVisible(true);

    }
    }class MyFrame extends JPanel{
    private int x=0;
    private int y = 0;
    MyFrame(String s) {
    this.requestFocus();
    this.setFocusable(true);
    this.setLayout(null);
    this.setBounds(100, 100, 200, 300);
    this.setBackground(Color.white);
    this.setVisible(true);
    this.addMouseListener(new MouseAdapter() {
    public void mousePressed(MouseEvent e) {
     x = e.getX();
     y = e.getY();
     paint(MyFrame.this.getGraphics());
     

    }
    });

    } @Override
    public void paint(Graphics g) {
    // TODO Auto-generated method stub

    g.setColor(Color.blue);
    g.clearRect(0, 0, 200, 300);
    g.fillOval(x, y, 10, 10);
    }
    }
      

  3.   

    一旦失去焦点,被其他界面挡住,或者最小化
    这些都破坏了已经存在的窗口,界面需要重新绘制,会调用该JFrame里面的contentPane中的paintComponent方法。
    这里我把每次draw的点记录下来,并重写paintCompont,每次在调用paintCompont的时候都重新绘制这些点即可
    import java.awt.*;
    import java.awt.event.*;
    import java.util.ArrayList;
    import java.util.Arrays;import javax.swing.*;
    public class D {
    public static void main(String[] args){
    new MyFrame("D");
    }
    }
     
    class MyFrame extends JFrame{
    MyFrame(String s){
    super(s);
    this.setLayout(null);
    this.setBounds(100,100,200,300); 
    this.setBackground(Color.white );
    this.setVisible(true);
    final MyComponent my= new MyComponent();setContentPane(my);
    this.addMouseListener(new MouseAdapter(){
    public void mousePressed(MouseEvent e){
    int x= e.getX();
    int y =e.getY();
    my.draw(my.getGraphics(),x,y);
    }
    }
    );
    this.addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent e){
    setVisible(false);
    System.exit(0);
    }});
    }
    class MyComponent extends JComponent{
    ArrayList<Point> p=new ArrayList<Point>(); 
    @Override
    public void paintComponent(Graphics g){
    for(Point point:p){
    g.setColor(Color.blue);
    g.fillOval((int)point.getX(),(int)point.getY(),10,10);

    }
    System.out.println(p);
    }
    public void draw(Graphics g,int x,int y){
    g.setColor(Color.blue);
    g.fillOval(x,y,10,10);
    p.add(new Point(x,y));
    }
    }
    }