下面是一段简单的代码,相信大家都能看懂:package thread.test;
import java.awt.*;
import javax.swing.*;public class test1 {
public static void main(String[] args) {
MyTest1 myTest = new MyTest1();
myTest.pack();
}
}class MyTest1 extends JFrame implements Runnable {
JButton button = new JButton("OK");
int x = 5;
Thread thread = null;
MyTest1() {
setBounds(100, 100, 200, 200);
setLayout(new FlowLayout());
setVisible(true);
add(button);
button.setBackground(Color.GREEN);
thread = new Thread(this);
thread.start();
}
public void run() {
while (true) {
x = x + 1;
if (x > 100)
x = 5;
System.out.println("x =" + x);
button.setBounds(40, 40, x, x);
try {
thread.sleep(2000);
} catch (InterruptedException e) {
}
}
}
}
想法是 每隔两秒钟窗口中的JButton控件改变大小,但实际效果不是这样的,需要改变窗口大小,里面JButton控件大小才会改变,突然感觉,while(true){}这句没有一直循环,因为运行的时候JButton控件大小没改变,然而,后台运行的x值却每隔两秒递增。
奇怪,x每隔两秒递增,为什么JButton控件大小未变???望能赐教,不甚感激!!!
import java.awt.*;
import javax.swing.*;public class test1 {
public static void main(String[] args) {
MyTest1 myTest = new MyTest1();
myTest.pack();
}
}class MyTest1 extends JFrame implements Runnable {
JButton button = new JButton("OK");
int x = 5;
Thread thread = null;
MyTest1() {
setBounds(100, 100, 200, 200);
setLayout(new FlowLayout());
setVisible(true);
add(button);
button.setBackground(Color.GREEN);
thread = new Thread(this);
thread.start();
}
public void run() {
while (true) {
x = x + 1;
if (x > 100)
x = 5;
System.out.println("x =" + x);
button.setBounds(40, 40, x, x);
try {
thread.sleep(2000);
} catch (InterruptedException e) {
}
}
}
}
想法是 每隔两秒钟窗口中的JButton控件改变大小,但实际效果不是这样的,需要改变窗口大小,里面JButton控件大小才会改变,突然感觉,while(true){}这句没有一直循环,因为运行的时候JButton控件大小没改变,然而,后台运行的x值却每隔两秒递增。
奇怪,x每隔两秒递增,为什么JButton控件大小未变???望能赐教,不甚感激!!!
import javax.swing.*;public class test1 {
public static void main(String[] args) {
MyTest1 myTest = new MyTest1(); }
}class MyTest1 extends JFrame implements Runnable { JButton button = new JButton("OK");
int x = 5;
Thread thread = null; MyTest1() {
setBounds(100, 100, 200, 200);
setLayout(new FlowLayout());
setVisible(true);
add(button);
button.setBackground(Color.GREEN);
setSize(300,400);
thread = new Thread(this);
thread.start();
} public void run() {
while (true) {
x = x + 1;
if (x > 100)
x = 5;
System.out.println("x =" + x);
button.setBounds(40, 40, x, x);
try {
thread.sleep(2000);
} catch (InterruptedException e) {
}
}
}
}
import javax.swing.*;public class test1 {
public static void main(String[] args) {
MyTest1 myTest = new MyTest1();
}
} class MyTest1 extends JFrame implements Runnable {
JButton button = new JButton("OK");
int x = 5;
Thread thread = null;
MyTest1() {
super("Test1");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout()); button.setBackground(Color.GREEN);
add(button); thread = new Thread(this);
thread.start(); setBounds(100, 100, 300, 400);
setVisible(true);
}
public void run() {
while (true) {
x = x + 1;
if (x > 100) x = 5;
System.out.println("x =" + x);
button.setBounds(40, 40, x, x);
try {
thread.sleep(2000);
} catch (InterruptedException e) {
}
}
}
}