首页上一页 1 下一页尾页 1 条记录 1/1页
《JAVA开发实战宝典》第十一章
发表在Java图书答疑
2010-04-05
是否精华
是
否
版块置顶:
是
否
在这本书中P286有这样一个例子是开发一个窗体,通过线程,动态缩放按钮组件大小,它的完整的实例代码是这样的:
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Point;
import javax.swing.JButton;
import javax.swing.JFrame;
/**
* 按钮缩放
* @author 李钟尉
*/
public class ButtonZoom extends JFrame implements Runnable {
private JButton button;
private int x;
private int y;
private int width;
private int height;
private boolean zoomIn = true;
/**
* Launch the application
* @param args
*/
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ButtonZoom frame = new ButtonZoom();
frame.setVisible(true);
new Thread(frame).start();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame
*/
public ButtonZoom() {
super();
setResizable(false);
getContentPane().setLayout(null);
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button = new JButton();
button.setText("一个按钮");
getContentPane().add(button);
button.setLocation(getWidth() / 2, getHeight() / 2);
}
@Override
public void run() {
while (true) {
int fwidth = getWidth();
int fheight = getHeight();
Point point = button.getLocation();
Dimension size = button.getSize();
int len = Math.min(fwidth, fheight)/2;
for (int i = 0; i < len; i++) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (zoomIn) {
x = point.x - i;
y = point.y - i;
width = size.width + i * 2;
height = size.height + i * 2;
} else {
x = point.x + i;
y = point.y + i;
width = size.width - i * 2;
height = size.height - i * 2;
}
if (width >= fwidth || height >= fheight)
zoomIn = !zoomIn;
if(x<0||y<0){
zoomIn = !zoomIn;
}
System.out.println(x + "\t" + y + "\t" + width + "\t" + height);
EventQueue.invokeLater(new Runnable() {
public void run() {
button.setLocation(x, y);
button.setSize(width, height);
}
});
}
zoomIn = !zoomIn;
}
}
可是当我读到这段代码是我始终不能理解这段代码是:
while (true) {
int fwidth = getWidth();
int fheight = getHeight();
Point point = button.getLocation();
Dimension size = button.getSize();
int len = Math.min(fwidth, fheight)/2;
for (int i = 0; i < len; i++) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (zoomIn) {
x = point.x - i;
y = point.y - i;
width = size.width + i * 2;
height = size.height + i * 2;
} else {
x = point.x + i;
y = point.y + i;
width = size.width - i * 2;
height = size.height - i * 2;
}
if (width >= fwidth || height >= fheight)
zoomIn = !zoomIn;
if(x<0||y<0){
zoomIn = !zoomIn;
}
System.out.println(x + "\t" + y + "\t" + width + "\t" + height);
EventQueue.invokeLater(new Runnable() {
public void run() {
button.setLocation(x, y);
button.setSize(width, height);
}
});
}
zoomIn = !zoomIn;
}
能不能解释一下呀?非常感谢!
}
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Point;
import javax.swing.JButton;
import javax.swing.JFrame;
/**
* 按钮缩放
* @author 李钟尉
*/
public class ButtonZoom extends JFrame implements Runnable {
private JButton button;
private int x;
private int y;
private int width;
private int height;
private boolean zoomIn = true;
/**
* Launch the application
* @param args
*/
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ButtonZoom frame = new ButtonZoom();
frame.setVisible(true);
new Thread(frame).start();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame
*/
public ButtonZoom() {
super();
setResizable(false);
getContentPane().setLayout(null);
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button = new JButton();
button.setText("一个按钮");
getContentPane().add(button);
button.setLocation(getWidth() / 2, getHeight() / 2);
}
@Override
public void run() {
while (true) {
int fwidth = getWidth();
int fheight = getHeight();
Point point = button.getLocation();
Dimension size = button.getSize();
int len = Math.min(fwidth, fheight)/2;
for (int i = 0; i < len; i++) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (zoomIn) {
x = point.x - i;
y = point.y - i;
width = size.width + i * 2;
height = size.height + i * 2;
} else {
x = point.x + i;
y = point.y + i;
width = size.width - i * 2;
height = size.height - i * 2;
}
if (width >= fwidth || height >= fheight)
zoomIn = !zoomIn;
if(x<0||y<0){
zoomIn = !zoomIn;
}
System.out.println(x + "\t" + y + "\t" + width + "\t" + height);
EventQueue.invokeLater(new Runnable() {
public void run() {
button.setLocation(x, y);
button.setSize(width, height);
}
});
}
zoomIn = !zoomIn;
}
}
可是当我读到这段代码是我始终不能理解这段代码是:
while (true) {
int fwidth = getWidth();
int fheight = getHeight();
Point point = button.getLocation();
Dimension size = button.getSize();
int len = Math.min(fwidth, fheight)/2;
for (int i = 0; i < len; i++) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (zoomIn) {
x = point.x - i;
y = point.y - i;
width = size.width + i * 2;
height = size.height + i * 2;
} else {
x = point.x + i;
y = point.y + i;
width = size.width - i * 2;
height = size.height - i * 2;
}
if (width >= fwidth || height >= fheight)
zoomIn = !zoomIn;
if(x<0||y<0){
zoomIn = !zoomIn;
}
System.out.println(x + "\t" + y + "\t" + width + "\t" + height);
EventQueue.invokeLater(new Runnable() {
public void run() {
button.setLocation(x, y);
button.setSize(width, height);
}
});
}
zoomIn = !zoomIn;
}
能不能解释一下呀?非常感谢!
}