0
我被“菜单屏幕”卡住了一段时间。JavaFX Button onAction将不会打开所选的FXML视图
当我点击激活的客户小组,它不会打开CustomerDisplay.fxml
下面是我的代码;
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( “启动客户小组”),但我看不到任何新的窗口。 我想知道我们如何点击主屏幕上的按钮,并显示新窗口。主屏幕保持打开,除非我们关闭它。我们是否需要定义新的舞台?
你的方法'openXXXPanel'只是加载FXML文件,并指定对应的根元素变量'content'的对象,但它们不与新的内容做任何事。您需要将当前场景的根目录设置为“内容”(如果要使用现有窗口),或者创建一个新的“舞台”(如果需要新窗口)。 –
我创建了第二阶段,但它保持主屏幕弹出主屏幕点击时 –
好吧,现在我明白了。感谢您的洞察力 –