为什么我的矩形在我的JPanel中以奇怪的形式显示?

时间:2019-03-14 05:31:27

标签: java jpanel

运行代码时,只有在JPanel的左上角单击鼠标时才会显示一个矩形。当它出现时,它会变形。如果我将fillRect更改为fillOval,它根本不会显示。我的目标是在单击鼠标时在鼠标的位置填充一个圆圈。我的代码是:

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class PaintProgram {
    JFrame frame;
    JPanel panel;
    Point p;
    int x;
    int y;
    int diameter;
    public PaintProgram(){
        frame = new JFrame();
        frame.setSize(500,500);
        panel = new JPanel();
        diameter = 100;
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        panel.addMouseListener(new MListener());
        panel.add(new DrawCircle());
        frame.add(panel);
        frame.setVisible(true);
    }

    public static void main(String[] args){
        PaintProgram p = new PaintProgram();
    }

    class UBListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent a){

        }
    }


    class MListener implements MouseListener{
        public void mousePressed(MouseEvent e){
            p = panel.getMousePosition();
            x = (int) p.getX();
            y = (int) p.getY();
            panel.add(new DrawCircle());
            frame.repaint();
            frame.revalidate();
            System.out.println(x + "," + y);
        }

        public void mouseReleased(MouseEvent e){
        }

        public void mouseClicked(MouseEvent e){
        }

        public void mouseExited(MouseEvent e){
        }

        public void mouseEntered(MouseEvent e){
        }
    }

    class DrawCircle extends JPanel{
        @Override
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            g.setColor(Color.orange);
            g.fillRect(x,y,diameter,diameter);
            System.out.println(x + "," + y);
        }
    }
}

1 个答案:

答案 0 :(得分:2)

我认为您的代码有几个问题。首先,在PaintProgram中定义的JPanel是不必要的,并且在代码中引起了一些问题。基本上,我已经将JFrame用于几乎所有内容。接下来,我不希望仅通过单击鼠标就可以在DrawCircle()中创建新的PaintProgram

以下代码对我有用(单击鼠标时在光标周围画一个圆,我认为这是您想要的)。我做了一些我没有提到的更改,请随时提出问题。

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

public class PaintProgram {
    JFrame frame;
    Point p;
    int x;
    int y;
    int diameter;
    public PaintProgram(){
        frame = new JFrame();
        frame.setSize(500,500);
        diameter = 100;
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.addMouseListener(new MListener());
        frame.setVisible(true);
    }

    public static void main(String[] args){
        PaintProgram p = new PaintProgram();
    }

    class UBListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent a){

        }
    }


    class MListener extends MouseAdapter {
       @Override
       public void mousePressed(MouseEvent e){
            p = frame.getMousePosition();
            x = (int) p.getX();
            y = (int) p.getY();
            frame.add(new DrawCircle());
            frame.repaint();
            frame.revalidate();
            System.out.println(x + "," + y);
        }
    }

    class DrawCircle extends JPanel{
        @Override
        public void paintComponent(Graphics g){
            g.setColor(Color.orange);
            g.fillOval(x - 56, y - 77, diameter, diameter);
            System.out.println(x + "," + y);
        }
    }
}

circle around pointer