【问题标题】:JAVAFX - Change specific Pane only using 1 windowJAVAFX - 仅使用 1 个窗口更改特定窗格
【发布时间】:2017-01-18 22:27:00
【问题描述】:

你能帮我如何更改 1 个场景中的特定窗格。

所以当我想单击菜单 A 时,内容将更改为内容 A。 当我单击菜单 B 时,内容将更改为内容 B

我尝试使用 2 FXML 并使用正常方法,例如加载屏幕 A 和屏幕 B

但结果只是改变了窗口。我只想用 1 个窗口更改内容。

有什么建议如何更改 1 个窗口中的特定窗格吗?

【问题讨论】:

    标签: javafx pane


    【解决方案1】:

    作为一种选择。 为任何“内容”制作 FXML 和控制器,当单击某个按钮以删除旧的“内容”并上传新内容时。

    下面的工作示例(根据 James_D 评论编辑):

    Main.java

    public class Main extends Application {
        Parent root;
        Stage stage;
    
        @Override
        public void start(Stage primaryStage) {
            try {
                root = FXMLLoader.load(getClass().getResource("Main.fxml"));
                stage = primaryStage;
                stage.setTitle("Stage");
    
                Scene scene = new Scene(root);
                stage.setScene(scene);
                stage.show();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public static void main(String[] args) {
            launch(args);
        }   
    }
    

    Main.fxml

    <?xml version="1.0" encoding="UTF-8"?>
    
    <?import javafx.scene.control.*?>
    <?import java.lang.*?>
    <?import javafx.scene.layout.*?>
    <?import javafx.scene.layout.AnchorPane?>
    
    <BorderPane fx:id="mainPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Controller">
       <left>
          <ToolBar orientation="VERTICAL" BorderPane.alignment="CENTER">
            <items>
              <Button mnemonicParsing="false" onAction="#onBtnAClick" text="A" />
                <Button mnemonicParsing="false" onAction="#onBtnBClick" text="B" />
                <Button mnemonicParsing="false" onAction="#onBtnCClick" text="C" />
            </items>
          </ToolBar>
       </left>
    </BorderPane>
    

    Controller.java

    import javafx.fxml.FXML;
    import javafx.scene.layout.BorderPane;
    
    public class Controller {
    
        @FXML
        BorderPane mainPane;
    
        @FXML
        public void onBtnAClick(){
            ContentA contentA = new ContentA();
            mainPane.setCenter(contentA);
        }
    
        @FXML
        public void onBtnBClick(){
            ContentB contentB = new ContentB();
            mainPane.setCenter(contentB);
        }
    
        @FXML
        public void onBtnCClick(){
            ContentC contentC = new ContentC();
            mainPane.setCenter(contentC);
        }
    
    }
    

    还有一些内容示例:

    ContentA.java

    public class ContentA extends AnchorPane{
    
        public ContentA(){
            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("ContentA.fxml"));
    
            fxmlLoader.setRoot(this);
            fxmlLoader.setController(this);
    
            try {
                fxmlLoader.load();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
    }
    

    ContentA.fxml

    <?xml version="1.0" encoding="UTF-8"?>
    
    <?import javafx.scene.control.*?>
    <?import java.lang.*?>
    <?import javafx.scene.layout.*?>
    <?import javafx.scene.layout.AnchorPane?>
    
    <fx:root prefHeight="200.0" prefWidth="300.0" type="AnchorPane" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
       <children>
          <ListView layoutX="50.0" layoutY="-26.0" prefHeight="200.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
       </children>
    </fx:root>
    

    【讨论】:

    • 很好的答案,但你真的不需要AnchorPane,对吗?只需在 BorderPane 上调用 setCenter 并加载您加载的每个内容。
    • 罗杰。这是我使用 BorderPane 的第一个代码,我几乎不知道)会在几分钟内修改)谢谢)
    【解决方案2】:

    试试这个,很简单

    Parent root = null; 
            try {
                root=FXMLLoader.load(getClass().getResource("your_FXML_File.fxml"));
            } catch (Exception e) {
            }
            borderpane.setCenter(root);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-19
      • 2016-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-09
      • 1970-01-01
      相关资源
      最近更新 更多