我正在创建一个登录,它包括输入用户名和密码,然后当我单击验证时,登录关闭并打开一个新窗口(我将在其中包含我尚未实现的功能)......但是这个新窗口有一个注销按钮,我怎样才能使单击该按钮返回登录?
我一直在寻找解决方案,但一切似乎都太混乱了,我已经看到我可以在同一个 Stage 中加载所有内容,只需更改场景,但我认为它很复杂,我不明白它是如何做到的...,这是我的代码。
主班
package mainPack;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class mainC extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("/GUIs/Login.fxml"));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
}
登录控制器
package GUIs;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.event.ActionEvent;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;
public class LoginController implements Initializable {
/**
* Initializes the controller class.
*/
@FXML PasswordField txtPass;
@FXML TextField txtUser;
@FXML Label lblError;
@FXML Label lblStatus;
@FXML
public void btnHandle(ActionEvent e) throws Exception{
if(txtUser.getText().equals("Sistemas") && txtPass.getText().equals("2019")){
lblError.setOpacity(0);
lblStatus.setText("Conectado.");
Stage stage = new Stage();
Parent root = FXMLLoader.load(getClass().getResource("/GUIs/Principal.fxml"));
Scene escena = new Scene(root);
stage.setScene(escena);
stage.show();
//en esta linea, esconde el Stage del Login y carga el nuevo stage
( (Node) (e.getSource() ) ).getScene().getWindow().hide();
}else{
lblStatus.setText("Desconectado.");
lblError.setOpacity(1);
}
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
WindowPrincipalController(登录后)
package GUIs;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
/**
* FXML Controller class
*
* @author VAPESIN
*/
public class PrincipalController implements Initializable {
/**
* Initializes the controller class.
*/
@FXML
public void cerrarSesHandle(ActionEvent e){
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
登录.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.net.URL?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="493.0" styleClass="mainFxmlClass" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="GUIs.LoginController">
<stylesheets>
<URL value="@login.css" />
</stylesheets>
<children>
<Label layoutX="49.0" layoutY="71.0" prefHeight="46.0" prefWidth="52.0" text="Usuario" />
<Label layoutX="49.0" layoutY="148.0" prefHeight="46.0" prefWidth="66.0" text="Contraseña" />
<Button layoutX="145.0" layoutY="252.0" mnemonicParsing="false" onAction="#btnHandle" text="Validar" />
<TextField fx:id="txtUser" layoutX="145.0" layoutY="82.0" />
<PasswordField fx:id="txtPass" layoutX="145.0" layoutY="159.0" />
<Label fx:id="lblError" layoutX="74.0" layoutY="301.0" opacity="0.0" prefHeight="46.0" prefWidth="345.0" text="Usuario o Contraseña incorrecta." textFill="RED">
<font>
<Font size="17.0" />
</font>
</Label>
<Label fx:id="lblStatus" layoutX="101.0" layoutY="25.0" prefHeight="46.0" prefWidth="330.0" text="Desconectado" />
</children>
</AnchorPane>
主窗口.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.net.URL?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" styleClass="mainFxmlClass" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/10.0.1" fx:controller="GUIs.PrincipalController">
<stylesheets>
<URL value="@principal.css" />
</stylesheets>
<children>
<Label layoutX="167.0" layoutY="77.0" prefHeight="103.0" prefWidth="271.0" text="Bienvenido">
<font>
<Font size="53.0" />
</font>
</Label>
<Button layoutX="438.0" layoutY="345.0" mnemonicParsing="false" onAction="#cerrarSesHandle" prefHeight="25.0" prefWidth="142.0" text="Cerrar sesión" />
</children>
</AnchorPane>
单击关闭时,我希望它返回登录。
你好,你可以在我学校的系统上查看我在 github 上的代码,该系统使用注销实现登录并返回注销,我记得我遇到了问题,因为如果你想查看我的https代码,我对以前的数据感到惊讶://github.com/WihoverEsal/ProyectoAsesorias在 java FX 中,看看我在 github 上这部分的代码![在此处输入图像描述](https://i.stack.imgur.com/3QgtD.png)
我有这个代码,我在我的按钮中制作了关闭会话并将您返回到登录,这是您首先看到的我从那里创建它的 setOnAction 我为字母添加了一些样式,这部分用于关闭按钮会话,当我想要它返回到我想要在最后一部分的方法中的窗口时,我们将窗口的构造函数放在我们希望它返回的位置,这就是我所希望的,它对你有用