我在 JavaFX 中创建了一个登录表单,成功登录后它应该被隐藏,
我正在遵循从该视频成功登录后隐藏表单的代码,但对我不起作用 https://www.youtube.com/watch?v=DeK9DfXG5Tg
基本上这是代码 ((Node)(event.getSource())).getScene().getWindow().hide();
问题我没有收到任何错误来知道问题是什么?
这可能是Main
类
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
BorderPane root =
(BorderPane)FXMLLoader.load(getClass().getResource("/fx/Login.fxml"));
Scene scene = new Scene(root,310,180);
scene.getStylesheets().
add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
我的 Controller 类
public class LoginController {
@FXML
private TextField Usernamefield;
@FXML
private PasswordField Passwordfield;
@FXML
public void LoginButtonHandler(ActionEvent event){
String UserName = Usernamefield.getText().trim();
String PassWord = Passwordfield.getText().trim();
Session session =
HibernateUtil.getSessionFactory().getCurrentSession();
try {
session.beginTransaction();
String hql =" from User where Username=:UserName and Password=:PassWord";
Query query = session.createQuery(hql);
query.setParameter("UserName",UserName);
query.setParameter("PassWord",PassWord);
List ResultSet = query.getResultList();
if (ResultSet.size()==1){
SessionInfo sessioninfo = new SessionInfo();
sessioninfo.setUsername(UserName);
System.out.println("welcome "+ sessioninfo.getUsername());
System.out.println("opening dashboard");
/*****************************************************
* openining dashboard after user logged successfully*
*****************************************************/
try{
((Node)(event.getSource())).getScene().getWindow().hide();
FXMLLoader fxmlLoader = new
FXMLLoader(getClass().getResource("DashBoard.fxml"));
BorderPane root = (BorderPane) fxmlLoader.load();
Stage stage = new Stage();
//stage.initModality(Modality.APPLICATION_MODAL);
//stage.initStyle(StageStyle.UNDECORATED);
stage.setTitle("Welcome "+sessioninfo.getUsername());
stage.setScene(new Scene(root));
stage.show();
} catch(Exception e) {
e.printStackTrace();
}
}else
{
System.out.println("login faild");
}
System.out.println("current session statics"+session.getStatistics());
System.out.println(Usernamefield.getText());
System.out.println(Passwordfield.getText());
System.out.println("transaction status
:"+session.getTransaction().getStatus());
System.out.println("result size is :"+ResultSet.size());
}
catch (HibernateException e) {
if (session.getTransaction() != null) {
session.getTransaction().rollback();
e.printStackTrace();
}
}
finally {
session.close();
System.out.println("session is closed");
}
}
}
我的FXML
<BorderPane xmlns="http://javafx.com/javafx/8.0.111"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="fx.LoginController">
<center>
<GridPane minHeight="172.0" minWidth="300.0" prefHeight="172.0"
prefWidth="326.0" BorderPane.alignment="CENTER">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="228.0"
minWidth="10.0" prefWidth="65.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="406.0"
minWidth="100.0" prefWidth="261.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0"
vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0"
vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0"
vgrow="SOMETIMES" />
</rowConstraints>
<children>
<ImageView fitHeight="40.0" fitWidth="40.0" pickOnBounds="true"
preserveRatio="true">
<image>
<Image url="@../../resource/images/User-40.png" />
</image>
<GridPane.margin>
<Insets left="10.0" />
</GridPane.margin>
</ImageView>
<ImageView fitHeight="150.0" fitWidth="40.0"
pickOnBounds="true" preserveRatio="true" GridPane.rowIndex="1">
<image>
<Image url="@../../resource/images/Lock-40.png" />
</image>
<GridPane.margin>
<Insets left="10.0" />
</GridPane.margin>
</ImageView>
<PasswordField fx:id="Password" promptText="password"
GridPane.columnIndex="1" GridPane.rowIndex="1">
<GridPane.margin>
<Insets left="10.0" right="10.0" top="10.0" />
</GridPane.margin>
</PasswordField>
<Button mnemonicParsing="false" onAction="#Login" text="Login"
GridPane.columnIndex="1" GridPane.rowIndex="2">
<GridPane.margin>
<Insets left="190.0" />
</GridPane.margin>
</Button>
<TextField fx:id="Username" promptText="username"
GridPane.columnIndex="1">
<GridPane.margin>
<Insets left="10.0" right="10.0" />
</GridPane.margin></TextField>
</children>
<BorderPane.margin>
<Insets />
</BorderPane.margin>
<opaqueInsets>
<Insets />
</opaqueInsets>
</GridPane>
</center>
<top>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Air Book"
BorderPane.alignment="CENTER" />
</top>
</BorderPane>
最佳答案
您需要在 fxml 中指定处理程序的正确方法名称。由于该方法的名称是 LoginButtonHandler
, onAction
的正确值<Button>
的属性元素是 #LoginButtonHandler
:
<Button mnemonicParsing="false"
onAction="#LoginButtonHandler"
text="Login"
GridPane.columnIndex="1"
GridPane.rowIndex="2">
<GridPane.margin>
<Insets left="190.0" />
</GridPane.margin>
</Button>
关于java - 登录成功后无法隐藏我的登录表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44116423/