场景生成器和应用程序中的不同锚点结果

时间:2014-03-08 20:38:41

标签: java javafx scenebuilder

我在Scene Builder中生成了以下FXML。当我在Scene Builder中预览并调整窗口大小时,内部拆分视图会保持自身与锚点视图之间的间距。但是当我在我的应用程序中运行FXML时却没有。有任何想法吗?我使用的是javafx 2.2.51。

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

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

<AnchorPane prefHeight="477.0" prefWidth="515.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="com.nm.haze.LibraryViewController">
  <children>
    <SplitPane dividerPositions="0.09484536082474226" focusTraversable="true" prefHeight="415.0" prefWidth="487.0" AnchorPane.bottomAnchor="14.0" AnchorPane.leftAnchor="14.0" AnchorPane.rightAnchor="14.0" AnchorPane.topAnchor="48.0">
      <items>
        <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0" />
        <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
          <children>
            <ListView prefHeight="371.0" prefWidth="425.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
          </children>
        </AnchorPane>
      </items>
    </SplitPane>
  </children>
</AnchorPane>

我的控制器代码:

package com.nm.haze;

import java.net.URL;
import java.util.ResourceBundle;

public class LibraryViewController extends BaseController {

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {

    }
}

主要代码

private static final String LIBRARY_SELECT = "LibrarySelect";
private static final String LIBRARY_VIEW = "LibraryView";

@Override
public void start(Stage primaryStage) throws IOException {
    SceneController sceneController = new SceneController(primaryStage, new SettingsManager());
    sceneController.loadScene(LIBRARY_SELECT);
    sceneController.loadScene(LIBRARY_VIEW);
    if (sceneController.setupNeeded()) {
        sceneController.setScreen(LIBRARY_SELECT);
    } else {
        sceneController.setScreen(LIBRARY_VIEW);
    }
    Group root = new Group();
    root.getChildren().addAll(sceneController);
    primaryStage.setScene(new Scene(root));
    primaryStage.show();
}

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

SceneController代码:

public class SceneController extends StackPane {

Map<String, Node> scenes = new HashMap<>();
Stage stage;
SettingsManager settingsManager;

public SceneController(Stage stage, SettingsManager settingsManager) {
    this.stage = stage;
    this.settingsManager = settingsManager;
}

public void addScene(String name, Node scene) {
    scenes.put(name, scene);
}

public void loadScene(String name) throws IOException {
    FXMLLoader myLoader = new FXMLLoader(getClass().getResource(name + ".fxml"));
    Parent loadScreen = (Parent)myLoader.load();
    ((BaseController)myLoader.getController()).setSceneController(this);
    addScene(name, loadScreen);
}

public void setScreen(final String name) {
    List<Node> children = getChildren();
    Node scene = scenes.get(name);
    if(scene != null) {
        if (children.size() > 0) {
            children.remove(0);
        }
        children.add(0, scene);

    } else {
        throw new IllegalArgumentException("Scene has not been loaded");
    }
}

public Stage getStage() {
    return stage;
}

public boolean setupNeeded() {
    return settingsManager.setupNeeded();
}

}

要明确问题所在,请参阅下面的截图前后截图。 ListViewAnchorPane之间的距离应该保持不变(在Scene Builder中也是如此)。

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

`您只需要修复SplitPane部分的最小和最大宽度,在调整窗口大小时您不想更改其宽度!

通过设置minWidth的{​​{1}}和maxWidth,我已修复了左侧部分。根据您的要求,您也可以在右侧执行相同的操作。

<强> abcFxml.fxml     

AnchorPane on the left side

<强>控制器

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

<AnchorPane prefHeight="477.0" prefWidth="515.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="bounty.LibraryViewController">
  <children>
    <SplitPane dividerPositions="0.09484536082474226" focusTraversable="true" prefHeight="415.0" prefWidth="487.0" AnchorPane.bottomAnchor="14.0" AnchorPane.leftAnchor="14.0" AnchorPane.rightAnchor="14.0" AnchorPane.topAnchor="48.0">
      <items>
        <AnchorPane minHeight="0.0" minWidth="100.0" prefHeight="160.0" prefWidth="100.0" maxHeight="160.0" maxWidth="100.0" />
        <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
          <children>
            <ListView prefHeight="371.0" prefWidth="425.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
          </children>
        </AnchorPane>
      </items>
    </SplitPane>
  </children>
</AnchorPane>

<强>截图

尺寸1

enter image description here

尺寸2

enter image description here

编辑根据用户更新

您遇到的问题是因为您使用的是import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Scene; import javafx.scene.layout.AnchorPane; import javafx.stage.Stage; public class LibraryViewController extends Application { @Override public void start(Stage arg0) throws Exception { FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("abcFxml.fxml")); AnchorPane pane = (AnchorPane) fxmlLoader.load(); Scene scene = new Scene(pane); arg0.setScene(scene); arg0.show(); } public static void main(String args[]) { launch(args); } } 。集团本身不可调整大小!尝试将StackPane直接放到场景中(就像我一样),或者你可以使用任何其他容器,如Group / VBOX

HBOX