【问题标题】:JavaFX Scene Builder: Creating a new scene or just making changes on a specific element?JavaFX Scene Builder:创建新场景还是仅对特定元素进行更改?
【发布时间】:2017-08-29 02:58:44
【问题描述】:

我对 JavaFX 和 Scene Builder 完全陌生。

我的程序设计如下图,右侧有 4 个按钮,左侧有一个 TabPane。问题是我不知道如何为右侧的每个按钮设计 TabPane。例如,如果用户点击Button 1,它会显示两个标签Option 1 - AOption 1 - B。如果点击Button 2,会显示Option 2 - AOption 2 - B等等。

我怎样才能做到这一点?是否可以在 1 个场景中添加 4 个 TabPane 设计(通过显示隐藏元素(例如使用 html 和 javascript)在它们之间切换)或者我需要制作第一个场景的 4 个副本并为每个场景更改 TabPane?

【问题讨论】:

  • 您可能会创建四个不同的场景并根据单击的按钮加载适当的场景。您正在显示的这个主要阶段应该只有四个按钮,也许还有一个锚窗格。单击按钮时,将您创建的其他 fxml 文件之一加载到锚窗格中。

标签: java javafx scenebuilder tabpanel


【解决方案1】:

示例应用:此应用有一个主视图,其中包含一个锚窗格和两个按钮。这个应用程序还有另外两个视图。当在主视图中按下顶部按钮时,它将 viewOne 加载到主视图的锚窗格中。当在主视图中按下底部按钮时,它将 viewTwo 加载到主视图的锚窗格中。

主要

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

/**
 *
 * @author blj0011
 */
public class JavaFXApplication63 extends Application
{

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

        Scene scene = new Scene(root);

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

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        launch(args);
    }

}

基础视图控制器

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;

/**
 *
 * @author blj0011
 */
public class FXMLDocumentController implements Initializable
{
    @FXML AnchorPane apMain;

    @FXML
    private void handleButtonAction(ActionEvent event)
    {
        try 
        {
            Pane newLoadedPane;

            Button tempButton = (Button)event.getSource();
            switch(tempButton.getId())
            {

                case "btnOne":
                    newLoadedPane =  FXMLLoader.load(getClass().getResource("viewOne.fxml"));
                    apMain.getChildren().add(newLoadedPane);
                    break;
                case "btnTwo":
                    newLoadedPane =  FXMLLoader.load(getClass().getResource("viewTwo.fxml"));
                    apMain.getChildren().add(newLoadedPane);
                    break;
            }
        }
        catch (IOException ex) {
            Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void initialize(URL url, ResourceBundle rb)
    {
        // TODO
    }    

}

基础视图 FXML

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication63.FXMLDocumentController">
    <children>
        <Button fx:id="btnOne" layoutX="241.0" layoutY="24.0" onAction="#handleButtonAction" text="Click Me!" />
        <Button fx:id="btnTwo" layoutX="241.0" layoutY="56.0" onAction="#handleButtonAction" text="Click Me!" />
        <AnchorPane fx:id="apMain" maxHeight="200.0" maxWidth="200.0" minHeight="200.0" minWidth="200.0" prefHeight="200.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="120.0" AnchorPane.topAnchor="0.0" />
    </children>
</AnchorPane>

ViewOne 控制器

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.Initializable;

/**
 * FXML Controller class
 *
 * @author blj0011
 */
public class ViewOneController implements Initializable
{

    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb)
    {
        // TODO
    }    

}

ViewOne FXML

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

<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane fx:id="apOption2" maxHeight="200.0" maxWidth="200.0" minHeight="200.0" minWidth="200.0" prefHeight="200.0" prefWidth="200.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication63.ViewOneController">
   <children>
      <TabPane layoutX="125.0" layoutY="83.0" prefHeight="200.0" prefWidth="200.0" tabClosingPolicy="UNAVAILABLE" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
        <tabs>
          <Tab text="1 - A">
            <content>
              <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
            </content>
          </Tab>
          <Tab text="1 - B">
            <content>
              <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
            </content>
          </Tab>
        </tabs>
      </TabPane>
   </children>
</AnchorPane>

ViewTwo 控制器

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.Initializable;

/**
 * FXML Controller class
 *
 * @author blj0011
 */
public class ViewTwoController implements Initializable
{

    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb)
    {
        // TODO
    }    

}

ViewTwo FXML

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

<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane fx:id="apOption2" maxHeight="200.0" maxWidth="200.0" minHeight="200.0" minWidth="200.0" prefHeight="200.0" prefWidth="200.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication63.ViewTwoController">
   <children>
      <TabPane layoutX="24.0" layoutY="-14.0" prefHeight="200.0" prefWidth="200.0" tabClosingPolicy="UNAVAILABLE" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
        <tabs>
          <Tab text="2 - A">
            <content>
              <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
            </content>
          </Tab>
          <Tab text="2 - B">
            <content>
              <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
            </content>
          </Tab>
        </tabs>
      </TabPane>
   </children>
</AnchorPane>

在此应用中,应用启动时不会将初始视图加载到主锚窗格中。您可能希望在应用启动后立即加载视图。

【讨论】:

  • 我想知道当我在 2 个窗格之间切换时,是否会为每次单击创建一个新窗格,因为在上面的代码中,您只需创建一个新窗格然后将其添加到 appMain 中。我知道它会调用 appMain 及其所有子项,然后在其中添加一个新窗格。我应该在切换之前移除孩子吗?
  • 在这段代码中是的 它每次都会创建一个新窗格。不过应该很容易解决。
  • 如何删除 appMain 中已有的内容?
  • 我不知道我是否明白你在问什么?
  • 刚刚在这里找到了答案。 --> stackoverflow.com/questions/8382834/…
猜你喜欢
  • 2016-08-08
  • 1970-01-01
  • 2019-08-19
  • 2017-07-27
  • 2014-09-04
  • 1970-01-01
  • 2015-10-08
  • 2020-04-26
  • 1970-01-01
相关资源
最近更新 更多