【发布时间】:2021-12-23 06:43:52
【问题描述】:
我正在尝试构建一个在 JavaFX 中实现群聊的聊天应用程序。我想在边框窗格中创建一个滚动窗格,该窗格将包含用户所属的所有组。当用户加入群组图标(ImageViews)时,需要动态添加(不能在场景生成器中完成)到滚动窗格(HBox 内部)。
目前我正在使用一个负责所有舞台和场景更改的 SceneController 类。
package com.ong.Controllers;
import com.ong.Main;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import java.io.IOException;
import java.util.HashMap;
public class ScreenController {
private static HashMap<String, Scene> screenMap = new HashMap<>();
private static Stage main;
public ScreenController(Stage _main){
main = _main;
}
public static void addScreen(String name, FXMLLoader fxmlLoader){
Scene scene = null;
try {
scene = new Scene(fxmlLoader.load());
} catch (IOException e) {
e.printStackTrace();
}
screenMap.put(name,scene);
}
public static void addScene(String name, Scene scene){
screenMap.put(name,scene);
}
public static void removeScreen(String name){
screenMap.remove(name);
}
public static void setMinimumDimensions(int width, int height){
main.setMinWidth(width);
main.setMinHeight(height);
}
public static void setMaximized(boolean full){
main.setMaximized(full);
}
public static void activate(String name){
main.setScene(screenMap.get(name));
main.getIcons().add(new Image(Main.class.getResource("Images/not_logo.png").toString()));
main.setTitle(name);
main.show();
}
}
我已经创建了一个 FXML 文件(使用场景构建器),其中包含边框窗格和滚动窗格作为顶级子项。
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<BorderPane fx:id="borderPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="1080.0" prefWidth="1920.0" xmlns="http://javafx.com/javafx/11.0.2" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.ong.Controllers.HomepageController">
<top>
<ScrollPane fx:id="groupsMenuScrollPane" hbarPolicy="NEVER" prefHeight="62.0" prefWidth="1920.0" stylesheets="@../CSS/groups_menu.css" vbarPolicy="NEVER" BorderPane.alignment="CENTER" />
</top>
</BorderPane>
我的计划是将所有组(初始化时)填充到页面控制器中的滚动窗格中。
package com.ong.Controllers;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.shape.Circle;
import java.net.URL;
import java.util.ResourceBundle;
public class HomepageController implements Initializable {
@FXML
private ScrollPane groupsMenuScrollPane;
@FXML
private BorderPane borderPane;
@Override
public void initialize(URL location, ResourceBundle resources) {
final ImageView imageView = new ImageView("com/ong/Images/not_logo.png");
final Circle clip = new Circle(300, 200, 200);
imageView.setClip(clip);
HBox hbox = new HBox();
hbox.getChildren().add(imageView);
groupsMenuScrollPane.setContent(hbox);
borderPane.setTop(groupsMenuScrollPane);
Scene scene = new Scene(borderPane);
ScreenController.addScene("Homepage", scene);
ScreenController.activate("Homepage");
}
}
我已经尝试使用更新的边框窗格创建场景,但我收到错误消息“边框窗格已设置为另一个场景的根”。我也尝试直接更改已经存在的场景的根,但在这种情况下我得到了 NullPointerException。
您能否告知将节点动态添加到滚动窗格的最佳方式。我也将感谢 cmets 关于项目结构的信息。
【问题讨论】:
-
这种结构没有任何意义。
ScreenController中的 Everything 是static(这很糟糕,但还可以)。那么为什么它有一个构造函数呢?而且您还没有真正向我们展示问题的原因:只是用“当我尝试使用更新的边框窗格创建场景时”暗示它。你为什么要这样做呢?如果您更新内容,它将在其现有场景中更新。 -
@James_D 嗯好的。谢谢!那么您能否建议更改和跟踪场景的最佳方法是什么?我真的研究了这个主题,恕我直言,这是我找到的最佳解决方案。
-
我只是不明白你在做什么。您所要做的就是向
HBox添加内容。整个“在不同场景之间切换”的事情似乎与您在这里实际询问的内容完全无关。 -
我想问题是:你的问题到底是什么?是关于“将内容动态添加到用 FXML 创建的滚动窗格”吗?还是关于“改变和跟踪场景”?这些是完全不同的问题,甚至不应该出现在同一个问题中。
标签: java javafx responsive-design scenebuilder