【发布时间】:2014-03-28 12:18:36
【问题描述】:
我正在尝试制作一个像这样工作的程序: 在 Window 类中,每次单击按钮时,都会调用 Panel 的方法 panel2:首先绘制第一个圆圈,然后绘制第二个圆圈(在计时器中定义的时间之后)。然后,我再次点击按钮,它正在画一个第一个圆圈,然后是第二个圆圈,然后是第三个圆圈。等等 问题是,当我单击以获得一个接一个出现的 3 个圆圈时,在上一步绘制的两个第一个圆圈(在我第二次按下按钮之前)停留在屏幕上,当我只绘制第三个圆圈按下按钮(而不是:绘制第一个圆圈,绘制第二个圆圈,绘制第三个圆圈)。我希望我很清楚。 这是一个简单的代码:
窗口
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Window extends JFrame implements ActionListener{
int h = 2;
Panel b = new Panel();
JPanel container = new JPanel();
JButton btn = new JButton("Start");
JButton bouton = new JButton();
Panel boutonPane = new Panel();
public Window(){
this.setTitle("Animation");
this.setSize(300, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
container.setBackground(Color.white);
container.setLayout(new BorderLayout());
JPanel top = new JPanel();
btn.addActionListener(this);
top.add(btn);
container.add(top);
this.setContentPane(container);
this.setVisible(true);
}
public void window2(){
this.setTitle("ADHD");
this.setSize(1000,700);
this.setLocationRelativeTo(null);
if (h < 11){
boutonPane.panel2(h);
bouton.addActionListener(this);
boutonPane.add(bouton);
this.add(boutonPane);
this.setContentPane(boutonPane);
updateWindow2();
}
this.setVisible(true);
}
public void updateWindow2(){
boutonPane.panel2(h);
this.revalidate();
this.repaint();
}
public void actionPerformed(ActionEvent e){
if ((JButton) e.getSource() == btn){
System.out.println("pressed0");
window2();
}
if ((JButton) e.getSource() == bouton){
h++;
System.out.println("pressed" + h);
updateWindow2();
}
}
public static void main(String[] args){
Window w = new Window();
}
}
面板
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Panel extends JPanel implements ActionListener{
int m;
int u=0;
int lgi, lrgi;
int [] ta;
Timer timer1 = new Timer(300, this);
Panel(){
}
public void panel2(int n){
m=n;
ta = new int [n];
for(int it=0; it<m;it++){
ta[it]=100*it;
}
timer1.start();
}
public void paintComponent(Graphics gr){
super.paintComponent(gr);
gr.setColor(Color.red);
for(int i=0;i<m;i++){
gr.fillOval(ta[i],ta[i], 150, 150);
}
}
@Override
public void actionPerformed(ActionEvent arg0) {
if(u<m){
u++;
revalidate();
repaint();
}
}
}
【问题讨论】:
-
如果您的程序产生错误,您应该发布该错误消息以供所有人查看。
-
另外:请学习并遵循 Java 命名约定。方法和变量名称应以小写字母开头。还要给出有意义的变量名称。很难知道
ta,或u,或lg代表什么变量。 -
另外,当使用 for 循环遍历数组时,在条件语句中使用数组的长度字段来决定循环多少次。您当前的代码使用与
ta数组的长度无关的 int 变量u,导致您的代码从 for 循环中抛出 ArrayIndexOutOfBoundsException。 -
@peeskillet -- 你的答案去哪儿了?我对你的回答投的赞成票到哪里去了?
-
@Hovercraft -- 我更正了你告诉我的一切。对于变量
u,我放了这个,因为它使paintComponent用actionPerformed中的当前变量重新绘制(因为我只在paintComponent中绘制了一个)。
标签: java swing timer jpanel actionlistener