下面这个程序运行,我点击一下Start按钮,程序已开始画图,但是按钮被按下去就不再起来了,请问如何解决这个问题?import java.util.*;
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.awt.Graphics;
import java.awt.Color;
import java.lang.Math.*;class DrawSquare4 extends JFrame implements ActionListener {
Frame f1;
Canvas cvs;
Graphics cvsimage, gh;
Image image;
JPanel btnpan;
JButton btnstart;
public DrawSquare4() {
this.setSize(800,800);
this.setVisible(true);
btnpan = new JPanel();
btnstart = new JButton("Start");
btnstart.addActionListener(this);
btnpan.add(btnstart, FlowLayout.LEFT);
this.getContentPane().add(btnpan, BorderLayout.NORTH);
cvs = new Canvas();
cvs.setSize(this.getSize().width, this.getSize().height);
this.getContentPane().add(cvs,BorderLayout.CENTER);
this.pack();
cvs.setBackground(Color.BLACK);
cvsimage = cvs.getGraphics();
cvsimage.setColor(Color.yellow);
image = createImage(this.getSize().width, this.getSize().height);
gh = image.getGraphics();}
void Start() {
while (true) {
int i = 0;
long start = System.currentTimeMillis();
while (i < 1000) {
gh.setColor(Color.black);
gh.fillRect(0,0,this.getSize().width, this.getSize().height);
gh.setColor(Color.BLUE);
gh.fillRect(0,0,i,i);
cvsimage.drawImage(image,0,0,null);
i = i + 1;
}
long end = System.currentTimeMillis();
System.out.println("时间是"+(end-start));
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnstart) {
Start();
}
}
}
public class Solar {
public static void main(String[] args) throws IOException {
JFrame dd= new DrawSquare4();
}
}
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.awt.Graphics;
import java.awt.Color;
import java.lang.Math.*;class DrawSquare4 extends JFrame implements ActionListener {
Frame f1;
Canvas cvs;
Graphics cvsimage, gh;
Image image;
JPanel btnpan;
JButton btnstart;
public DrawSquare4() {
this.setSize(800,800);
this.setVisible(true);
btnpan = new JPanel();
btnstart = new JButton("Start");
btnstart.addActionListener(this);
btnpan.add(btnstart, FlowLayout.LEFT);
this.getContentPane().add(btnpan, BorderLayout.NORTH);
cvs = new Canvas();
cvs.setSize(this.getSize().width, this.getSize().height);
this.getContentPane().add(cvs,BorderLayout.CENTER);
this.pack();
cvs.setBackground(Color.BLACK);
cvsimage = cvs.getGraphics();
cvsimage.setColor(Color.yellow);
image = createImage(this.getSize().width, this.getSize().height);
gh = image.getGraphics();}
void Start() {
while (true) {
int i = 0;
long start = System.currentTimeMillis();
while (i < 1000) {
gh.setColor(Color.black);
gh.fillRect(0,0,this.getSize().width, this.getSize().height);
gh.setColor(Color.BLUE);
gh.fillRect(0,0,i,i);
cvsimage.drawImage(image,0,0,null);
i = i + 1;
}
long end = System.currentTimeMillis();
System.out.println("时间是"+(end-start));
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnstart) {
Start();
}
}
}
public class Solar {
public static void main(String[] args) throws IOException {
JFrame dd= new DrawSquare4();
}
}
事件里面要启动一个子线程,让子线程去执行绘画的过程。此外,应该还需要解决GUI线程通讯问题。
是个死循环!
将actionPerformed方法改成如下代码就行了 public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnstart) {
new Thread(){
@Override
public void run(){
Start();
}
}.start();
}
}