我目前正在开发一个涉及数据库和 JavaFX 的项目。 但是,我希望用户在不同的场景中获得成功注册后的反馈。
假设用户注册,注册成功,因此用户被重定向到登录场景(Login.fxml)。这里我想要一个简单的标签来表示“您已成功注册”。
我该如何解决这个问题?线程是一个可行的选择吗?
提前致谢!
最佳答案
这是一个关于如何处理成功/失败状态的小示例。如果操作成功,则会弹出一条警报,说明情况。一旦用户单击“确定”按钮,您就应该加载您的 fxml。代码alert.showAndWait() 显示警报并等待用户按“确定”按钮或关闭警报。在alert.showAndWait()之后,您需要加载新的fxml。
Controller
import java.net.URL;
import java.util.*;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.control.Alert.AlertType;
/**
*
* @author blj0011
*/
public class FXMLDocumentController implements Initializable
{
@FXML
private Label label;
@FXML
private void handleButtonAction(ActionEvent event)
{
Random random = new Random();
boolean regSuccessful = random.nextBoolean();
if(regSuccessful)
{
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Registration Status:");
alert.setContentText("You have successfully registered!");
alert.showAndWait();
//load the fxml need here!
System.out.println("loading fxml!");
label.setText("loading fxml!");
}
else
{
Alert alert = new Alert(AlertType.WARNING);
alert.setTitle("Registration Status:");
alert.setContentText("Registration failed!");
alert.showAndWait();
label.setText("registration failed!");
}
}
@Override
public void initialize(URL url, ResourceBundle rb)
{
// TODO
}
}
FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication53.FXMLDocumentController">
<children>
<Button layoutX="126" layoutY="90" text="Click Me!" onAction="#handleButtonAction" fx:id="button" />
<Label layoutX="126" layoutY="120" minHeight="16" minWidth="69" fx:id="label" />
</children>
</AnchorPane>
Main
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* @author blj0011
*/
public class JavaFXApplication53 extends Application
{
@Override
public void start(Stage stage) throws Exception
{
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}
关于JavaFX - 注册成功的反馈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42926406/