0

在这些天里,我正在将 java swing 代码重构为 javafx。关于一些 jpanel,我只是重构为 JFXPanel,而 JFXPanel 的根是 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 制作为手风琴。当我在谷歌搜索时,我只看到了一些使用的代码,当我制作 Accordion 标签时,我只能在标签下面使用 TitledPane。有没有其他方法可以让 JFXPanels 作为 Accordion 重用我的代码?...

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

我只是尝试让 titledPanesclicklistener 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;
    }
});

它没有用。请帮助我:)

4

2 回答 2

0

我只是使用 VBox 解决了这个问题。

我制作了 VBox、titledPane 和 VBox,其中包含一些 Btns、Label 等。

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 中,titledPanes 像手风琴一样显示。

于 2019-05-15T23:49:29.387 回答
0

如果您的问题是关于如何TitledPane一次打开 close 2 s,您可以通过编程方式执行此操作,例如使用按钮:

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()两个TitledPanes:

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());
    }
}
于 2019-04-20T14:09:16.580 回答