带有FXML的JavaFx 2.0:底部而不是顶部的标签?

Far*_*san 0 javafx javafx-2 fxml

是否可以定义TabBar以将标签放在底部而不是顶部?

and*_*eas 6

要设置标签的一侧,请使用以下代码段:

TabPane tabPane = new TabPane();
Tab tab1 = new Tab("Tab 1");
tab1.setContent(new Label("Tab1 content"))
tabPane.getTabs().add(tab1);
tabPane.setSide(Side.BOTTOM)
Run Code Online (Sandbox Code Playgroud)

现在内容位于选项卡上方.

同样作为完整的FXML示例:

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

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.collections.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.effect.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.shape.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.web.*?>

<BorderPane xmlns:fx="http://javafx.com/fxml" fx:controller="example.MainViewController">
  <bottom>
    <HBox>
      <children>
        <Button fx:id="btnNewTab" mnemonicParsing="false" text="Add New Tab" onAction="#btnNewTabAction" />
      </children>
    </HBox>
  </bottom>
  <center>
    <TabPane fx:id="tabPane" side="BOTTOM">
      <tabs>
        <Tab text="Untitled Tab 1">
          <content>
            <AnchorPane id="Content">
                <Label>Content 1</Label>
            </AnchorPane>
          </content>
        </Tab>
        <Tab text="Untitled Tab 2">
          <content>
            <AnchorPane id="Content">
                <Label>Content 2</Label>
            </AnchorPane>
          </content>
        </Tab>
      </tabs>
    </TabPane>
  </center>
  <top>
    <Label text="TabPane Example" />
  </top>
</BorderPane>
Run Code Online (Sandbox Code Playgroud)

主要课程:

package example;

import java.io.File;
import java.io.IOException;
import java.net.URL;

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

public class ExampleMain extends Application {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Application.launch(ExampleMain.class, args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        URL location = ExampleMain.class.getResource("MainView.fxml");

        FXMLLoader fxmlLoader = new FXMLLoader();
        fxmlLoader.setLocation(location);
        fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());

        try {
            Parent root = (Parent) fxmlLoader.load(location.openStream());

            // in case you need access to the controller
            MainViewController mainViewController = fxmlLoader.getController();

            primaryStage.setScene(new Scene(root, 1024, 768));
            Scene s = primaryStage.getScene();
            s.setRoot(root);
            primaryStage.sizeToScene();
            primaryStage.show();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

和控制器:

package example;

import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;

public class MainViewController {

    private static int counter = 0;

    @FXML
    private TabPane tabPane;

    @FXML
    public void btnNewTabAction() {
        Tab tab = new Tab();
        tab.setText("new Tab " + ++counter);
        tab.setContent(new Label("Content of new tab " + counter));
        tabPane.getTabs().add(tab);
    }
}
Run Code Online (Sandbox Code Playgroud)

正如您在示例中看到的,fxml是Java类的xml表示形式.如果类具有属性侧(getSide,setSide),则该类在fxml中具有属性侧.因此,您可以阅读api文档以找出fxml中可用的属性.

希望这可以帮助.