我在 netbeans 中使用 javafx 场景构建器创建了一个 fxml 文件。
如何将 netbeans 中的 .fxml 文件转换为 .java?有什么程序可以转换它吗?
我在 fxml 方面不是很好,所以我想使用 .java 文件来编写我的 javafx 应用程序。
请帮我。谢谢
我正在学习javaFX,我的问题是我有一些带有选择框和按钮的简单窗口。这个窗口是通过 FXML 定义的,它也与控制器类相关联。我想知道,如何用控制器类中的数据填充这个选择框,因为使用@FXML 引用这个选择框会抛出NullpointerEception
编辑 - 添加源代码 FXML 代码
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="240.0"
prefWidth="320.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="supermarket.ManageWindowCC">
<children>
<ChoiceBox fx:id="countChoiceBox" layoutX="44.0" layoutY="71.0" prefHeight="25.0" prefWidth="191.0"/>
<Label layoutX="44.0" layoutY="54.0" text="To change item's count, choose one"/>
<TextField layoutX="140.0" layoutY="129.0" prefHeight="25.0" prefWidth="24.0"/>
<Label layoutX="123.0" layoutY="112.0" text="New count"/>
<Button layoutX="126.1875" layoutY="171.5" mnemonicParsing="false" text="Submit"/>
</children>
Run Code Online (Sandbox Code Playgroud)
Java控制器代码:
public class ManageWindowCC {
@FXML
private ChoiceBox countChoiceBox;
public void onChangeCountClick(ActionEvent actionEvent) {
try {
Parent root = FXMLLoader.load(getClass().getResource("ChangeCount.fxml"));
Stage newStage = new Stage();
newStage.setTitle("Change …Run Code Online (Sandbox Code Playgroud) 在 JavaFX8 中,有一个UI 控件架构用于创建自定义控件。基本上基于:
此外,还有一个 FXML 项目的基本结构,也可用于制作 GUI。基本上:
我想将 FXML 与UI Controls Architecture一起使用,所以我的问题是:
FXML 文件的控制者是谁?皮肤?
我必须做类似下面的代码的事情?:
public class MySkin extends SkinBase<MyControl> {
public GaugeSkin(MyControl control) {
super(control);
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("MyView.fxml"));
fxmlLoader.setRoot(control);
fxmlLoader.setController(control);
try {
fxmlLoader.load();
} catch (IOException exception) {
throw new RuntimeException(exception);
}
}
Run Code Online (Sandbox Code Playgroud) 所以我开始使用 javafx,我想知道以下内容:
假设我有一个应用程序,里面有一些容器。
例如:
VBox vBox = new VBox();
我还有一个 FXML 文件,其中包含我想要vBox多次添加的其他组件。
现在我可以做类似的事情:
for(int i = 0; i < 8; i++) {
vBox.getChildren().add(FXMLLoader.load(getClass().getResource("/someComponent.fxml")));
}
Run Code Online (Sandbox Code Playgroud)
但这对我来说似乎非常低效,因为每次我添加组件时,都会从文件中重新读取它。
有没有办法构造一个 FXMLLoader ,它只读取一次文件,以某种方式保存它,并让我生成文件中定义的组件的新实例?
我正在使用javafx编写应用程序.这是一个"多屏"应用程序,带有一个主菜单,可以从中切换场景.
我的场景在不同的fxml文件中定义.
因为我尝试使用mvc模式我没有在fxml文件中设置控制器,我在FXMLloader上使用setController.
一切都运行正常,但我在单独的控制器和单独的fxml文件中有onmen的mainmenu及其所有功能.
我试过了
<fx:include source="Menubar.fxml"/>
Run Code Online (Sandbox Code Playgroud)
并为fxml文件创建了一个控制器,当我在fxml文件中设置控制器时,我无法编译源代码.如何为包含的fxml文件设置控制器?
startpage.fxml获取其控制器"Startpage"
FXMLLoader loader = new FXMLLoader(getClass().getResource("../fxml/startpage.fxml"));
loader.setController(new Startpage(m));
Pane mainPane = loader.load();
Run Code Online (Sandbox Code Playgroud)
startpage.fxml包括menubar.fxml,如何为菜单栏控件设置控制器?或者如何在每个其他控制器中轻松包含menubarController?
如何使用 JavaFX/FXML 创建新选项卡?我在 FXML 中创建了一个选项卡,但我想单击一个按钮,导致出现一个新选项卡。
这是我的 FXML:
<?import javafx.scene.effect.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.layout.*?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<top>
<MenuBar BorderPane.alignment="CENTER">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="New..." onAction="#btnNew"/>
<MenuItem mnemonicParsing="false" text="Save..." />
<MenuItem mnemonicParsing="false" text="Save As..." />
<MenuItem mnemonicParsing="false" text="Open..." />
<MenuItem mnemonicParsing="false" text="Close" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About Blank" onAction="#btnAbout"/>
</items>
</Menu>
</menus>
</MenuBar>
</top>
<center>
<TabPane prefHeight="200.0" prefWidth="200.0" tabClosingPolicy="UNAVAILABLE" BorderPane.alignment="CENTER" fx:id="tabPane">
<tabs> …Run Code Online (Sandbox Code Playgroud) 我无法设置文本字段的文本。没有错误,但文本字段仍然为空。程序的其余部分正在工作,调用该方法并且 System.out.println(...) 打印正确的文本。所以问题是,文本字段的文本无法设置。即使我只是写 textField.setText("0"); 文本字段仍然是空的。当我在 public voidinitialize(...) 下设置文本字段的文本时,它也可以工作。那么为什么它在 setCurrentInfo 中不起作用呢?
@FXML
private TextField textField;
private Info currentInfo;
@Override
public void initialize(URL url, ResourceBundle rb) {
}
public void setCurrentInfo(Info currentInfo) {
textField.setText(currentInfo.getpw1());
System.out.println(currentInfo.getpw1());
this.currentInfo = currentInfo;
}
Run Code Online (Sandbox Code Playgroud)
控制器中调用 setCurrentInfo 的部分:
@FXML
private void handleBtn1(ActionEvent event) throws Exception{
Info info = new Info(textFieldA.getText(), textFieldB.getText());
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("FXMLPassword.fxml"));
loader.load();
Stage stage;
Parent root;
if(event.getSource()==btn1){
//get reference to the button's stage
stage=(Stage) btn1.getScene().getWindow();
//load up OTHER FXML document
root = …Run Code Online (Sandbox Code Playgroud) 下图是对正在发生的事情的描述:
我有一个Pane其中包含一个Text和我使用下面的代码做出marquee like effect的Text。因此,当Pane没有足够的空间来显示文本时,动画就会开始,并且文本必须来回移动,以便用户可以完整地看到它。
问题是我希望文本在向左使用setTranslateX()时消失而不与其他元素重叠StackPane。例如,这里是在左侧重叠。
代码来自 -> JavaFX Marquee Animation问题。
下面是代码:
Marquee.java [类]:
import java.io.IOException;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.beans.InvalidationListener;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;
import javafx.util.Duration;
/**
* When the screen element is not big enough to show the text then an animation
* will start automatically
*
*
*/
public …Run Code Online (Sandbox Code Playgroud) 我一直在互联网上搜索这个,但对于我看到的大多数用 CSS 回答的问题,人们只是给出了代码而不解释它们。
-fx-effect: dropshadow(gaussian, rgba(0, 0, 0, 0.3), 10, 0.5, 0.0, 0.0);
对于在 CSS 中实现投影的这行代码,变量是什么意思?
根据我的推断,这意味着:
-fx-effect: dropshadow(blurType, color, radius, spread, offsetX, offsetY)
但是在 FXML 中实际的 DropShadow 效果标签中,有 6 个数值变量而不是 4 个。
<DropShadow blurType="GAUSSIAN" color="#ee8c9e8f" height="151.47" offsetX="3.0" offsetY="3.0" radius="73.75" spread="0.5" width="145.53" />
如何在 CSS 中使用 8 个参数实现这种效果?
我真的很绝望。\n我不\xe2\x80\x99不知道为什么,但每次我尝试运行我的程序时都会出现此错误。\n我正在使用 Netbeans 和 Java 10.0.2,以便已经安装了 JavaFX。\ n我认为\xe2\x80\x99s是由于FXML文件的一些问题造成的。\n你能帮助我吗?
\njava.lang.reflect.InvocationTargetException\n at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)\n at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\n at java.base/java.lang.reflect.Method.invoke(Method.java:564)\n at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:473)\n at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:372)\n at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)\n at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\n at java.base/java.lang.reflect.Method.invoke(Method.java:564)\n at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:941)\nCaused by: java.lang.RuntimeException: Exception in Application start method\n at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:973)\n at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:198)\n at java.base/java.lang.Thread.run(Thread.java:844)\nCaused by: javafx.fxml.LoadException: Root hasn't been set. Use method setRoot() before load.\nfile:/C:/Users/User/Documents/NetBeansProjects/gruppo71/dist/run2124011701/gruppo71.jar!/gruppo71/FXMLDocument.fxml:13\n\n at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2621)\n at javafx.fxml/javafx.fxml.FXMLLoader.access$100(FXMLLoader.java:105)\n at javafx.fxml/javafx.fxml.FXMLLoader$RootElement.constructValue(FXMLLoader.java:1338)\n at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:754)\n at javafx.fxml/javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2722)\n at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2552)\n at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)\n at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3253)\n at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3210)\n at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3179)\n at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3152)\n at …Run Code Online (Sandbox Code Playgroud) fxml ×10
javafx ×9
java ×7
javafx-8 ×2
controller ×1
css ×1
java-8 ×1
javafx-css ×1
scenebuilder ×1
tabs ×1