2017-05-22 124 views
0

我有一個在JavaFX中創建的登錄表單,登錄成功後應該隱藏, 我遵循從此視頻成功登錄後用於隱藏表單的代碼,但不爲我工作https://www.youtube.com/watch?v=DeK9DfXG5Tg 基本上這是代碼((Node)(event.getSource())).getScene().getWindow().hide();成功登錄後無法隱藏我的登錄表

問題我沒有得到任何錯誤知道是什麼問題?

這是5月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); 

    } 
} 

我的控制器類

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> 

回答

2

您需要在FXML處理程序指定conrrect方法名。由於該方法的名稱爲LoginButtonHandler,對於<Button>元件的onAction屬性的正確值是#LoginButtonHandler

<Button mnemonicParsing="false" 
     onAction="#LoginButtonHandler" 
     text="Login" 
     GridPane.columnIndex="1" 
     GridPane.rowIndex="2"> 
    <GridPane.margin> 
     <Insets left="190.0" /> 
    </GridPane.margin> 
</Button> 
相關問題