sách gpt4 ai đã đi

java - 通过单个类管理不同的 JavaFX 阶段

In lại 作者:行者123 更新时间:2023-12-01 09:07:03 28 4
mua khóa gpt4 Nike

我正在编写一个简单的 JavaFX 应用程序,它具有三个阶段:登录、注册 (Anmeldung) 和欢迎 (Anwendung). 抱歉采用德语命名!

我已经在 App 类中创建了每个舞台及其场景,在 Controller 类中创建了处理事件,并在 fxml 文件中进行了设计。我需要实现一个 MainApp 类,它必须管理 Login,AnmeldungAnwendung 窗口之间的通信。

MainApp 最初应启动一个 Login 窗口,然后在该窗口中,如果选中该复选框,则应通知 MainApp 并订购启动 Anmeldung 窗口。成功注册后,MainApp 应关闭 Anmeldung 窗口并再次显示 Login 窗口。在那里,当用户登录时,MainApp 应再次关闭 Login 窗口并命令启动 Anwendung 窗口。

nhập mô tả hình ảnh ở đây

我已经通过修改 LoginController 完成了 LoginAnmeldung 窗口之间的转换,这不应该在那里完成,必须在通过 MainApp 完成。

此外,该任务希望仅通过一次 launch(args) 来完成整个事情。

LoginController.java:

package controller;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Modality;
import javafx.stage.Stage;
import resources.Benutzer;

public class LoginController {

// private static MainApp mainApp;

@FXML
public TextField textFieldUserId;

@FXML
public PasswordField passwordFieldPasswort;

@FXML
public CheckBox checkBoxNeuAnmeldung;

@FXML
public Button buttonEinloggen;

private boolean neuAnmeldung = false;

// public void setCallBack(MainApp mainApp) {
// LoginController.mainApp = mainApp;
// }

@FXML
public void handleButtonEinloggenAction(ActionEvent event) throws Exception {
// Stage stage = (Stage) buttonEinloggen.getScene().getWindow();
if (neuAnmeldung == false) {
Benutzer benutzer = new Benutzer(textFieldUserId.getText(),
passwordFieldPasswort.getText());
Parent anwendungsScene = FXMLLoader
.load(getClass().getResource("/design/Anwendung.fxml"));
Stage anwendungsStage = new Stage();
((Node) (event.getSource())).getScene().getWindow().hide();
anwendungsStage.setScene(new Scene(anwendungsScene));
anwendungsStage.setTitle("Anmeldung");
anwendungsStage.show();
System.out.println(benutzer);

}

// stage.close();
System.out.println("Eingeloggt!");

}

@FXML
public void handleCheckBoxNeuAnmeldungAction(ActionEvent event)
ném Ngoại lệ {
if (checkBoxNeuAnmeldung.isSelected()) {
neuAnmeldung = true;
Parent anmeldungsScene = FXMLLoader
.load(getClass().getResource("/design/Anmeldung.fxml"));
Stage anmeldungsStage = new Stage();
anmeldungsStage.initModality(Modality.WINDOW_MODAL);
anmeldungsStage
.initOwner(((Node) (event.getSource())).getScene().getWindow());
anmeldungsStage.setScene(new Scene(anmeldungsScene));
anmeldungsStage.setTitle("Anmeldung");
anmeldungsStage.show();

} else
neuAnmeldung = false;
System.out.println("Neu-Anmeldung? " + neuAnmeldung);

}

}

AnmeldungsController.java:

package controller;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import resources.Benutzer;

public class AnmeldungsController {

// private static MainApp mainApp;

@FXML
public TextField textFieldUserId;

@FXML
public PasswordField passwordFieldPasswort;

@FXML
public PasswordField passwordFieldWiederholung;

@FXML
public Button buttonAnmelden;

// public void setCallBack(MainApp mainApp) {
// AnmeldungsController.mainApp = mainApp;
// }

@FXML
public void handleButtonAnmeldenAction(ActionEvent event) {
Stage stage = (Stage) buttonAnmelden.getScene().getWindow();

if (passwordFieldPasswort.getText()
.equals(passwordFieldWiederholung.getText())) {
Benutzer benutzer = new Benutzer(textFieldUserId.getText(),
passwordFieldPasswort.getText());
System.out.println(benutzer);
System.out.println("Angemeldet!");
stage.close();

} khác {
textFieldUserId.setText("Passwörter stimmen nicht überein!");
System.out.println("Passwörter stimmen nicht überein!");
System.out.println(passwordFieldPasswort.getText() + " != "
+ passwordFieldWiederholung.getText());

}

}

}

AnwendungsController.java:

package controller;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.stage.Stage;

public class AnwendungsController {

// private static MainApp mainApp;

@FXML
public Button buttonSchliessen;

// public void setCallBack(MainApp mainApp) {
// AnwendungsController.mainApp = mainApp;
// }

@FXML
public void handleButtonAbbrechenAction(ActionEvent event) {
Stage stage = (Stage) buttonSchliessen.getScene().getWindow();
stage.close();
System.out.println("Fenster Geschlossen!");

}

}

MainApp.java:

package application;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class MainApp extends Application {

public static void main(String[] args) {
launch(args);
}

@Ghi đè
public void start(Stage primaryStage) throws Exception {
FXMLLoader loader = new FXMLLoader(
getClass().getResource("/design/Login.fxml"));
Parent root = loader.load();
primaryStage.setTitle("Benutzerverwaltung");
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
}

当然,程序的其他方面(例如异常和创建/读取/删除用户等)超出了这个问题的范围!

1 Câu trả lời

以下是您应该入门的基本想法:

public class LoginController {

private final ReadOnlyBooleanWrapper loggedIn = new ReadOnlyBooleanWrapper();

public ReadOnlyBooleanProperty loggedInProperty() {
return loggedIn.getReadOnlyProperty() ;
}

public final boolean isLoggedIn() {
return loggedInProperty().get();
}


@FXML
public TextField textFieldUserId;

@FXML
public PasswordField passwordFieldPasswort;

@FXML
public CheckBox checkBoxNeuAnmeldung;

@FXML
public Button buttonEinloggen;

private boolean neuAnmeldung = false;


@FXML
public void handleButtonEinloggenAction(ActionEvent event) throws Exception {

// assuming you verify the login credentials...
loggedIn.set(true);

System.out.println("Eingeloggt!");

}

}

现在在您的 MainApp 中您可以执行以下操作:

package application;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class MainApp extends Application {

public static void main(String[] args) {
launch(args);
}

@Ghi đè
public void start(Stage primaryStage) throws Exception {
FXMLLoader loader = new FXMLLoader(
getClass().getResource("/design/Login.fxml"));
Parent root = loader.load();

LoginController loginController = loader.getController();

loginController.loggedInProperty().addListener((obs, wasLoggedIn, isNowLoggedIn) -> {
if (isNowLoggedIn) {
// user is now logged in, show welcome screen...
}
});

primaryStage.setTitle("Benutzerverwaltung");
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
}

关于java - 通过单个类管理不同的 JavaFX 阶段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41187591/

28 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com