gpt4 book ai didi

java - 场景生成器和应用程序中的不同 anchor 结果

转载 作者:太空宇宙 更新时间:2023-11-04 06:59:42 25 4
gpt4 key购买 nike

我在场景生成器中生成了以下 FXML。当我在场景生成器中预览它并调整窗口大小时,内部分割 View 会保持其自身与 anchor View 之间的间距。但是,当我在应用程序中运行 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>

还有我的 Controller 代码:

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

场景 Controller 代码:

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 之间的距离应保持相同(在场景生成器中也是如此)。

enter image description here

enter image description here

最佳答案

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

这里我通过设置左侧的AnchorPaneminWidthmaxWidth来修复左侧部分。根据您的要求,您也可以对右侧执行相同的操作。

abcFxml.fxml

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

Controller

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

}

屏幕截图

尺寸1

enter image description here

尺寸 2

enter image description here

编辑:根据用户更新

您面临的问题是由于您使用的Group造成的。组本身不可调整大小!尝试将 StackPane 直接放入您的场景(就像我所做的那样),或者您可以使用任何其他容器,例如 VBOX/HBOX

@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);
}
primaryStage.setScene(new Scene(sceneController));
primaryStage.show();
}

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

关于java - 场景生成器和应用程序中的不同 anchor 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22274773/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com