我想用swt写个像windows xp里的扫雷程序,但是我不能实现动态计时器。我做好了布局,也写了一个mytimer类,每隔一秒返回一个字符串,如01:00,01:01……,怎么让它显示在面板的label里? 我自己搞总是报线程访问错误,望高手指点!
package com.huan.mine;import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;import java.util.Timer;
import java.util.TimerTask;public class AppMine { private int showMineLength = (Constant.length + Constant.EDGE)
* Constant.row + Constant.SPACING * 2; private int shellWidth = showMineLength + Constant.WIDTH; private int shellHeight = showMineLength + Constant.HEIGHT; /**
* Launch the application
*
* @param args
*/
public static void main(String[] args) {
try {
AppMine window = new AppMine();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
} /**
* Open the window
*/
public void open() {
final Display display = Display.getDefault();
final Shell shell = new Shell(SWT.MIN | SWT.CLOSE);
shell.setLayout(new GridLayout());
shell.setSize(shellWidth, shellHeight);
shell.setText("扫雷");
shell.open(); ……
// 计数板块 BENGIN
class MyTimer implements Runnable {
private int second = 0; private int minute = 0; private Timer timer; private String time = null; private String stemp = null; private String mtemp = null; public TimerTask task = new TimerTask() { public void run() {
second++;
second = second % 60;
minute = second / 60; if (second >= 10)
stemp = new Integer(second).toString();
else
stemp = "0" + new Integer(second).toString(); if (minute >= 10)
mtemp = new Integer(minute).toString();
else
mtemp = "0" + new Integer(minute).toString(); time = mtemp + ":" + stemp;
}
}; public void run() {
timer = new Timer();
timer.schedule(task, 0, 1000); Composite showNum = new Composite(shell, SWT.BORDER);
showNum.setLayoutData(new GridData(GridData.FILL_BOTH));
showNum.setLayout(new GridLayout(5, false)); Label mineLabel = new Label(showNum, SWT.NONE);
mineLabel.setText(" " + new Integer(Constant.row).toString()
+ " ");// 雷的个数 new Label(showNum, SWT.NONE).setLayoutData(new GridData(
GridData.FILL_HORIZONTAL)); Button button = new Button(showNum, SWT.NONE);
button.setText("开始"); new Label(showNum, SWT.NONE).setLayoutData(new GridData(
GridData.FILL_HORIZONTAL)); Label timeLabel = new Label(showNum, SWT.BORDER);
timeLabel.setText(time);
System.out.println(time);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Display.getCurrent().syncExec(new MyTimer());
// timeLabel.setText(" ");
// MyTimer.show(timeLabel);// 耗时
// /////////////////////////////////////////////////////////
// 计数板块 END // 雷区板块 BENGIN
// 雷区面板布置
Composite showMine = new Composite(shell, SWT.BORDER);
showMine.setLayoutData(new GridData(showMineLength, showMineLength));
showMine.setBackground(Constant.COLOR);
final GridLayout gridLayout = new GridLayout(Constant.row, true);
gridLayout.verticalSpacing = 2;
gridLayout.horizontalSpacing = 2;
showMine.setLayout(gridLayout); // 布雷
Label[][] mineLabels = new Label[Constant.row][Constant.row];
Mine[][] mines = new Mine[Constant.row][Constant.row];
int i, j;
for (i = 0; i < Constant.row; i++) {
for (j = 0; j < Constant.row; j++) {
mineLabels[j] = new Label(showMine, SWT.NONE);
mineLabels[j].setLayoutData(new GridData(Constant.length,
Constant.length));
mines[j] = new Mine();
}
}
// 雷区板块 END shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}}就是计数板块的那一段有问题,还有就是Display.getCurrent().syncExec(new MyTimer())可以显示00:01,但是不变;我用asyncExec或者是timerExec就会报错,这又是为什么,高手过来帮忙啊!!!
package com.huan.mine;import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;import java.util.Timer;
import java.util.TimerTask;public class AppMine { private int showMineLength = (Constant.length + Constant.EDGE)
* Constant.row + Constant.SPACING * 2; private int shellWidth = showMineLength + Constant.WIDTH; private int shellHeight = showMineLength + Constant.HEIGHT; /**
* Launch the application
*
* @param args
*/
public static void main(String[] args) {
try {
AppMine window = new AppMine();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
} /**
* Open the window
*/
public void open() {
final Display display = Display.getDefault();
final Shell shell = new Shell(SWT.MIN | SWT.CLOSE);
shell.setLayout(new GridLayout());
shell.setSize(shellWidth, shellHeight);
shell.setText("扫雷");
shell.open(); ……
// 计数板块 BENGIN
class MyTimer implements Runnable {
private int second = 0; private int minute = 0; private Timer timer; private String time = null; private String stemp = null; private String mtemp = null; public TimerTask task = new TimerTask() { public void run() {
second++;
second = second % 60;
minute = second / 60; if (second >= 10)
stemp = new Integer(second).toString();
else
stemp = "0" + new Integer(second).toString(); if (minute >= 10)
mtemp = new Integer(minute).toString();
else
mtemp = "0" + new Integer(minute).toString(); time = mtemp + ":" + stemp;
}
}; public void run() {
timer = new Timer();
timer.schedule(task, 0, 1000); Composite showNum = new Composite(shell, SWT.BORDER);
showNum.setLayoutData(new GridData(GridData.FILL_BOTH));
showNum.setLayout(new GridLayout(5, false)); Label mineLabel = new Label(showNum, SWT.NONE);
mineLabel.setText(" " + new Integer(Constant.row).toString()
+ " ");// 雷的个数 new Label(showNum, SWT.NONE).setLayoutData(new GridData(
GridData.FILL_HORIZONTAL)); Button button = new Button(showNum, SWT.NONE);
button.setText("开始"); new Label(showNum, SWT.NONE).setLayoutData(new GridData(
GridData.FILL_HORIZONTAL)); Label timeLabel = new Label(showNum, SWT.BORDER);
timeLabel.setText(time);
System.out.println(time);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Display.getCurrent().syncExec(new MyTimer());
// timeLabel.setText(" ");
// MyTimer.show(timeLabel);// 耗时
// /////////////////////////////////////////////////////////
// 计数板块 END // 雷区板块 BENGIN
// 雷区面板布置
Composite showMine = new Composite(shell, SWT.BORDER);
showMine.setLayoutData(new GridData(showMineLength, showMineLength));
showMine.setBackground(Constant.COLOR);
final GridLayout gridLayout = new GridLayout(Constant.row, true);
gridLayout.verticalSpacing = 2;
gridLayout.horizontalSpacing = 2;
showMine.setLayout(gridLayout); // 布雷
Label[][] mineLabels = new Label[Constant.row][Constant.row];
Mine[][] mines = new Mine[Constant.row][Constant.row];
int i, j;
for (i = 0; i < Constant.row; i++) {
for (j = 0; j < Constant.row; j++) {
mineLabels[j] = new Label(showMine, SWT.NONE);
mineLabels[j].setLayoutData(new GridData(Constant.length,
Constant.length));
mines[j] = new Mine();
}
}
// 雷区板块 END shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}}就是计数板块的那一段有问题,还有就是Display.getCurrent().syncExec(new MyTimer())可以显示00:01,但是不变;我用asyncExec或者是timerExec就会报错,这又是为什么,高手过来帮忙啊!!!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货