单击按钮时无法重新绘制圆圈
单击按钮时,我无法重新绘制窗口。当我单击按钮时应该发生的情况是应该在框架上绘制更多圆圈(从技术上讲,它应该是上次绘制的圆圈数 * 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不想第二次创建
new Roach()
。只需创建一个 roach 并保留对它的引用并在任何地方使用该引用即可。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.问题是您正在创建两个 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.