Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Swing-单击按钮后绘制连续矩形_Java_Swing - Fatal编程技术网

Java Swing-单击按钮后绘制连续矩形

Java Swing-单击按钮后绘制连续矩形,java,swing,Java,Swing,我想在按下按钮后画一个矩形。当我第一次按下按钮时,它会画一个矩形。再次按下按钮后,我试图在第一个矩形附近绘制更多矩形,但没有绘制任何内容。有人能帮我吗 这是我使用的代码。多谢各位 您的paintComponent()只绘制一个矩形。它清除面板的背景,然后绘制矩形 如果需要多个矩形,则需要: 保留要绘制的矩形列表,然后每次遍历该列表并绘制矩形 在BuffereImage上绘制每个矩形,然后只绘制BuffereImage 查看这两种方法的工作示例。尝试这两种方法,看看您更喜欢哪一种。您的paint

我想在按下按钮后画一个矩形。当我第一次按下按钮时,它会画一个矩形。再次按下按钮后,我试图在第一个矩形附近绘制更多矩形,但没有绘制任何内容。有人能帮我吗

这是我使用的代码。多谢各位

您的paintComponent()只绘制一个矩形。它清除面板的背景,然后绘制矩形

如果需要多个矩形,则需要:

  • 保留要绘制的矩形列表,然后每次遍历该列表并绘制矩形

  • 在BuffereImage上绘制每个矩形,然后只绘制BuffereImage

  • 查看这两种方法的工作示例。尝试这两种方法,看看您更喜欢哪一种。

    您的paintComponent()只绘制一个矩形。它清除面板的背景,然后绘制矩形

    如果需要多个矩形,则需要:

  • 保留要绘制的矩形列表,然后每次遍历该列表并绘制矩形

  • 在BuffereImage上绘制每个矩形,然后只绘制BuffereImage

  • 查看这两种方法的工作示例。两种方法都试一下,看看你更喜欢哪一种

    class Coord{
        int x = 0;
        int y = 0;
    }
    public class DrawRectangle extends JPanel {
        int x, y, width, height;
    
        public DrawRectangle (int x, int y, int width, int height){
            this.x = x;
            this.y = y;
            this.width = width;
            this.height = height;
        }
    
        public Dimension getPreferredSize()
        {
            return new Dimension(this.width, this.height);
        }
    
        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            g2.setColor(Color.RED);
            g2.fillRect(this.x, this.y, this.width, this.height);
    
        }
    
        public static void main(String[] args)
        {
            final Coord coord = new Coord(); 
    
            final JPanel center = new JPanel();
            center.setLayout(null);
            center.setLocation(10, 10);
            center.setSize(300, 300);
            JButton button = new JButton("Button");
            button.setBounds(350,200,75,50); 
            button.addActionListener( new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
                {
                    DrawRectangle component = new DrawRectangle(coord.x, coord.y, 30, 30);
                    component.setLocation(coord.x, coord.y);
                    component.setSize(component.getPreferredSize());
                    center.add(component);
                    center.repaint();
                    coord.x += 30;
                    coord.y +=30;
                }
            });
    
            JFrame frame = new JFrame();
            frame.setLayout(null);
            frame.add(center);
            frame.add(button);
            frame.setSize(500, 500);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
    
    }