JavaFx 2.0 与 FXML:选项卡位于底部而不是顶部?

发布于 2024-12-15 01:20:06 字数 36 浏览 2 评论 0原文

是否可以定义 TabBar 将选项卡放置在底部而不是顶部?

Is it possible to define the TabBar to place the tabs at bottom instead of top?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

变身佩奇 2024-12-22 01:20:06

要设置选项卡的侧面,请使用以下代码片段:

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

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

也是完整的 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>

主类:

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();
        }
    }
}

和控制器:

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);
    }
}

正如您在示例中看到的,fxml 是 Java 类的 xml 表示形式。如果该类具有属性端(getSide、setSide),则该类在 fxml 中具有属性端。因此,您可以阅读 api 文档来了解 fxml 中也提供了哪些属性。

希望这有帮助。

To set the side of the tabs use following snippet:

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

Now the content is above the tabs.

Also as full FXML example:

<?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>

The main class:

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();
        }
    }
}

And the controller:

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);
    }
}

as you can see in the example, fxml is an xml representation of the Java classes. If the class has an property side (getSide, setSide) then the class has in fxml an attribute side. So you can read the api documentation to find out which attributes are available in fxml as well.

Hope this helps.

相思碎 2024-12-22 01:20:06

不要定义 TabBar!使用正确的布局窗格,例如javafx.scene.layout.BorderPane
查找 JavaFX 2.0 API 和 FXML 的附加示例:

JAVAFX 2.0 API

package tabtest;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class TabTest extends Application {

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

    @Override
    public void start(Stage primaryStage) {
        BorderPane root = new BorderPane();
        Scene scene = new Scene(root, 300, 250);

        TabPane tabPane = new TabPane();
        Tab tab1 = new Tab("Tab 1");
        tabPane.getTabs().add(tab1);

        root.setBottom(tabPane);

        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

有关详细信息,请查看 http://download.oracle.com/javafx/2.0/layout/jfxpub-layout.htm

FXML

package fxmltest;

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 {

    public static void main(String[] args) {
        Application.launch(FxmlTest.class, args);
    }

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("Sample.fxml"));

        Scene scene = new Scene(root, 300, 250);
        stage.setScene(scene);
        stage.show();
    }
}

示例.fxml:

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

<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<BorderPane>
    <bottom>
        <TabPane>
            <tabs>
                <Tab text="Tab 1" />
            </tabs>
        </TabPane>
    </bottom>
</BorderPane>

Don't define the TabBar! Use a proper layout pane e.g. javafx.scene.layout.BorderPane.
Find attached examples for JavaFX 2.0 API and FXML:

JAVAFX 2.0 API

package tabtest;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class TabTest extends Application {

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

    @Override
    public void start(Stage primaryStage) {
        BorderPane root = new BorderPane();
        Scene scene = new Scene(root, 300, 250);

        TabPane tabPane = new TabPane();
        Tab tab1 = new Tab("Tab 1");
        tabPane.getTabs().add(tab1);

        root.setBottom(tabPane);

        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

For further information have a look at http://download.oracle.com/javafx/2.0/layout/jfxpub-layout.htm

FXML

package fxmltest;

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 {

    public static void main(String[] args) {
        Application.launch(FxmlTest.class, args);
    }

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("Sample.fxml"));

        Scene scene = new Scene(root, 300, 250);
        stage.setScene(scene);
        stage.show();
    }
}

Sample.fxml:

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

<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<BorderPane>
    <bottom>
        <TabPane>
            <tabs>
                <Tab text="Tab 1" />
            </tabs>
        </TabPane>
    </bottom>
</BorderPane>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文