2017-05-02 74 views
0

我被「菜單屏幕」卡住了一段時間。JavaFX Button onAction將不會打開所選的FXML視圖

當我點擊激活的客戶小組,它不會打開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( 「啓動客戶小組」),但我看不到任何新的窗口。 我想知道我們如何點擊主屏幕上的按鈕,並顯示新窗口。主屏幕保持打開,除非我們關閉它。我們是否需要定義新的舞臺?

+0

你的方法'openXXXPanel'只是加載FXML文件,並指定對應的根元素變量'content'的對象,但它們不與新的內容做任何事。您需要將當前場景的根目錄設置爲「內容」(如果要使用現有窗口),或者創建一個新的「舞臺」(如果需要新窗口)。 –

+0

我創建了第二階段,但它保持主屏幕彈出主屏幕點擊時 –

+0

好吧,現在我明白了。感謝您的洞察力 –

回答

0

根據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(); 
     } 
    }