我有以下三个类: RNDM:
import java.util.*;
public class Rndm {
private static Random rn;
public static String randInt(int min, int max)
{
return Integer.toString((min + (int)(Math.random()*(max-min))));
}
public static String randInt(int max)
{
return Integer.toString(((int)(Math.random()*(max))));
}
public static int intRandInt(int min, int max)
{
return (min + (int)(Math.random()*(max-min)));
}
public static int intRandInt(int max)
{
return (int)(Math.random()*(max));
}
public static String randString(int length)
{
String randstr="";
for (int i=0; i<=length; i++)
{
int charset = 1 + (int)(Math.random()*3);
if (charset==1)
{
char randChar = (char) (48 + (int)(Math.random()*(57-48)));
randstr += randChar;
}
else if (charset==2)
{
char randChar = (char) (65 + (int)(Math.random()*(90-65)));
randstr += randChar;
}
else if (charset==3)
{
char randChar = (char) (97 + (int)(Math.random()*(122-97)));
randstr += randChar;
}
}
return randstr;
}
}
MovingWindow(一个移动的JFrame):
import java.awt.*;
import javax.swing.*;
import java.util.*;
public class MovingWindow implements Runnable {
private JFrame frame;
private Point location;
private Thread t;
private int x;
private int y;
private int increment = 15;
private int delay = 1;
public MovingWindow(String tName) {
t = new Thread(this, tName);
t.start();
}
public void run() {
drawFrame();
}
private void drawFrame() {
frame = new JFrame("Moving Window");
x = Rndm.intRandInt(70, 1000);
y = Rndm.intRandInt(70, 1000);
location = new Point(x, y);
frame.setSize(500, 50);
frame.setLocation(location);
frame.setVisible(true);
moveAround();
}
public void moveAround() {
while(true) {
int dir = Rndm.intRandInt(0, 9);
switch(dir) {
case 1:
quad1();
break;
case 2:
quad2();
break;
case 3:
quad3();
break;
case 4:
quad4();
break;
case 5:
quad5();
break;
case 6:
quad6();
break;
case 7:
quad7();
break;
case 8:
quad8();
break;
default:
}
}
}
public void quad1() {
while (x<1000 && y<1000) {
try { Thread.sleep(delay); } catch (java.lang.InterruptedException e) {}
setX(x+increment);
setY(y+increment);
}
}
public void quad2() {
while (x<1000 && y>0) {
try { Thread.sleep(delay); } catch (java.lang.InterruptedException e) {}
setX(x+increment);
setY(y-increment);
}
}
public void quad3() {
while (x>0 && y>0) {
try { Thread.sleep(delay); } catch (java.lang.InterruptedException e) {}
setX(x-increment);
setY(y-increment);
}
}
public void quad4() {
while (x>0 && y<1000) {
try { Thread.sleep(delay); } catch (java.lang.InterruptedException e) {}
setX(x-increment);
setY(y+increment);
}
}
public void quad5() {
while (x>0) {
setX(x-increment);
}
}
public void quad6() {
while (x<1000) {
setX(x+increment);
}
}
public void quad7() {
while (y>0) {
setY(y-increment);
}
}
public void quad8() {
while (y<1000) {
setY(y+increment);
}
}
public void setX(int x) {
this.x = x;
location = new Point(x, y);
frame.setLocation(location);
}
public void setY(int y) {
this.y = y;
location = new Point(x, y);
frame.setLocation(location);
}
}
和WindowManager(实例化MovingWindow对象):
import java.util.ArrayList;
public class WindowManager {
public static void main(String[] args) {
ArrayList<MovingWindow> windows = new ArrayList<MovingWindow>();
for (int i=0; i<Integer.parseInt(args[0]); i++) {
windows.add(new MovingWindow(Rndm.randString(10)));
}
}
}
程序按预期运行,直到创建多个帧。此时,除了一帧或两帧之外的所有帧都会大幅减速一段时间,恢复正常移动几秒钟,然后再开始缓慢移动。我有两个问题: 1:为什么会这样? 2:我该如何解决? 我在OSX Mavericks上运行java 7。 提前致谢
答案 0 :(得分:3)
您正在从主线程调用moveAround(),并且您正在从那里更改GUI。这是一个很大的禁忌。对GUI的所有更改都必须在事件调度线程或EDT上。
但是,你永远不应该在EDT上调用Thread.sleep(),所以你当前的方法是行不通的。
我会考虑使用Swing Timer。