删除二级选项卡

问题描述 投票:0回答:1

我有应用程序布局:

https://i.stack.imgur.com/YsK5R.png

作为编写代码的结果我得到这个

https://i.stack.imgur.com/j2Nym.png

问:如何与关闭选项卡取出顶线,只留上的布局?

项目结构: - [SRC] ..... | - [示例] .............. | - 主 .............. | - sample.fxml .............. | - SampleController .............. | - Tab1.fxml .............. | - Tab1Controller .............. | - Tab2.fxml .............. | - Tab2Controller

码 主类

package sample;  
import javafx.application.Application;  
import javafx.fxml.FXMLLoader;  
import javafx.scene.Scene;  
import javafx.scene.control.Tab;  
import javafx.scene.control.TabPane;  
import javafx.scene.layout.BorderPane;  
import javafx.stage.Stage;  
import java.io.IOException;  
public class Main extends Application {  

private Stage primary;
private BorderPane rootLayout;


@Override
public void start(Stage primary) throws Exception{
   this.primary = primary;

   initRootLayout();
   showTab();
}

private void initRootLayout() {
    try {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Main.class.getResource("sample.fxml"));

        rootLayout = loader.load();

        Scene scene = new Scene(rootLayout);

        primary.setScene(scene);
        primary.show();

    }catch (IOException e){
        e.printStackTrace();}

}


private void showTab() {
    try {

        FXMLLoader loaderTab1 = new FXMLLoader();
        loaderTab1.setLocation(Main.class.getResource("Tab1.fxml"));

        FXMLLoader loaderTab2 = new FXMLLoader();
        loaderTab2.setLocation(Main.class.getResource("Tab2.fxml"));

        TabPane tabPane = new TabPane();

        Tab tab1 = new Tab();
        Tab tab2 = new Tab();


        tab1.setContent(loaderTab1.load());
        tab2.setContent(loaderTab2.load());

        tabPane.getTabs().addAll(tab1,tab2);


        rootLayout.setCenter(tabPane);

    } catch (IOException e){
        e.printStackTrace();
    }



}

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

sample.fxml

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

<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.layout.BorderPane?>

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.SampleController">
   <top>
      <MenuBar BorderPane.alignment="CENTER">
        <menus>
          <Menu mnemonicParsing="false" text="File">
            <items>
              <MenuItem mnemonicParsing="false" text="Close" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="Edit">
            <items>
              <MenuItem mnemonicParsing="false" text="Delete" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="Help">
            <items>
              <MenuItem mnemonicParsing="false" text="About" />
            </items>
          </Menu>
        </menus>
      </MenuBar>
   </top>
</BorderPane>

Tab1.fxml = Tab2.fxml = ... =分页{N} .fxml

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

<?import javafx.scene.control.Label?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>

<TabPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" tabClosingPolicy="UNAVAILABLE" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Tab1Controller">
  <tabs>
      <Tab text="Tab1">
         <content>
            <SplitPane dividerPositions="0.29797979797979796" prefHeight="160.0" prefWidth="200.0">
              <items>
                <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
                     <children>
                        <TableView fixedCellSize="1.0" layoutX="-12.0" layoutY="27.0" prefHeight="371.0" prefWidth="176.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                          <columns>
                            <TableColumn prefWidth="75.0" text="C1" />
                            <TableColumn prefWidth="75.0" text="C2" />
                          </columns>
                           <columnResizePolicy>
                              <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
                           </columnResizePolicy>
                        </TableView>
                     </children></AnchorPane>
                <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
                     <children>
                        <Label layoutY="14.0" text="Label" AnchorPane.leftAnchor="5.0" AnchorPane.topAnchor="14.0" />
                        <GridPane layoutX="98.0" layoutY="125.0" AnchorPane.leftAnchor="5.0" AnchorPane.rightAnchor="5.0" AnchorPane.topAnchor="35.0">
                          <columnConstraints>
                            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                          </columnConstraints>
                          <rowConstraints>
                            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                          </rowConstraints>
                           <children>
                              <Label text="Label" />
                              <Label text="Label" GridPane.rowIndex="1" />
                              <Label text="Label" GridPane.rowIndex="2" />
                              <Label text="Label" GridPane.columnIndex="1" />
                              <Label text="Label" GridPane.columnIndex="1" GridPane.rowIndex="1" />
                              <Label text="Label" GridPane.columnIndex="1" GridPane.rowIndex="2" />
                           </children>
                        </GridPane>
                     </children></AnchorPane>
              </items>
            </SplitPane>
         </content></Tab>
  </tabs>
</TabPane>

这个问题是来自这双:https://ru.stackoverflow.com/questions/941060/%D0%A3%D0%B1%D1%80%D0%B0%D1%82%D1%8C-%D0%B2%D1%82%D0%BE%D1%80%D0%B8%D1%87%D0%BD%D1%8B%D0%B5-%D0%B2%D0%BA%D0%BB%D0%B0%D0%B4%D0%BA%D0%B8

java javafx tabs
1个回答
1
投票

有很多方式做到这一点取决于你想要添加到您的应用程序/布局的通用性。

但最重要的事情是,你似乎由TabPaneTabs混淆犯了一个错误。虽然第一个是容器,第二个(多个)的一个(或多个)对应于你可以有各标签。通常你有一个TabPane包含许多Tab(S)(即使它是没有必要的)。

一种解决方案可能是通过这样来修改你的方法showTab(阅读评论)

private void showTab() {
    try {
        // Tab 1 loader
        FXMLLoader loaderTab1 = new FXMLLoader(Main.class.getResource("Tab1.fxml"));
        // Tab 2 loader
        FXMLLoader loaderTab2 = new FXMLLoader(Main.class.getResource("Tab2.fxml"));

        TabPane tabPane = new TabPane();

        // Remove the 4 following lines, Tabs are already declared in FXML
        //Tab tab1 = new Tab();
        //Tab tab2 = new Tab();
        //tab1.setContent(loaderTab1.load());
        //tab2.setContent(loaderTab2.load());
        Tab tab1 = loaderTab1.load();
        Tab tab2 = loaderTab2.load();

        tabPane.getTabs().addAll(tab1,tab2);

        rootLayout.setCenter(tabPane);

    } catch (IOException e){
        e.printStackTrace();
    }
}

并改变你的FXML文件类似以下(TabPane和选项卡标签被删除):

<Tab xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Tab1Controller" text="Tab1">
   <content>
      <SplitPane dividerPositions="0.29797979797979796" prefHeight="160.0" prefWidth="200.0">
        <items>
          <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
               <children>
                  <TableView fixedCellSize="1.0" layoutX="-12.0" layoutY="27.0" prefHeight="371.0" prefWidth="176.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                    <columns>
                      <TableColumn prefWidth="75.0" text="C1" />
                      <TableColumn prefWidth="75.0" text="C2" />
                    </columns>
                     <columnResizePolicy>
                        <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
                     </columnResizePolicy>
                  </TableView>
               </children></AnchorPane>
          <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
               <children>
                  <Label layoutY="14.0" text="Label" AnchorPane.leftAnchor="5.0" AnchorPane.topAnchor="14.0" />
                  <GridPane layoutX="98.0" layoutY="125.0" AnchorPane.leftAnchor="5.0" AnchorPane.rightAnchor="5.0" AnchorPane.topAnchor="35.0">
                    <columnConstraints>
                      <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                      <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                    </columnConstraints>
                    <rowConstraints>
                      <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                      <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                      <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                    </rowConstraints>
                     <children>
                        <Label text="Label" />
                        <Label text="Label" GridPane.rowIndex="1" />
                        <Label text="Label" GridPane.rowIndex="2" />
                        <Label text="Label" GridPane.columnIndex="1" />
                        <Label text="Label" GridPane.columnIndex="1" GridPane.rowIndex="1" />
                        <Label text="Label" GridPane.columnIndex="1" GridPane.rowIndex="2" />
                     </children>
                  </GridPane>
               </children></AnchorPane>
        </items>
      </SplitPane>
   </content>
</Tab>
© www.soinside.com 2019 - 2024. All rights reserved.