在javaFX中的BorderPane中的窗格之间切换

时间:2015-09-23 08:39:22

标签: javafx

我是JavaFx的新人,我有一个问题。 我有5个Anchor或SplitPanes我需要通过按下按钮来切换它们。我该怎么做?如果可以,请写一个简短的例子。

在这里我放置我的代码: 这是我的Main.class,我导入所有FXML文件和控制器

public class MainApp extends Application{


private Stage primaryStage;
private BorderPane rootLayout;
private ArrayList<Costs> list = new ArrayList<>();

private SplitPane overView;
private AnchorPane history;
private AnchorPane center;


@Override
public void start(Stage primaryStage) {
    this.primaryStage=primaryStage;
    this.primaryStage.setTitle("Home Budget");

    initRootLayout();

    initOverView();
    rootLayout.setCenter(center);

    Scene scene = new Scene(rootLayout);
    primaryStage.setScene(scene);
    primaryStage.show();

}

private void initRootLayout() {
    try{
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource("view/RootLayout.fxml"));
        rootLayout =(BorderPane)loader.load();

        RootLayoutController controller =loader.getController();
        }
    catch (IOException ex) {
        ex.printStackTrace();
        Alert alert = new Alert(Alert.AlertType.ERROR);
        alert.setTitle("Error");
        alert.setHeaderText("");
        alert.setContentText("Could not load FXML Loader");
        alert.showAndWait();
    }
}

public boolean showEditDialog(Costs costs){
    try{
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(MainApp.class.getResource("view/ShowEditDialog.fxml"));
        AnchorPane pane =(AnchorPane) loader.load();

        Stage dialogStage = new Stage();
        dialogStage.setTitle("New expence");
        dialogStage.initModality(Modality.WINDOW_MODAL);
        dialogStage.initOwner(primaryStage);

        Scene scene = new Scene(pane);
        dialogStage.setScene(scene);
        ShowEditDialogController controller = loader.getController();
        controller.setDialogStage(dialogStage);
        dialogStage.showAndWait();
        return controller.isOkClicked();
        }
    catch (IOException ex) {
        ex.printStackTrace();
        Alert alert = new Alert(Alert.AlertType.ERROR);
        alert.setTitle("Error");
        alert.setHeaderText("");
        alert.setContentText("Could not load FXML Loader of Edit Dialog");
        alert.showAndWait();
        return false;
    }
}

public SplitPane initOverView(){
    try{
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(MainApp.class.getResource("view/OverView.fxml"));
        overView = (SplitPane)loader.load();

        OverViewController controller = loader.getController();
        }
    catch (IOException ex) {
        ex.printStackTrace();
        Alert alert = new Alert(Alert.AlertType.ERROR);
        alert.setTitle("Error");
        alert.setHeaderText("");
        alert.setContentText("Could not load FXML Loader of Overview");
        alert.showAndWait();
    }
return overView;
}

public AnchorPane initHistory(){
    try{
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(MainApp.class.getResource("view/History.fxml"));
        history = (AnchorPane)loader.load();
        HistoryController controller = loader.getController();
    }
    catch (IOException ex) {
        ex.printStackTrace();
        Alert alert = new Alert(Alert.AlertType.ERROR);
        alert.setTitle("Error");
        alert.setHeaderText("");
        alert.setContentText("Could not load FXML Loader of Overview");
        alert.showAndWait();
    }
return history;
}


public Stage getPrimaryStage() {
    return primaryStage;
}

public void setPrimaryStage(Stage primaryStage) {
    this.primaryStage = primaryStage;
}

public BorderPane getRootLayout() {
    return rootLayout;
}

public void setRootLayout(BorderPane rootLayout) {
    this.rootLayout = rootLayout;
}

public ArrayList<Costs> getList() {
    return list;
}

public void setList(ArrayList<Costs> list) {
    this.list = list;
}

public SplitPane getOverView() {
    return overView;
}

public void setOverView(SplitPane overView) {
    this.overView = overView;
}

public AnchorPane getHistory() {
    return history;
}

public void setHistory(AnchorPane history) {
    this.history = history;
}

public AnchorPane getCenter() {
    return center;
}

public void setCenter(AnchorPane center) {
    this.center = center;
}


public static void main(String[] args) {
    launch(args);
}   
}

这里是RootLayout的控制器,我想从中更改Panes

public class RootLayoutController implements Initializable{

MainApp mainApp = new MainApp();

@FXML
private BorderPane root;
@FXML
private Button overView;
@FXML
private Button History;

@FXML
private void handleNew(){

    Costs costs = new Costs();
    boolean okClicked = mainApp.showEditDialog(costs);
}

@FXML
private void handleHistory(){

}

@FXML
private void handleOverView(){

}

@Override
public void initialize(URL location, ResourceBundle resources) {

} 
}

最后一个我的RootLayout FXML文件

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

<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<BorderPane prefHeight="375.0" prefWidth="700.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="apg.budget.view.RootLayoutController">
   <left>
      <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="375.0" prefWidth="150.0" BorderPane.alignment="CENTER">
         <children>
            <Group />
            <Button alignment="BOTTOM_LEFT" onAction="#handleOverView" prefHeight="35.0" prefWidth="130.0" text="Overview" AnchorPane.leftAnchor="1.0" AnchorPane.rightAnchor="1.0">
               <font>
                  <Font size="15.0" />
               </font>
            </Button>
            <Button alignment="BOTTOM_LEFT" layoutY="35.0" mnemonicParsing="false" onAction="#handleHistory" prefHeight="35.0" prefWidth="130.0" text="History" AnchorPane.leftAnchor="1.0" AnchorPane.rightAnchor="1.0">
               <font>
                  <Font size="15.0" />
               </font>
            </Button>
            <Button alignment="BOTTOM_LEFT" layoutX="1.0" layoutY="70.0" mnemonicParsing="false" prefHeight="35.0" prefWidth="130.0" text="Budget" AnchorPane.leftAnchor="1.0" AnchorPane.rightAnchor="1.0">
               <font>
                  <Font size="15.0" />
               </font>
            </Button>
            <Button alignment="BOTTOM_LEFT" layoutY="105.0" mnemonicParsing="false" prefHeight="35.0" prefWidth="130.0" text="Distribution" AnchorPane.leftAnchor="1.0" AnchorPane.rightAnchor="1.0">
               <font>
                  <Font size="15.0" />
               </font>
            </Button>
            <Button alignment="BOTTOM_LEFT" layoutX="1.0" layoutY="140.0" mnemonicParsing="false" prefHeight="35.0" prefWidth="130.0" text="Statistics" textAlignment="CENTER" AnchorPane.leftAnchor="1.0" AnchorPane.rightAnchor="1.0">
               <font>
                  <Font size="15.0" />
               </font>
            </Button>
            <Button alignment="BOTTOM_LEFT" layoutX="1.0" layoutY="340.0" mnemonicParsing="false" onAction="#handleNew" prefHeight="35.0" prefWidth="130.0" text="New" textAlignment="CENTER" AnchorPane.leftAnchor="1.0" AnchorPane.rightAnchor="1.0">
               <font>
                  <Font size="15.0" />
               </font>
            </Button>
         </children>
      </AnchorPane>
   </left>
</BorderPane>

谢谢你的帮助!

0 个答案:

没有答案