下面这个程序我用SWT写就不行,但是用swing就可以,为什么啊?
多谢高手们了
import java.util.Timer;
import java.util.TimerTask;import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;public class MyTimerTest { /**
* Launch the application
* @param args
*/
public static void main(String[] args) {
final Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setLayout(new FillLayout());
shell.setSize(500, 375);
shell.setText("SWT Application");
// shell.open(); final Composite composite = new Composite(shell, SWT.NONE); final Label label = new Label(composite, SWT.NONE);
// label.setText("Label");
Timer timer=new Timer();
TimerTask myTimer=new MyTimer(label);
timer.schedule(myTimer, 0, 1000);
label.setBounds(33, 159, 120, 30);
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}}import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimerTask;import org.eclipse.swt.widgets.Label;public class MyTimer extends TimerTask{
Label label;
/**
* @param args
*/
public MyTimer(Label label){
this.label=label;
}
@Override
public void run() {
while(true){
try{
Date date=new Date();
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
label.setText(df.format(date));
}catch(Exception e){
e.printStackTrace();
}
}
}
}
多谢高手们了
import java.util.Timer;
import java.util.TimerTask;import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;public class MyTimerTest { /**
* Launch the application
* @param args
*/
public static void main(String[] args) {
final Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setLayout(new FillLayout());
shell.setSize(500, 375);
shell.setText("SWT Application");
// shell.open(); final Composite composite = new Composite(shell, SWT.NONE); final Label label = new Label(composite, SWT.NONE);
// label.setText("Label");
Timer timer=new Timer();
TimerTask myTimer=new MyTimer(label);
timer.schedule(myTimer, 0, 1000);
label.setBounds(33, 159, 120, 30);
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}}import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimerTask;import org.eclipse.swt.widgets.Label;public class MyTimer extends TimerTask{
Label label;
/**
* @param args
*/
public MyTimer(Label label){
this.label=label;
}
@Override
public void run() {
while(true){
try{
Date date=new Date();
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
label.setText(df.format(date));
}catch(Exception e){
e.printStackTrace();
}
}
}
}
Display.getCurrent().asyncExec(new Runnable() {
//定义一个myTimerThread
public void run() {
while(true){
Date date=new Date();
……
label.setText(……);
}
}
});
public void run() {
while(true){
Date date=new Date();
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
label_3.setText(df.format(date));
try{
Thread.sleep(1000);
}catch(Exception e){
e.printStackTrace();
}
}
}
});
我直接这么写的行不行啊?
运行以后就没有反映了,不过非用户线程还是创建了
import java.util.Date;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;public class MyTimerTest { /**
* Launch the application
* @param args
*/
public static void main(String[] args) {
final Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setLayout(new FillLayout());
shell.setSize(500, 375);
shell.setText("SWT Application");
shell.open(); final Composite composite = new Composite(shell, SWT.NONE); final Label label = new Label(composite, SWT.NONE);
label.setBounds(33, 159, 200, 30);
shell.layout();
while (!shell.isDisposed()) {
display.syncExec(new Thread(){
public void run() {
Date date=new Date();
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
label.setText(df.format(date));
}
});
if (!display.readAndDispatch()){
try {
Thread.sleep(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
有没有好点的办法啊
只是显示一个时间就那样不好吧
谢谢了
可以把QQ留下吗
我是新学的