我通过 Scene Builder 使用 FXML 来建立一些 JavaFX 场景和格式化模板。浏览了网络和 Oracle 教程后,我仍然发现确定布局大小/换行/适合内容/等的“规则”。元素和组件之间的间距对我来说 90% 是一种魔法。我缺少的是“布局概述”(缺少章节),它将 FXML(和 JavaFX)布局放在一起。如果您遇到过这种生物,请分享链接。
到目前为止,我只找到了少量信息。例如:
提供有关某一属性、参数或特征的一些有用信息。似乎没有任何内容概述大局,也没有努力将“字体系列”与(什么是)“有效字体”之间的点联系起来?
另外,我正在寻找一些可以完成更多业务或应用程序类型工作的示例。更多真实世界的示例,例如数据输入表单,其中包含文本字段、组合框、单选按钮等详细信息。在屏幕上执行“正常”操作,而不仅仅是查看闪亮的图形来显示 JavaFX 可能执行的操作。
我认为缺少的主要内容是有关不同 JavaFX 容器和元素的描述,以及将它们关联在一起以实现彼此相关的格式化外观、格式化布局、渲染大小。
请原谅我举了一个听起来像是批评的例子,这并不是为了我只是没有找到让我满足一些简单要求的信息:
我可以列出我遇到的主要的、具体的障碍(接下来)。我接受的是,我的知识和我正在阅读的关于容器如何工作的知识存在差距,最小-首选-最大宽度和高度如何工作?他们如何互动等等?就目前而言,这个问题可能太大了。我可以举一个例子和一些可以遵循的细节,并将其留给群众的智慧......
例子
| col-01 | col-02 | col-03 | col-04 | col-04 |
| | | | | |
| expand | fixed | scale | expand | fit |
| | percent …Run Code Online (Sandbox Code Playgroud) 我有两个fxml文件。我用一个include声明将它们联系起来:
“主”fxml文件如下所示:
<?import javafx.geometry.*?>
// ...
<BorderPane prefHeight="962" prefWidth="1280" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MyMainController">
<center>
<SplitPane dividerPositions="0.63" BorderPane.alignment="CENTER">
<items>
<fx:include source="AnotherFile.fxml" />
// ...
</items>
</SplitPane>
</center>
<top>
// ...
</top>
</BorderPane>
Run Code Online (Sandbox Code Playgroud)
第二个(=“AnotherFile.fxml”)就像这样:
<?import java.lang.*?>
// ...
<SplitPane dividerPositions="0.15" orientation="VERTICAL" prefHeight="400.0" prefWidth="500.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1">
<items>
// ...
<Label fx:id="oneOfMyLabels" text="myText" GridPane.columnIndex="2" GridPane.rowIndex="1" />
</items>
</SplitPane>
Run Code Online (Sandbox Code Playgroud)
现在,我在“主”控制器中使用注入application.MyMainController:
@FXML
private Label oneOfMyLabels;
Run Code Online (Sandbox Code Playgroud)
如果我运行控制器,我会得到java.lang.NullPointerException一个异常java.lang.reflect.InvocationTargetException。在调试模式下我发现,注入的Label是null!
现在,我的问题:无法MyMainController从“主 fxml 文件”访问所包含的 …
我最初的 fxml(比如说home.fxml)有很多功能,因此需要很多时间才能完全加载。因此,为了避免程序启动和 fxml 加载之间的时间间隔,我引入了另一个loader.fxml带有 gif 图像的 fxml(例如 ),该图像应在主 fxml 加载时出现。问题是我的 loader.fxml 中的 gif 图像没有移动,因为程序挂起,直到 home.fxml 完全加载。为了避免这种情况,我将 home.fxml 加载移动到线程中,如下面的代码所示。
public class UATReportGeneration extends Application {
private static Stage mainStage;
@Override
public void start(Stage stage) {
Parent loaderRoot = null;
try {
loaderRoot = FXMLLoader.load(getClass().getResource("/uatreportgeneration/fxml/Loader.fxml"));
} catch (IOException ex) {
Logger.getLogger(UATReportGeneration.class.getName()).log(Level.SEVERE, null, ex);
}
Scene loadScene = new Scene(loaderRoot);
stage.setScene(loadScene);
stage.initStyle(StageStyle.UNDECORATED);
stage.getIcons().add(new Image(this.getClass().getResourceAsStream("/uatreportgeneration/Images/logo.png")));
stage.show();
mainStage = new Stage(StageStyle.UNDECORATED);
mainStage.setTitle("Upgrade Analysis");
mainStage.getIcons().add(new Image(this.getClass().getResourceAsStream("/uatreportgeneration/Images/logo.png")));
setStage(mainStage);
new Thread(() -> {
Platform.runLater(() …Run Code Online (Sandbox Code Playgroud) 我需要通过 CSS 格式化 JavaFX 中表格视图的最后一行,如果在 CSS 中不可能,那么如何在代码中做到这一点。
我有这个:
一个带左节点的边框窗格(绿色箭头),里面有按钮(橙色箭头)。
我不能做的是找到一种方法来根据窗口的大小更改 Vbox(绿色段)的尺寸,然后更改按钮(橙色段)的尺寸。(当用户播放窗口大小时)
我更喜欢找到一种方法将参数设置到我的 css 文件中,或者作为最后的手段在我的 fxml 中。
.css 文件:
.left-borderPane{ /*this is style class for Vbox */
/*something so similar: */
-fx-min-width:30% ;
-fx-max-width:30% ;
}
.icon-settings{/*this is style class for buttons*/
-fx-shape:"M72.5,65.9 M90,50c0,8.6-2.5,16.9-7.3,24.1c-0.4,0.6-1,0.9-1.7,0.9c-0.4,0-0.8-0.1-1.1-0.3c-0.9-0.6-1.2-1.8-0.6-2.50zM69.6,50c0,3.2-0.6,6.2-1.8,9.1c-0.3,0.8-1.1,1.2-1.8,1.2c-0.2,0-0.5,0-0.8-0.1c-1-0.4c0.6-0.6,1.4-0.7,2.2-0.4C57.5,14.5,58,15.2,58,16z M35,37H14v26h21V37z M54,20.8l-15,15v28.3l15,15V20.8z";
/*something so similar: */
-fx-min-width:80% ;
-fx-max-width:80% ;
}
Run Code Online (Sandbox Code Playgroud) 我在创建的 StackPane 布局中添加了两个图像视图,但是当我尝试在 StackPane 布局中添加按钮节点时,虽然图像工作得很好,但按钮不允许程序运行并抛出此错误我无法弄清楚的例外:
> > Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: javafx.fxml.LoadException: Invalid identifier.
/C:/Users/Steli/Dropbox/Java_Projects/IntelliJ%20IDEA%20Projects/DokkanCardsPreview/out/production/DokkanCardsPreview/fxml/home.fxml:33
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2597)
at javafx.fxml.FXMLLoader.access$100(FXMLLoader.java:103)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:901)
at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:971)
at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at …Run Code Online (Sandbox Code Playgroud) 我有一个Button我想放在我的 BorderPane 底部并将其居中。我想用FXML.
这是我的 BorderPane 的底部:
<bottom>
<Button>
<text>
Center this button
</text>
</Button>
</bottom>
Run Code Online (Sandbox Code Playgroud)
完整FXML(不含进口):
<BorderPane id="BorderPane" xmlns:fx="http://javafx.com/fxml/1">
<top>
<HBox>
<BorderPane.margin>
<Insets left="15" right="15" top="15"/>
</BorderPane.margin>
<spacing>
5
</spacing>
<Label>
<padding>
<Insets top="5" right="5"/>
</padding>
<text>
Y-Axis:
</text>
</Label>
<TextField>
<text>
10
</text>
</TextField>
<Label>
<padding>
<Insets top="5" left="15" right="5"/>
</padding>
<text>
X-Axis:
</text>
</Label>
<TextField>
<text>
10
</text>
</TextField>
</HBox>
</top>
<bottom>
<Button>
<text>
Center this button
</text>
</Button>
</bottom>
Run Code Online (Sandbox Code Playgroud)
结果:
是否有任何标签可以使用 …
我的 FXML 中有一个 DatePicker,我需要将它插入到我的 SQL 数据库中的日期。我想格式化我的日期,但它不起作用。
LocalDate localDate = purchased_at.getValue();
localDate.format(DateTimeFormatter.ofPattern("dd.mm.yyyy"));
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误。
Caused by: java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: MinuteOfHour
Run Code Online (Sandbox Code Playgroud)
我还是个初学者。在过去的 3 或 4 个月里,我使用了 Java。我正在努力改进。
我正在使用 JavaFX 和 jdk 1.8。
使用 FXML 文件上的脚本,可以在禁用另一个按钮时禁用一个按钮吗?
我正在尝试使用 Scene Builder 解决我正在使用 javafx 11 和 java11 处理的计算器项目的问题。我试图找出一种方法,让计算器上的按钮在从键盘输入相应的值时改变颜色。是否有解决此问题的方法或 onKeyPressed 等类型的解决方案?
当用户用鼠标单击按钮时,我能够使按钮改变颜色(变为绿色)。这是在我的 css 样式表中完成的。我尝试向控制器类中的 onKeyReleased 方法添加一个方法,并且可以通过这种方式更改背景,但无法确定一种在不导致 UI 延迟的情况下及时更改颜色的方法。我希望 UI 更改类似于大多数计算器(例如标准 Windows 计算器)的颜色。这是用户按住键并改变颜色的地方,当释放键时它又变回。
//main.java
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Parent root =
FXMLLoader.load(getClass().getResource("calculator.fxml"));
primaryStage.getIcons().add(new Image("CALC.png"));
primaryStage.setTitle(" TS Calculator");
primaryStage.setScene(new Scene(root, 250, 375));
primaryStage.setResizable(true);
primaryStage.setMinHeight(375);
primaryStage.setMinWidth(250);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
/CSS
Button{
-fx-background-color: black;
-fx-text-align: …Run Code Online (Sandbox Code Playgroud) fxml ×10
javafx ×8
java ×6
javafx-8 ×3
css ×2
layout ×2
date ×1
datepicker ×1
exception ×1
fxmlloader ×1
java-11 ×1
javafx-11 ×1
javafx-2 ×1
scenebuilder ×1
task ×1