Java JPanel设计问题

Java JPanel设计问题,java,swing,graphics,jpanel,paintcomponent,Java,Swing,Graphics,Jpanel,Paintcomponent,我有一张表格,上面画的是鼠标点击事件中的椭圆形。这对我来说很好。圆圈是画的。但当我最小化窗体并再次将其最大化时,面板将刷新,圆圈将被删除(即面板保留为空白) 代码是: 我有一个JFrame,上面有一个名为jPanel1的Jpanel,在这个面板上画了圆圈 private void jPanel1MouseClicked(java.awt.event.MouseEvent evt) { count += 1; if (count <= clients) {

我有一张表格,上面画的是鼠标点击事件中的椭圆形。这对我来说很好。圆圈是画的。但当我最小化窗体并再次将其最大化时,面板将刷新,圆圈将被删除(即面板保留为空白)

代码是: 我有一个JFrame,上面有一个名为jPanel1的Jpanel,在这个面板上画了圆圈

private void jPanel1MouseClicked(java.awt.event.MouseEvent evt) {
        count += 1;
        if (count <= clients) {
            drawCircle(evt.getX() - (radius / 2), evt.getY() - (radius / 2));
        }
    }

    public void drawCircle(int x, int y) {
        Graphics g = jPanel1.getGraphics();
        g.drawOval(x - radius, y - radius, 2 * radius, 2 * radius);
        g.setColor(Color.BLACK);
        g.fillOval(x - radius, y - radius, 2 * radius, 2 * radius);
    }
private void jPanel1MouseClicked(java.awt.event.MouseEvent evt){
计数+=1;

if(count所有绘图必须在面板的paint方法中完成。因此,您必须在面板中重写此方法,并将绘图代码放在面板中

您不必在
JPanel的
paintComponent
方法之外显式调用绘图函数

您应该扩展
JPanel
并将
drawCircle
代码放入
paintComponent
方法中:

public class DrawCircleClass extends JPanel
{
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawOval(x - radius, y - radius, 2 * radius, 2 * radius);
        g.setColor(Color.BLACK);
        g.fillOval(x - radius, y - radius, 2 * radius, 2 * radius);
    }

}

当组件应该被重新绘制时,Swing将自动调用
paintComponent
方法(在最大化最小化窗口之后)

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

import javax.swing.*;



public class TempProject extends JPanel{
    /** Stores info about circles  */
    public ArrayList<CircleInfo> circles = new ArrayList<CircleInfo>();

    /** fields that were in example code */
    public int count = 0;
    public final int radius = 20;
    public final int clients = 20;

    public TempProject(){

        addMouseListener(new MouseAdapter(){

            @Override
            public void mouseClicked(MouseEvent evt) {
                count += 1;
                if (count <= clients) {
                        // Store info about the circle to draw
                    circles.add(new CircleInfo(evt.getX() - (radius / 2), evt.getY() - (radius / 2), radius));
                        // Tell swing to repaint asap
                    repaint();
                }
            }});
    }

    @Override
    public void paintComponent(Graphics g ) {
            super.paintComponent(g);

            //Iterates through saved circles and paints them
        for(CircleInfo circle : circles){
            g.drawOval(circle.x - circle.radius, circle.y - circle.radius, 2 * circle.radius, 2 * circle.radius);
            g.setColor(Color.BLACK);
            g.fillOval(circle.x - circle.radius, circle.y - circle.radius, 2 * circle.radius, 2 * circle.radius);
        }
    }

    public static void main(String args[])    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
                frame.setContentPane(new TempProject());  
                frame.setPreferredSize(new Dimension(400, 300));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    /** Simple class for storing Circle info */
    public static class CircleInfo{
        int x = 0;
        int y = 0;
        int radius = 0;

        public CircleInfo(int x, int y, int radius){
            this.x = x; this.y = y; this.radius = radius;
        }
    }

}
import java.awt.*;
导入java.awt.event.*;
导入java.util.ArrayList;
导入javax.swing.*;
公共类TempProject扩展了JPanel{
/**存储有关圆圈的信息*/
public ArrayList circles=new ArrayList();
/**示例代码中的字段*/
公共整数计数=0;
公共最终整数半径=20;
公共最终int客户=20;
公共工程({
addMouseListener(新的MouseAdapter(){
@凌驾
公共无效mouseClicked(MouseEvent evt){
计数+=1;

如果(count,当然你是指paintComponent(不是paint):-)当我单击绘制第二个圆时,第一个圆是不可见的。jPanel被重新设计。得到了解决方案..我正在paintComponent()中编写super.paintComponent()方法方法,这就是它被重新设计的原因。需要注意的是,如果不保存圆信息并从中重新绘制,则在调整大小时仍会出现丢失圆的问题(请参见我回答中的示例)。不过,您可能已经解决了这一问题。代码应该调用
super.paintComponent(g)
。如果它在没有它的情况下只能“工作”,那么它就被破坏了。归根结底,就是不使用getGraphics()(正如答案中已经提到的,重复只是为了强调:).@AndrewThompson我忘了添加
super.paintComponent
,直到我看到你对另一个答案的评论:哇!谢谢你让我保持诚实!很高兴我能帮上忙,哪怕只是在那一点上。@NickRippe真是一个非常好的答案。它真的非常有用