我是javafx的新手,我需要你的帮助。我在互联网上搜索但没有成功......所以,当我第一次使用jar时,我试图让对话框出现。为此,我尝试测试文件中是否存在某个属性,如果是,则启动“第一次对话框”。当我在此对话框上单击“确定”时,我将属性保存到文件中。唯一的问题是,primaryStage是“主窗口”,而不是第一次。这个对话框有一个控制器,所以当我在按钮的“setOnAction方法”时,我无法启动primaryStage。我不能使用“node.getWindow.getScene”(或类似的东西)方法,因为它尚未打开,用户仍处于“第一次窗口”。这是我的代码..
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 :(得分: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();
}
}
}
}
然后在你的节目窗口写出你的房产?
答案 1 :(得分: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();
}
}
}
// ...
}