2017-03-15 63 views
0

我是javafx的新手,我需要您的帮助。我在互联网上搜索,但没有成功......所以,当我第一次使用jar时,我正在尝试使对话框出现。要做到这一点,我试图测试一个文件中是否存在某个属性,如果是这样,请启动“首次对话框”。当我在这个对话框上点击“确定”时,我将把属性保存到文件中。唯一的问题,primaryStage不是第一次是“主窗口”。这个对话框有一个控制器,所以当我在按钮的“setOnAction方法”中时,我无法启动primaryStage。我不能使用“node.getWindow.getScene”(或类似的东西)方法,因为它还没有打开,用户仍然在“第一时间窗口”。这里是我的代码..从JavaFX的Main类中显示一个舞台

public void start(Stage primaryStage) throws Exception{ 

    Parent root = FXMLLoader.load(getClass().getResource("real.fxml")); 
    primaryStage.setTitle("LoL Ready To Win"); 
    primaryStage.setResizable(false); 
    primaryStage.setScene(new Scene(root)); 

    try { 

     input = new FileInputStream("config.properties"); 
     prop.load(input); 

     if(prop.getProperty("name") != null){ 
      primaryStage.show(); 
     }else if(prop.getProperty("name") == null){ 

      try { 
       Stage stage = new Stage(); 
       Parent root1 = FXMLLoader.load(getClass().getResource("summ.fxml")); 
       stage.setTitle("Insert Title Here"); 
       stage.setResizable(false); 
       stage.setScene(new Scene(root1)); 
       stage.show(); 
      }catch(IOException e){ 
       e.printStackTrace(); 
      } 
     } 


    } catch (IOException ex) { 
     ex.printStackTrace(); 
    } finally { 
     if (input != null) { 
      try { 
       input.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

这是控制器

submit.setOnAction(e->{ 
    if (summfield.getText() == null || summfield.getText().trim().isEmpty()) { 
     Alert alert = new Alert(Alert.AlertType.INFORMATION); 
     alert.setTitle("Temp title"); 
     alert.setHeaderText(null); 
     alert.setContentText("Please enter a valid name!"); 
     alert.showAndWait(); 
    }else{ 
     try { 

      output = new FileOutputStream("config.properties"); 

      prop.setProperty("name", summfield.getText()); 

      prop.store(output, null); 

     } catch (IOException io) { 
      io.printStackTrace(); 
     } finally { 
      if (output != null) { 
       try { 
        output.close(); 
       } catch (IOException a) { 
        a.printStackTrace(); 
       } 
      } 

     } 

     // And this is where I wanna launch the primaryStage 
    } 

}); 

如果你有,即使它不是一个回答我的问题的任何意见,我不胜感激!谢谢大家。

回答

0

在你的控制器summ.fxml,做到:

public class SummController { 

    private String name ; 

    public String getName() { 
     return name ; 
    } 

    public void initialize() { 

     submit.setOnAction(e->{ 
      if (summfield.getText() == null || summfield.getText().trim().isEmpty()) { 
       Alert alert = new Alert(Alert.AlertType.INFORMATION); 
       alert.setTitle("Temp title"); 
       alert.setHeaderText(null); 
       alert.setContentText("Please enter a valid name!"); 
       alert.showAndWait(); 
      }else{ 
       try { 

        name = summfield.getText(); 

        output = new FileOutputStream("config.properties"); 

        prop.setProperty("name", name); 

        prop.store(output, null); 

        submit.getScene().getWindow().hide(); 

       } catch (IOException io) { 
        io.printStackTrace(); 
       } finally { 
        if (output != null) { 
         try { 
          output.close(); 
         } catch (IOException a) { 
          a.printStackTrace(); 
         } 
        } 

       } 

      } 

     }); 
    } 

} 

现在修改start方法为:

public void start(Stage primaryStage) throws Exception{ 

    Parent root = FXMLLoader.load(getClass().getResource("real.fxml")); 
    primaryStage.setTitle("LoL Ready To Win"); 
    primaryStage.setResizable(false); 
    primaryStage.setScene(new Scene(root)); 

    try { 

     input = new FileInputStream("config.properties"); 
     prop.load(input); 

     if(prop.getProperty("name") != null){ 
      primaryStage.show(); 
     }else if(prop.getProperty("name") == null){ 

      try { 
       Stage stage = new Stage(); 

       FXMLLoader loader = new FXMLLoader(getClass().getResource("summ.fxml")); 
       Parent root1 = loader.load(); 

       stage.setTitle("Insert Title Here"); 
       stage.setResizable(false); 
       stage.setScene(new Scene(root1)); 

       stage.showAndWait(); 

       // if you need the input: 
       SummController summController = loader.getController(); 
       String name = summController.getName(); 
       // do something with name... 

       primaryStage.show(); 

      }catch(IOException e){ 
       e.printStackTrace(); 
      } 
     } 


    } catch (IOException ex) { 
     ex.printStackTrace(); 
    } finally { 
     if (input != null) { 
      try { 
       input.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

    // ... 
} 
+0

这似乎很好地工作,但唯一的问题是,如果输入没有按”如果存在,它会给出错误。我会尝试检查它是否也存在...如果它有效,我会让你知道。 –

+0

非常感谢!这工作完美。我在输入声明前添加了一个“if(file.exists())”,就像这样完美。谢谢! –

0

你不能改变你的推出是这样的顺序:

public void start(Stage primaryStage) { 
    Parent root = FXMLLoader.load(getClass().getResource("real.fxml")); 
    primaryStage.setTitle("LoL Ready To Win"); 
    primaryStage.setResizable(false); 
    primaryStage.setScene(new Scene(root)); 

    try { 

     input = new FileInputStream("config.properties"); 
     prop.load(input); 

     if(prop.getProperty("name") == null){    
      try { 
       Stage stage = new Stage(); 
       Parent root1 = FXMLLoader.load(getClass().getResource("summ.fxml")); 
       stage.setTitle("Insert Title Here"); 
       stage.setResizable(false); 
       stage.setScene(new Scene(root1)); 
       stage.showAndWait(); 

       input = new FileInputStream("config.properties"); 
       prop.load(input); 
      }catch(IOException e){ 
       e.printStackTrace(); 
      } 
     } 

     primaryStage.show(); 

    } catch (IOException ex) { 
     ex.printStackTrace(); 
    } finally { 
     if (input != null) { 
      try { 
       input.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

然后在你的节目,一旦窗口写出来你的财产?