如何在最小化框架Java时避免组件消失

时间:2015-12-06 16:09:57

标签: java

以下代码在特定位置画一个圆圈,但我注意到当我最大化或最小化框架时,圆圈在框架最小化的情况下消失,当我最大化框架时它不移动。

SimpleFrame类

import java.awt.*;
import javax.swing.JFrame;

public class SimpleFrame extends JFrame{

    public SimpleFrame(){
        setTitle("Test");
        setSize(WIDTH,HEIGHT);

        Container con = this.getContentPane();
        draw = new MyPanel();
        con.add(draw);
    }

    public static final int WIDTH = 300;
    public static final int HEIGHT = 200;

    private MyPanel draw;
}

MyPanel课程:

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

public class MyPanel extends JPanel{
    public MyPanel(){
        setBackground(Color.WHITE);
    }

    //override the default paintComponent method in JComponent
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g; //Drawing shapes using the Java2D library requires a Graphics2D object
        g2.setStroke(new BasicStroke(5));
        g2.setPaint(new Color(128,0,0));
        Shape[] shapeList = new Shape[3];
        //shapeList[0] = new Rectangle2D.Double(20.0,20.0,40.0,70.0);
        shapeList[1] = new Ellipse2D.Double(70.0,10.0,60.0,60.0);
        g2.draw(shapeList[1]);
    }
}

SimpleFrameTest类:

import javax.swing.JFrame;

public class SimpleFrameTest {
    public static void main(String[] args){
        JFrame frm = new SimpleFrame();
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frm.setVisible(true);
    }
}

我该如何解决这个问题?

0 个答案:

没有答案