【问题标题】:JavaFX Button onAction won't open selected FXML viewJavaFX 按钮 onAction 不会打开选定的 FXML 视图
【发布时间】:2017-05-02 22:33:00
【问题描述】:

我被“菜单屏幕”卡住了一段时间。

当我点击激活的客户面板时,它不会打开 CustomerDisplay.fxml

Main Screen

下面是我的代码;

public class MainApp extends Application {

protected Parent content;
private Stage primaryStage;
private Stage secondStage;
private CustomerController custCtrl;
private MaintainerController mntnCtrl;
private MachineryController machCtrl;
private String fxml="";
public static MainApp instance;

public MainApp () {
    instance=this;
}

/**
 * @param args
 */
public static void main(String[] args) {    
    launch(args);
}

public static MainApp getInstance() {
    return instance;
}

@Override
public void start(Stage primaryStage) throws Exception {
    initializePanel();
    Scene scene = new Scene(content);

    primaryStage.setResizable(false);
    primaryStage.initStyle(StageStyle.UTILITY);

    primaryStage.setScene(scene);
    primaryStage.show();
}

private void initializePanel() throws IOException{
    FXMLLoader loader = new FXMLLoader();
    loader.setLocation(getClass().getResource("fxml/SimulatorDisplay.fxml"));
    content = loader.load();        
}

public void openCustomerPanel() throws IOException{
    FXMLLoader loader = new FXMLLoader();
    loader.setLocation(getClass().getResource("fxml/CustomerDisplay.fxml"));
    content = loader.load(); 
}

public void openMaintainerPanel() throws IOException{
    FXMLLoader loader = new FXMLLoader();
    loader.setLocation(getClass().getResource("fxml/MaintainerDisplay.fxml"));
    content = loader.load();        
}

public void openMachineryPanel() throws IOException{
    FXMLLoader loader = new FXMLLoader();
    loader.setLocation(getClass().getResource("fxml/MachineryDisplay.fxml"));
    content = loader.load();        
}   

}

public class SimulatorController implements Initializable{{
.
.
.
    @FXML
    public void clickCustomer (ActionEvent event) throws IOException{
        log.info("Starting Customer Panel");
        MainApp.getInstance().openCustomerPanel()**;
    }
}

它打印 log.info("Starting Customer Panel") 但我看不到任何新窗口。 我想知道我们如何单击主屏幕上的按钮并显示新窗口。除非我们将其关闭,否则主屏幕将保持打开状态。我们需要定义新的阶段吗?

【问题讨论】:

  • 您的方法openXXXPanel 只是加载一个FXML 文件并将对应于根元素的对象分配给变量content,但它们对新内容不做任何事情。您需要将当前场景的根设置为content(如果您想使用现有窗口),或者创建一个新的Stage(如果您想要一个新窗口)。
  • 我创建了二级舞台,但是点击时它会在主屏幕上弹出主屏幕
  • 好的,现在我明白了。感谢您的洞察力

标签: javafx controller


【解决方案1】:

基于 James_D 的评论; 这是工作代码:

        public class MainApp extends Application {

        public static MainApp instance;
        private Stage secondaryStage;
    .
    .
    .

        public void openCustomerPanel() throws IOException{
            FXMLLoader loader = new FXMLLoader();
            secondaryStage= new Stage();
            loader.setLocation(getClass().getResource("fxml/CustomerDisplay.fxml"));
            content = loader.load(); 
            Scene scene = new Scene(content);
            secondaryStage.setScene(scene);
            secondaryStage.show();
        }
    }

【讨论】:

    猜你喜欢
    • 2013-06-25
    • 2019-08-29
    • 1970-01-01
    • 1970-01-01
    • 2015-07-21
    • 2018-09-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多