public static TextArea message_text=new TextArea();
final static String message_text_style="-fx-border-width: 5px;-fx-border-radius: 10px;-fx-border-style: solid;-fx-border-color: #ff7f7f;";
private static int timer;
public static void system_message(String what,int set_timer)
{
timer=set_timer;
message_text.setText(what);
message_text.setStyle("-fx-opacity: 1;"+message_text_style);
Platform.runLater(new Runnable()
{
public void run()
{
try
{
Thread.sleep(timer);
}
catch(InterruptedException ex)
{
}
message_text.setStyle("-fx-opacity: 0;"+message_text_style);
}
});
}
此代码旨在弹出一段消息,然后在一定时间后消失(以便系统事件可以传达给用户)。这是通过设置TextArea元素的不透明度以使其变为可见来实现的。 runLater块负责在一段时间后切换不透明度。
这是我调用此方法的示例:
Button save_as_pgn_button=new Button();
save_as_pgn_button.setText("Save as: ");
save_as_pgn_button.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {
String path=pgn_name_text.getText();
if(path.length()>0)
{
MyFile my_file=new MyFile(path);
calc_pgn();
my_file.content=pgn;
my_file.write_content();
Javachessgui.system_message("Saved to file: "+path+"\n\nContent: "+my_file.content,3000);
}
}
});
令人恐惧的是它有时会起作用,有时候不起作用。通常不是,但有时当我按下按钮时,它完全按预期工作。或者,如果我在调试模式下运行它并在system_message()之前切换断点然后跳过它,它总是有效!
发生了什么事?