对于上下文,我在 Intellij 中有一个Maven 项目结构,还有/target/classes/
我收到错误的代码:
@Override
public void start(Stage stage) throws IOException {
Parent root;
FXMLLoader fxmlLoader = new FXMLLoader();
// Below line has the problem of loading image inside fxml file.
root = fxmlLoader.load(getClass().getClassLoader().getResourceAsStream("AuthorizationPage.fxml"));
// not used in code but below line returns inputstream object which is not null so the image loads fine
when independently verified.
// System.out.println(Objects.isNull(getClass().getClassLoader().getResourceAsStream("sign-up-logo.png")));
//Below Line works fine and loads fxml file correctly with images.
root = FXMLLoader.load(Objects.requireNonNull(getClass().getResource("/AuthorizationPage.fxml")));
Scene scene = …Run Code Online (Sandbox Code Playgroud) The problem is that i want to know how to make responsive Desktop applications in JavaFx or Java Swing (preferred in JavaFx) . When i try to resize my application frame it's components size does not grow or shrink and similar is the situation when i try to run this on different screen sizes with different screen resolutions. I have searched on this topic but could not find any solution to this. So, any reference material will also be very …
我有一个用 java swing 创建的程序,但是我遇到了一个问题,在没有太多在线帮助的情况下使用它来解决我遇到的问题,所以我开始学习 JavaFX 并使用 fxml 作为 gui,所以我尝试创建主窗口在 JavaFX 代码中。只用代码,我就能让它看起来像我想要的那样。然后我决定将其切换到 fxml,因此我创建了 fxml 文件,在我的 java 文件中添加了代码来加载它,但是当我运行该程序时,我不断收到错误“Caused by: java.lang.ClassNotFoundException: javafx .scene.control.VBox”,或与 HBox 相同,具体取决于我使用的。我尝试自己解决这个问题,也尝试过chatGpt,但我找不到解决方案。让我困惑的是,当我第一次在 VS Code 中创建 JavaFX 项目时,它在 fxml 中使用了 VBox,但工作正常。我已经复制了使用的所有代码,只是更改了 fxml 节点。对于上下文,这里是模块文件、我的代码和 fxml 文件。
module gearworks {
requires javafx.controls;
requires javafx.fxml;
opens gearworks to javafx.fxml;
exports gearworks;
}
Run Code Online (Sandbox Code Playgroud)
package gearworks;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
/**
* JavaFX App
*/
public class App extends Application {
private BorderPane root;
public static void main(String[] …Run Code Online (Sandbox Code Playgroud) 由于缺少教程和以下问题的示例不完整,我决定写这个问题。如果这个问题的答案成为解决类似问题的有效例子,我会很高兴。
任务
让我们使用JavaFX技术(使用FXML作为该技术的一部分来制作图形视图)制作一个GUI 应用程序,例如显示用户集合和每个用户的汽车集合。让我们还使用JavaFX 属性和bindig机制将模型(数据)与 GUI 同步。
执行
我从为用户和汽车创建类开始。
用户.java
package example;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
public class User {
public StringProperty firstName;
public StringProperty lastName;
private ObservableList<Car> cars;
public User(String firstName, String lastName, Car[] cars) {
this.firstName = new SimpleStringProperty(firstName);
this.lastName = new SimpleStringProperty(lastName);
this.cars = FXCollections.observableArrayList(cars);
}
public String getFirstName() …Run Code Online (Sandbox Code Playgroud) 我在将VBox作为子属性插入MenuButton时遇到问题,因为我必须在赋值中执行此操作.到目前为止,我已经想出了这个:
<MenuButton fx:id="mb" mnemonicParsing="false" text="" GridPane.columnIndex="1" GridPane.rowIndex="3">
<items>
<VBox prefHeight="200.0" prefWidth="100.0" >
<Button text="Button" GridPane.columnIndex="0" GridPane.rowIndex="0" />
<RadioButton text="RadioButton" GridPane.columnIndex="1" GridPane.rowIndex="0" />
<Button text="Click Me" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<ComboBox GridPane.columnIndex="0" GridPane.rowIndex="1" />
<Slider GridPane.columnIndex="0" GridPane.rowIndex="2" />
<CheckBox text="CheckBox" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<TextField prefWidth="200.0" GridPane.columnIndex="0" GridPane.rowIndex="3" />
</VBox>
</items>
</MenuButton>Run Code Online (Sandbox Code Playgroud)
但是我收到了以下错误:
Caused by: java.lang.IllegalArgumentException: Unable to coerce VBox@13f6e35e to class javafx.scene.control.MenuItem.
Run Code Online (Sandbox Code Playgroud)
有人可以澄清我的错误或帮助我解决它吗?谢谢你的帮助.
我需要将 BorderPane 包含在 StackPane 内。用于使用 JFX 对话框。JFXDialog() 的第一个参数必须是 StackPane 变量。我无法传递 BorderPane 变量来代替它。所以我尝试围绕 BorderPane 创建一个新的 StackPane 标签
示例.fxml
<StackPane fx:id="rootPane" xmlns="http://javafx.com/javafx/8.0.112-ea"
xmlns:fx="http://javafx.com/fxml/1">
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity"
minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0"
prefWidth="600.0" style="-fx-background-color: #181818;" xmlns="http://javafx.com/javafx/16"
xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.SampleController">
<left>
.........
.........
</BorderPane>
</StackPane>
Run Code Online (Sandbox Code Playgroud)
但是在添加此标签后,Sample.fxml 无法使用 SceneBuilder 打开,当我尝试运行 JavaFx Main 类时,我收到此错误。
javafx.fxml.LoadException: StackPane is not a valid type.
Run Code Online (Sandbox Code Playgroud)
示例控制器.java
....
public class SampleController implements Initializable {
...
@FXML
private StackPane rootPane;
@FXML
private void migrateButtonAction(ActionEvent event){
JFXDialogLayout dialogLayout = new JFXDialogLayout();
JFXDialog dialog = new JFXDialog(rootPane,dialogLayout, …Run Code Online (Sandbox Code Playgroud) 编辑:我相信我终于找到了问题的正确答案。
原帖:
我目前正在尝试使用 JavaFX 和 EventBus 系统创建一个应用程序。为此,我必须在实例化其他类时将 EventBus 作为构造函数参数传递给它们。但是我不知道如何在使用 FXMLLoader 加载我的 .fxml 文件的同时执行此操作。
我的代码目前看起来像这样:
主班
public class MyApplication extends Application {
public void start(Stage stage) throws Exception {
EventBus eventBus = new EventBus();
>>> Here would be code that creates an Object of MainView, passing eventBus as constructor argument. <<<
Scene scene = new Scene(mainView);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Run Code Online (Sandbox Code Playgroud)
这个类继承自 BorderPane,我想使用 fxmlLoader 创建它的对象(我想。我不确定它是否像那样工作)
puplic class MainView extends BorderPane {
private EventBus …Run Code Online (Sandbox Code Playgroud) 来自JavaFX的fxml?你能打开缩写吗?
我找到的所有首字母缩略词网站都没有返回,最接近的是来自dummies.com但是这不是肯定的.