一次扩展2个标题窗格

时间:2019-04-18 23:49:36

标签: swing javafx accordion jfxpanel

这些天来,我正在将Java Swing代码重构为javafx。 关于某些jpanels,我只是将其重构为JFXPanel,而JFXPanels的根是VBOX。 JFXPanel的结构是VBOX-> BorderPane-> TitledPane-> AnchorPane。

<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="93.0" prefWidth="295.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <BorderPane prefHeight="259.0" prefWidth="309.0">
         <top>
            <TitledPane animated="false" prefHeight="93.0" prefWidth="297.0" style="-fx-background-color: #eeeeee;" text="Compensation Options" textFill="#1b75bc">
               <content>
                  <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="295.0" prefWidth="267.0">
                     <children>
                        <Button fx:id="mResetCountButton" layoutX="25.0" layoutY="5.0" mnemonicParsing="false" text="Reset Counts" textAlignment="CENTER" />
                        <Button fx:id="plotStatsButton" layoutX="123.0" layoutY="5.0" mnemonicParsing="false" text="Stats Panel" textAlignment="CENTER" />
                        <Button fx:id="gateStatsButton" layoutX="208.0" layoutY="5.0" mnemonicParsing="false" text="Gate State" textAlignment="CENTER" />
                        <Button fx:id="mLoadMatrixButton" layoutX="28.0" layoutY="37.0" mnemonicParsing="false" text="Load Matrix" textAlignment="CENTER" />
                        <Button fx:id="viewCompMatrix" layoutX="119.0" layoutY="37.0" mnemonicParsing="false" text="View Matrix" textAlignment="CENTER" />
                        <Label fx:id="mJCompMatrixCombo" layoutX="208.0" layoutY="41.0" text="Matrix Name" />
                     </children>
                  </AnchorPane>
               </content>
            </TitledPane>
         </top>
      </BorderPane>
   </children>
</VBox>

现在我想将JFXPanels用作手风琴。当我在Google中搜索时,我只看到一些使用的代码,当我制作手风琴标签时,我可以在标签下面使用TitledPane。 还有其他方法可以使JFXPanels作为Accordion重用我的代码吗?...

实际上使用Tag,我可以一次扩展一个titlePane。 如何使用Accordion制作JavaFX应用程序,它可以一次扩展许多titlePanes?

我只是尝试使titledPanes clicklistener and resizing VBox which is wrapping titledPane and titledpane的大小和在titledpane内部的一些窗格一样。

例如

boolean titledPaneClicked = false;

TiteldPane.setOnAction((e)->{
    if(titledPaneClicked){
        TitledPane.setHeight(0);
        Vbox.setHeight(0);
        soemotherPanes.setHeight(0);
        titledPaneClicked = !titledPaneClicked;
    }else{
        TitledPane.setHeight(previousSize);
        Vbox.setHeight(previousSize);
        soemotherPanes.setHeight(previousSize);
        titledPaneClicked = !titledPaneClicked;
    }
});

它没有用。请帮我:)

2 个答案:

答案 0 :(得分:0)

如果您的问题是如何一次关闭两个TitledPane,则可以通过编程方式进行,例如使用按钮:

Main.fxml (使您的代码mcve始终发布fxml名称并导入)

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TitledPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.VBox?>

<VBox minWidth="-Infinity" prefHeight="200.0" prefWidth="200.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" 
fx:controller="tests.xml.Controller">
   <children>
      <BorderPane>
         <top>
            <TitledPane fx:id="aTitledPane" animated="false" style="-fx-background-color: #eeeeee;" text="Compensation Options" textFill="#1b75bc">
               <content>
                  <AnchorPane>
                     <children>
                        <Label text="Top pane expanded" />
                     </children>
                  </AnchorPane>
               </content>
            </TitledPane>
         </top>
          <center>
            <TitledPane fx:id="bTitledPane" animated="false" style="-fx-background-color: #eeeeee;" text="Other Options" textFill="#1b75bc">
               <content>
                  <AnchorPane>
                     <children>
                        <Label text="Bottom pane expanded" />
                     </children>
                  </AnchorPane>
               </content>
            </TitledPane>
         </center>
         <bottom>   
            <Button fx:id="openCloseButton" onAction="#openClose" text="Close" textAlignment="CENTER" />
         </bottom>
      </BorderPane>
   </children>
</VBox>

它是控制器:

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TitledPane;

public class Controller {

    @FXML
    private TitledPane aTitledPane, bTitledPane;
    @FXML
    private Button openCloseButton;

    private final static String OPEN = "Open", CLOSE = "Close";

    @FXML
    void initialize(){
        openCloseButton.setText(OPEN);
        aTitledPane.setExpanded(false);
        bTitledPane.setExpanded(false);
    }

    @FXML
    private void openClose(){
        System.out.println(openCloseButton.getText());
        if(openCloseButton.getText().equals(OPEN)){
            openCloseButton.setText(CLOSE);
            aTitledPane.setExpanded(true);
            bTitledPane.setExpanded(true);
        }else{
            openCloseButton.setText(OPEN);
            aTitledPane.setExpanded(false);
            bTitledPane.setExpanded(false);
        }
    }
}

使用进行测试:

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

public class FXMLTest extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{

        Parent root = FXMLLoader.load(getClass().getResource("xml/Main.fxml"));
        primaryStage.setTitle("Hello World");
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

如果要打开/关闭一个TitledPane而不使用按钮来打开/关闭另一个,请从fxml中删除该按钮,并绑定两个TitledPane.expandedProperty()中的TitledPane在控制器中:

import javafx.fxml.FXML;
import javafx.scene.control.TitledPane;

public class Controller {

    @FXML
    private TitledPane aTitledPane, bTitledPane;

    @FXML
    void initialize(){
        aTitledPane.expandedProperty().bindBidirectional(bTitledPane.expandedProperty());
    }
}

答案 1 :(得分:0)

我仅使用VBox就解决了这个问题。

我制作了标题为Pane和VBox的VBox,其中包含一些Btns,标签等。

VBox mainBox = new VBox();
TitledPane T1 = new TitledPane();
VBox content1 = new VBox();
T1.setContent(content1);
TitledPane T2 = new TitledPane();
VBox content2 = new VBox();
T2.setContent(content2);

mainBox.getChildren().addAll(T1, T2);

然后在MainVBox中,显示标题为Panes的手风琴。