2017-04-04 111 views
1

我對JavaFX和Scene Builder完全陌生。JavaFX場景構建器:創建一個新場景或只是對特定元素進行更改?

我的程序設計如下圖,右邊有4個按鈕,左邊是TabPane。問題是我不知道如何爲右側的每個按鈕設計TabPane。例如,如果用戶點擊Button 1,它將顯示2個標籤Option 1 - AOption 1 - B。如果點擊Button 2,則顯示Option 2 - AOption 2 - B等。

我該如何做到這一點?它可以在1場景中添加4個TabPane設計(通過顯示隱藏元素(例如,使用html和javascript)來切換它們)或者我需要製作第一個場景的4個副本並更改每個場景的TabPane?

enter image description here

enter image description here

+0

根據點擊哪個按鈕,您可能會創建四個不同的場景並加載相應的場景。你正在顯示的主要舞臺應該只有四個按鈕,也許還有一個錨板。單擊按鈕時,將您創建的其他fxml文件中的一個加載到anchorpane中。 – Sedrick

回答

1

示例應用程序:這個程序具有由一個anchorpane和兩個按鈕的MAINVIEW。這個應用程序還有另外兩個視圖。當在mainview中按下頂部按鈕時,它將viewOne加載到mainview的anchorpane中。當在mainview中按下底部按鈕時,它將viewTwo加載到mainview的anchorpane中。

主要

import javafx.application.Application; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 

/** 
* 
* @author blj0011 
*/ 
public class JavaFXApplication63 extends Application 
{ 

    @Override 
    public void start(Stage stage) throws Exception 
    { 
     Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml")); 

     Scene scene = new Scene(root); 

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

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

} 

基本視角控制器

import java.io.IOException; 
import java.net.URL; 
import java.util.ResourceBundle; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javafx.event.ActionEvent; 
import javafx.fxml.FXML; 
import javafx.fxml.FXMLLoader; 
import javafx.fxml.Initializable; 
import javafx.scene.control.Button; 
import javafx.scene.layout.AnchorPane; 
import javafx.scene.layout.Pane; 

/** 
* 
* @author blj0011 
*/ 
public class FXMLDocumentController implements Initializable 
{ 
    @FXML AnchorPane apMain; 

    @FXML 
    private void handleButtonAction(ActionEvent event) 
    { 
     try 
     { 
      Pane newLoadedPane; 

      Button tempButton = (Button)event.getSource(); 
      switch(tempButton.getId()) 
      { 

       case "btnOne": 
        newLoadedPane = FXMLLoader.load(getClass().getResource("viewOne.fxml")); 
        apMain.getChildren().add(newLoadedPane); 
        break; 
       case "btnTwo": 
        newLoadedPane = FXMLLoader.load(getClass().getResource("viewTwo.fxml")); 
        apMain.getChildren().add(newLoadedPane); 
        break; 
      } 
     } 
     catch (IOException ex) { 
      Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 

    @Override 
    public void initialize(URL url, ResourceBundle rb) 
    { 
     // TODO 
    }  

} 

基本視圖FXML

<?xml version="1.0" encoding="UTF-8"?> 

<?import javafx.scene.control.Button?> 
<?import javafx.scene.layout.AnchorPane?> 

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication63.FXMLDocumentController"> 
    <children> 
     <Button fx:id="btnOne" layoutX="241.0" layoutY="24.0" onAction="#handleButtonAction" text="Click Me!" /> 
     <Button fx:id="btnTwo" layoutX="241.0" layoutY="56.0" onAction="#handleButtonAction" text="Click Me!" /> 
     <AnchorPane fx:id="apMain" maxHeight="200.0" maxWidth="200.0" minHeight="200.0" minWidth="200.0" prefHeight="200.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="120.0" AnchorPane.topAnchor="0.0" /> 
    </children> 
</AnchorPane> 

ViewOne控制器

import java.net.URL; 
import java.util.ResourceBundle; 
import javafx.fxml.Initializable; 

/** 
* FXML Controller class 
* 
* @author blj0011 
*/ 
public class ViewOneController implements Initializable 
{ 

    /** 
    * Initializes the controller class. 
    */ 
    @Override 
    public void initialize(URL url, ResourceBundle rb) 
    { 
     // TODO 
    }  

} 

ViewOne FXML

<?xml version="1.0" encoding="UTF-8"?> 

<?import javafx.scene.control.Tab?> 
<?import javafx.scene.control.TabPane?> 
<?import javafx.scene.layout.AnchorPane?> 

<AnchorPane fx:id="apOption2" maxHeight="200.0" maxWidth="200.0" minHeight="200.0" minWidth="200.0" prefHeight="200.0" prefWidth="200.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication63.ViewOneController"> 
    <children> 
     <TabPane layoutX="125.0" layoutY="83.0" prefHeight="200.0" prefWidth="200.0" tabClosingPolicy="UNAVAILABLE" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> 
     <tabs> 
      <Tab text="1 - A"> 
      <content> 
       <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" /> 
      </content> 
      </Tab> 
      <Tab text="1 - B"> 
      <content> 
       <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" /> 
      </content> 
      </Tab> 
     </tabs> 
     </TabPane> 
    </children> 
</AnchorPane> 

ViewTwo控制器

import java.net.URL; 
import java.util.ResourceBundle; 
import javafx.fxml.Initializable; 

/** 
* FXML Controller class 
* 
* @author blj0011 
*/ 
public class ViewTwoController implements Initializable 
{ 

    /** 
    * Initializes the controller class. 
    */ 
    @Override 
    public void initialize(URL url, ResourceBundle rb) 
    { 
     // TODO 
    }  

} 

ViewTwo FXML

<?xml version="1.0" encoding="UTF-8"?> 

<?import javafx.scene.control.Tab?> 
<?import javafx.scene.control.TabPane?> 
<?import javafx.scene.layout.AnchorPane?> 

<AnchorPane fx:id="apOption2" maxHeight="200.0" maxWidth="200.0" minHeight="200.0" minWidth="200.0" prefHeight="200.0" prefWidth="200.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication63.ViewTwoController"> 
    <children> 
     <TabPane layoutX="24.0" layoutY="-14.0" prefHeight="200.0" prefWidth="200.0" tabClosingPolicy="UNAVAILABLE" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> 
     <tabs> 
      <Tab text="2 - A"> 
      <content> 
       <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" /> 
      </content> 
      </Tab> 
      <Tab text="2 - B"> 
      <content> 
       <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" /> 
      </content> 
      </Tab> 
     </tabs> 
     </TabPane> 
    </children> 
</AnchorPane> 

enter image description here

enter image description here

enter image description here

在這個應用程序中,當應用程序啓動時,沒有初始視圖加載到主錨板中。您可能想要在應用程序啓動後立即加載視圖。

+0

它按預期工作。謝謝。 –

+0

我想知道當我在2個窗格之間切換時,是否爲每次點擊創建一個新窗格,因爲在上面的代碼中,您只需創建一個新窗格,然後將其添加到appMain中。我明白,它會調用appMain及其所有的兒童,然後添加一個新窗格。我應該在切換之前移除孩子嗎? –

+0

在這段代碼中是的它每次都會創建一個新窗格。應該是一個簡單的修復,但。 – Sedrick