本文介绍了Javafx Stackpane显示类似的CardLayout Java Swing的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用三个按钮在堆栈窗格中显示每个子面板,但是当我单击按钮时,它会在每次点击时显示不同的面板

I use three buttons to display each child panel in stackpane,but when I click the button,it displays different panels per click

示例

btn0->窗格0,窗格1和窗格2

btn0-> pane 0,pane 1and pane 2

btn1->窗格0,窗格1和窗格2

btn1-> pane 0,pane 1and pane 2

btn2->窗格0,窗格1和窗格2

btn2-> pane 0,pane 1and pane 2

我只是想让它显示java swing cardLayout中特定按钮的特定面板我应该这样做吗?

I just want it to display a specific panel for specific buttons in java swing cardLayout should I do?

btn0-> pane 0

btn0-> pane 0

btn1-> pane 1

btn1-> pane 1

btn2-> pane 2

btn2-> pane 2

请帮助我!

@FXML
void btn0(ActionEvent event) {
    stackConsole.getChildren().get(0).toFront();
}

@FXML
void btn1(ActionEvent event) {
    stackConsole.getChildren().get(1).toFront();
}

@FXML
void btn2(ActionEvent event) {
    stackConsole.getChildren().get(2).toFront();
}


推荐答案

根据您的需要,您可以对 TabPane 类感兴趣。

Depending on your needs you may be interested in the TabPane class.

当然在控制器类中实现类似的功能。但是,为孩子调用 toFront()将无法获得所需的效果,因为

Of course implement similar funtionality in the controller class. Calling toFront() for a child will not have the desired effect however, since


  1. 所有孩子都留在 stackConsole

  2. toFront 只需移动节点它被调用到父母的最后一个子位置。

  1. All children remain in the stackConsole
  2. toFront just moves the Node it is called for to the last child position of the parent.

你是什么然而,尝试实现似乎正在取代 stackConsole 的子代。这可以通过将不同的子节点注入控制器类并使用 ObservableList.setAll 来替换内容来完成。 < fx:define> 标记可用于最初未在场景中显示的子项。

What you're trying to achieve however seems to be replacing the children of stackConsole. This can be done by injecting the different children to the controller class and using ObservableList.setAll to replace the contents. The <fx:define> tag can be used for children not initially shown in the scene.

FXML

<BorderPane xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="fxml.ReplaceController">
    <center>
        <StackPane fx:id="stackConsole">
            <children>
                <!-- use multiply blend mode to demonstrate other children are not present
                     (result would be black otherwise) -->
                <Region fx:id="r0" blendMode="MULTIPLY" prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: red;" />
                <fx:define>
                    <Region fx:id="r1" blendMode="MULTIPLY" prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: blue;" />
                    <Region fx:id="r2" blendMode="MULTIPLY" prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: green;" />
                </fx:define>
            </children>
        </StackPane>
    </center>
    <left>
        <VBox prefHeight="200.0" spacing="10.0" BorderPane.alignment="CENTER">
            <children>
                <Button mnemonicParsing="false" text="1" onAction="#btn0"  />
                <Button mnemonicParsing="false" text="2" onAction="#btn1" />
                <Button mnemonicParsing="false" text="3" onAction="#btn2" />
            </children>
            <padding>
                <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
            </padding>
        </VBox>
    </left>
</BorderPane>

控制器

public class ReplaceController {
    @FXML
    private Region r1;
    @FXML
    private Region r2;
    @FXML
    private Region r0;
    @FXML
    private Pane stackConsole;

    @FXML
    private void btn0(ActionEvent event) {
        stackConsole.getChildren().setAll(r0);
    }

    @FXML
    private void btn1(ActionEvent event) {
        stackConsole.getChildren().setAll(r1);
    }

    @FXML
    private void btn2(ActionEvent event) {
        stackConsole.getChildren().setAll(r2);
    }

}

这篇关于Javafx Stackpane显示类似的CardLayout Java Swing的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 13:23