单击按钮时无法重新绘制圆圈

发布于 2024-10-28 04:48:03 字数 1633 浏览 2 评论 0原文

单击按钮时,我无法重新绘制窗口。当我单击按钮时应该发生的情况是应该在框架上绘制更多圆圈(从技术上讲,它应该是上次绘制的圆圈数 * 2)。由于某种原因,它不起作用,我真的无法弄清楚为什么它不会重新绘制。这是我的代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Roaches {  
    public static void main(String[] args) {
        //Create the frame
        JFrame frame = new JFrame();
        //Set the frame's size
        frame.setSize(800, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Roaches!");
        //Create the button
        JButton button = new JButton("Multiply Roaches!");
        button.addActionListener(new Roach());
        JPanel buttonPanel = new JPanel();
        //Add the button to the panel
        buttonPanel.add(button);
        //Add the panel to the frame
        frame.add(buttonPanel, BorderLayout.SOUTH);
        //Add the roach panel to the frame
        frame.add(new Roach());
        //Make frame visible
        frame.setVisible(true);
    }
}
class Roach extends JComponent implements ActionListener {
    //Keep track of how many roaches to draw
    private int roachCount = 1;
    protected void paintComponent(Graphics g) {
        Graphics2D gr = (Graphics2D)g;
        for(int i = 0; i < roachCount; i++) {
            int x = 5 * i;
            int y = 5 * 1;
            gr.drawOval(x, y, 50, 50);
        }
        System.out.println("REPAINT"); //For testing
    }
    public void actionPerformed(ActionEvent e) {
        roachCount = roachCount * 2;
        repaint();
        System.out.println("CLICK!!!!"); //For testing
    }
}

我不知道我是否有事件或重新绘制的概念错误,因此任何帮助/指针将不胜感激。谢谢!

I am having trouble making my window repaint when clicking a button. What's supposed to happen when I click the button is more circles should be drawn on the frame (it should technically be the number of circles drawn last time * 2). For some reason, it's not working and I can't really figure out why it wont repaint. Here's my code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Roaches {  
    public static void main(String[] args) {
        //Create the frame
        JFrame frame = new JFrame();
        //Set the frame's size
        frame.setSize(800, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Roaches!");
        //Create the button
        JButton button = new JButton("Multiply Roaches!");
        button.addActionListener(new Roach());
        JPanel buttonPanel = new JPanel();
        //Add the button to the panel
        buttonPanel.add(button);
        //Add the panel to the frame
        frame.add(buttonPanel, BorderLayout.SOUTH);
        //Add the roach panel to the frame
        frame.add(new Roach());
        //Make frame visible
        frame.setVisible(true);
    }
}
class Roach extends JComponent implements ActionListener {
    //Keep track of how many roaches to draw
    private int roachCount = 1;
    protected void paintComponent(Graphics g) {
        Graphics2D gr = (Graphics2D)g;
        for(int i = 0; i < roachCount; i++) {
            int x = 5 * i;
            int y = 5 * 1;
            gr.drawOval(x, y, 50, 50);
        }
        System.out.println("REPAINT"); //For testing
    }
    public void actionPerformed(ActionEvent e) {
        roachCount = roachCount * 2;
        repaint();
        System.out.println("CLICK!!!!"); //For testing
    }
}

I don't know if I have the concept of events or repainting wrong, so any help/pointers would be appreciated. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遗心遗梦遗幸福 2024-11-04 04:48:03

您不想第二次创建new Roach()。只需创建一个 roach 并保留对它的引用并在任何地方使用该引用即可。

 public static void main(String[] args) {
        //Create the frame
        JFrame frame = new JFrame();
        //Set the frame's size
        frame.setSize(800, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Roaches!");
        //Create the button
        JButton button = new JButton("Multiply Roaches!");

        Roach roach = new Roach();

        button.addActionListener(roach);
        JPanel buttonPanel = new JPanel();
        //Add the button to the panel
        buttonPanel.add(button);
        //Add the panel to the frame
        frame.add(buttonPanel, BorderLayout.SOUTH);
        //Add the roach panel to the frame
        frame.add(roach);
        //Make frame visible
        frame.setVisible(true);
    }

You don't want to create a new Roach() the second time. Just create one roach and keep a reference to it and use the reference everywhere.

 public static void main(String[] args) {
        //Create the frame
        JFrame frame = new JFrame();
        //Set the frame's size
        frame.setSize(800, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Roaches!");
        //Create the button
        JButton button = new JButton("Multiply Roaches!");

        Roach roach = new Roach();

        button.addActionListener(roach);
        JPanel buttonPanel = new JPanel();
        //Add the button to the panel
        buttonPanel.add(button);
        //Add the panel to the frame
        frame.add(buttonPanel, BorderLayout.SOUTH);
        //Add the roach panel to the frame
        frame.add(roach);
        //Make frame visible
        frame.setVisible(true);
    }
梦情居士 2024-11-04 04:48:03

问题是您正在创建两个 Roach 实例。一个作为动作侦听器添加到 JButton,另一个添加到要绘制的框架。因为它们是不同的实例,所以绘制的 Roach 永远不会收到操作,因此计数始终为 1。

这实际上是一个很好的例子,说明了为什么我不喜欢在 gui 对象上实现侦听器接口的做法。

The issue is that you're creating two instances of Roach. One that you add as an action listener to your JButton and the other that you add to the frame to draw. Because they are different instances, the Roach that is drawn never receives an action and thus always has a count of 1.

This is actually a good example of why I dislike the practice of implementing listener interfaces on gui objects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文