我儘量避免尋求幫助,盡我所能做我自己。但是我一直在研究這個問題太久了。我什至不明白什麼是錯的。哦,當我嘗試測試它時,我的GUI凍結創建簡單的秒錶 - javaFX
我必須做一個基本的秒錶:開始,停止,恢復和重置。但我無法做到。如果您還要評論或解釋您的代碼,請因爲我感到無能爲力。 (我的一些意見是過去的代碼,我以爲會工作,但沒有)
package gameclock;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class GameClock extends Application {
public void start(Stage primaryStage) {
HBox pane = new HBox();
Scene scene = new Scene(pane, 400, 100);
Button start = new Button("Start");
Button stop = new Button("Stop");
Button resume = new Button("Resume");
Button reset = new Button("Reset");
final TextField display = new TextField("0");
display.setEditable(false);
boolean onOff = false;
DateFormat df = new SimpleDateFormat("ss");
Calendar calobj = Calendar.getInstance();
System.out.println(df.format(calobj.getTime()));
start.setOnAction((ActionEvent event) ->
{
int time = Integer.parseInt(display.getText());
/*
Timer t = new Timer();
final int time1 = 1;
t.scheduleAtFixedRate(new TimerTask()
{
public void run()
{
time = time1 + 1;
}
},0,0);
display.setText(String.valueOf(time1));
*/
long timestamp = System.currentTimeMillis(); //divide by 1000 cause a millisecond is 1000 of a second
while (!onOff)
{
if(System.currentTimeMillis() - timestamp > 1000)
{
time++;
display.setText(String.valueOf(time));
}
}
String time2String = Long.toString(timestamp);
});
stop.setOnAction((ActionEvent event) ->
{
if(onOff == false)
onOff(true);
});
resume.setOnAction((ActionEvent event) ->
{
onOff(true);
});
pane.getChildren().addAll(start, display, resume, stop, reset);
pane.setAlignment(Pos.CENTER);
pane.setPadding(new Insets(0, 0, 0, 0));
primaryStage.setTitle("Stop Watch");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
private void onOff(boolean b) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
我知道循環是無止境的。我是這樣設計的。我希望它能夠在按下停止按鈕時將其從循環中取出。 –
這是問題所在。您的按鈕回調發生在您的應用程序的主線程上 - 由在調用堆棧底部的循環中運行的調度事件處理程序發出。如果您的按鈕事件處理程序未返回,則其他事件處理程序將無法運行。因此,您的UI線程在無限循環中凍結並且變得無響應。你想要做的是看到我關於UI定時器的更新答案。只要做一個互聯網搜索「Java定時器」或「JavaFX定時器」。你可能只需要一個計時器事件來每隔1秒觸發一次,你是否可以更新屏幕上的文本。 – selbie