所以我现在遇到了一个问题,因为一切都工作正常,并且只是在尝试一些代码来创建一个简单的系统时间和日期设置。我有一个问题似乎无法解决。因此,我已经识别了一个标签(不是 JLabel),当我尝试通过可运行的 void 运行它时,当我尝试设置文本以更新系统的确切时间时,它似乎出现错误。例如 Time.setText(time); time 将被识别为字符串,但它只是出错并且不起作用。
import java.util.Calendar;
import java.util.GregorianCalendar;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
public class HomeScreen {
protected Shell shell;
private Label Date;
private Label Time;
private Composite composite;
/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
try {
HomeScreen window = new HomeScreen();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
Date();
Time();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell();
shell.setSize(450, 300);
shell.setText("Project Serenity");
composite = new Composite(shell, SWT.NONE);
composite.setBounds(0, 0, 470, 278);
Time = new Label(composite, SWT.NONE);
Time.setLocation(10, 192);
Time.setSize(169, 31);
Time.setText("Time");
Date = new Label(composite, SWT.NONE);
Date.setLocation(10, 217);
Date.setSize(169, 38);
Date.setText("Date");
}
public void Date()
{
Thread Date2 = new Thread()
{
@Override
public void run()
{
try {
for(;;){
Calendar cal = new GregorianCalendar();
int day = cal.get(Calendar.DAY_OF_MONTH);
int month = cal.get(Calendar.MONTH);
int year = cal.get(Calendar.YEAR);
Time.setText(day + ", " + month + " " + year);
sleep(1000);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
Date2.start();
}
public void Time()
{
Thread Time2 = new Thread()
{
@Override
public void run()
{
try {
for(;;){
Calendar cal = new GregorianCalendar();
int second = cal.get(Calendar.SECOND);
int minute = cal.get(Calendar.MINUTE);
int hour = cal.get(Calendar.HOUR);
int am_pm = cal.get(Calendar.AM_PM);
sleep(1000);
Time.setText(hour + ":" + minute + ":" + second + " " + am_pm);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
Time2.start();
}
}
谢谢像素
最佳答案
对用户界面对象的所有访问都必须在用户界面线程中完成。尝试在任何其他线程中访问它们都会引发异常。
您可以使用Display
的asyncExec
方法在UI线程中执行Runnable
。因此,不要只是这样做:
Time.setText(day + ", " + month + " " + year);
在后台线程中执行以下操作:
Display.getDefault().asyncExec(() -> Time.setText(day + ", " + month + " " + year));
以上内容适用于 Java 8,如果您使用的是旧版本的 Java,请使用:
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run {
Time.setText(day + ", " + month + " " + year);
}
});
注意:请学习使用 Java 命名约定 - 变量以小写字母开头,因此 Time
应为 time
。
当您退出应用程序时,您也没有执行任何操作来停止更新线程。主 UI 线程停止后访问控件将会出现错误。您可以测试 Time
控件是否已释放,这将告诉您应用程序已停止。
if (Time.isDisposed()) {
// TODO exit the thread
}
关于java - 使用可运行程序时,为什么我的 Java SWT Gui 应用程序的标签没有更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36442823/