Oracle文档对此并不十分清楚,google和bing都没有.
我可以(像在WPF中)绑定到FXML中的模型,跳过Java中的绑定代码吗?
我确实在某个博客上看到了一个简短的flash,就像$ {object.field}这样的语法,但我无法确定.
后期编辑:我已经做了XAML开发(WPF,Silverlight,Windows Phone)已经有好几年了,我已经习惯于在标记中表达数据绑定.此外,我读过的关于FXExperience 2011年的文章说
我可以稍微改变一下,然后将绑定移动到FXML文档中.这将允许工具除了布局之外还处理数据绑定.请注意,以下代码今天不起作用,因为FXML不支持双向绑定,但我们正在努力修复它.
实际上它就像:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import fxmlapp.Model ?>
<VBox fx:id="root" xmlns:fx="http://javafx.com/fxml" fx:controller="fxmlapp.Sample">
<fx:define>
<Model fx:id="model" />
</fx:define>
<children>
<TextField fx:id="firstNameField" text="${model.person.firstName}" />
<Label fx:id="messageLabel" text="${model.person.firstName}" />
</children>
</VBox>
Run Code Online (Sandbox Code Playgroud)
由于它是在2011年的文章中,事情本来可以实施,但没有找到任何证据朝这个方向.因此,经过仔细搜索,我决定在这里问一下.
我想将css文件链接到我的应用程序.在我的fxml文件中,我使用:
<stylesheets>
<URL value="@../stylesheet1.css" />
</stylesheets>
Run Code Online (Sandbox Code Playgroud)
...当我在scenebuilder中打开fxml文件时,我可以看到样式预览.但是当我尝试运行应用程序时,我收到一个错误:
java.net.MalformedURLException:无协议:../ styleles1.css
所以我用这种方式测试了它:
<stylesheets>
<String fx:value="stylesheet1.css" />
</stylesheets>
Run Code Online (Sandbox Code Playgroud)
现在它是另一回事 - 应用程序启动并应用css,但我没有在scenebuilder中看到预览.错误消息:
"文件stylesheet1.css不存在.找不到资源stylesheet1.css."
那么如何正确附加css文件呢?
好吧,虽然我的问题没有得到解答,为什么它不能以上述方式工作,但我找到了一个适合我的解决方案.在我的FXML中我只有这一行
<?scenebuilder-stylesheet ../stylesheet1.css?>
Run Code Online (Sandbox Code Playgroud)
所以Scenebuilder与那个css一起工作.在我的主类中,我以编程方式设置样式表:
Scene scene = new Scene(root);
String css = this.getClass().getResource("../stylesheet1.css").toExternalForm();
scene.getStylesheets().add(css);
Run Code Online (Sandbox Code Playgroud) 在我的FXML内部,我有这个:
<fx:define>
<ToggleGroup fx:id="toggleGroup1"/>
</fx:define>
<Menu fx:id="toggleProofView" text="%proofView">
<items>
<RadioMenuItem text="%hide"
selected="true"
onAction="#handleLoadComponent"
toggleGroup="toggleGroup1"/>`
Run Code Online (Sandbox Code Playgroud)
不知怎的,我得到了错误:
Unable to coerce toggleGroup1 to class javafx.scene.control.ToggleGroup
但为什么?
我要做的是创建一个Menu包含其中几个RadioMenuItem都在同一个ToggleGroup中的东西.如何将它们添加到FXML文件中的切换组?
是否可以将网格窗格扩展到最大宽度和高度?
我有以下内容:
<fx:root type="javafx.scene.layout.AnchorPane" xmlns:fx="http://javafx.com/fxml"
stylesheets="view/Style.css">
<children>
<GridPane xmlns:fx="http://javafx.com/fxml"
AnchorPane.topAnchor="0.0" AnchorPane.bottomAnchor="128.0"
AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0"
hgap="10" vgap="10">
<!--anything-->
</GridPane>
</children>
</fx:root>
Run Code Online (Sandbox Code Playgroud)
并且我想将网格窗格扩展到由锚定窗格确定的限制(因此整个屏幕除了下部)
我使用javafx 和fxml,所以我使用控制器进行实际编码.我需要在舞台上做一些操作,例如获得x轴或y轴位置.我曾尝试stage.getX()和stage.getY,但他们不工作(艺名为高lited为错误).如何在控制器中使用此类功能?我尝试在我的主文件中执行此操作:
public int locationX = stage.getX();
Run Code Online (Sandbox Code Playgroud)
和
public double locationX = stage.getX();
Run Code Online (Sandbox Code Playgroud)
但它不起作用,反而使整个程序成为一个大错误.
那么如何在我的控制器文件中执行此类功能呢?我是否需要以其他方式导入某些内容或执行上述操作?
error: cannot find symbol
locationX = stage.getX();
symbol: variable stage
location: class FXMLController
Run Code Online (Sandbox Code Playgroud)
我知道"舞台"缺失了.但是如何在我的控制器中获得"舞台"?
我正在尝试使用JavaFX和SceneBuilder 1.1进行自定义控件.
我有这个代码:
FXML
<?import libreria.javaFX.componentes.componenteTextField.*?>
<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml">
<children>
<CustomComponent fx:id="pastaTxt" layoutX="69.0" layoutY="87.0" prefWidth="200.0" />
</children>
</AnchorPane>
Run Code Online (Sandbox Code Playgroud)
CustomComponent.java
package libreria.javaFX.componentes.componenteTextField;
import javafx.scene.control.TextField;
public class CustomComponent extends TextField {
public CustomComponent() {
super();
// TODO Auto-generated constructor stub
}
public CustomComponent(String arg0) {
super(arg0);
// TODO Auto-generated constructor stub
}
Run Code Online (Sandbox Code Playgroud)
}
当我尝试从SceneBuilder打开它时它会告诉我:
缺少的类型有:[CustomComponent]
它让我有机会指定Classpath(它也不能解决问题).
我尝试将类放在import语句中,如下所示:
<?import libreria.javaFX.componentes.componenteTextField.CustomComponent?>
Run Code Online (Sandbox Code Playgroud)
但它给了一个ClassNotFoundException.
关于为什么会发生这种情况的任何想法?
更多信息
我只使用这些类完成了一个新项目:

代码如下:
CustomControl.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import custom.CustomControl?>
<?import java.lang.*?>
<?import java.util.*?> …Run Code Online (Sandbox Code Playgroud) 是否可以在JavaFX应用程序运行时更改css?
我正在寻找的效果是点击一个按钮改变皮肤或主题.
FXML如果存在任何差异,则UI位于文件中.
我试过了
Scene.getStylesheets()
.add(getClass().getResource(skinFileName).toExternalForm());
Run Code Online (Sandbox Code Playgroud)
这没有效果.
谢谢
我想这是一件非常简单的事情,但我无法支持它.我想要的只是在链接到fxml的ImageView上显示图像.这是我的代码:
package application;
import java.io.File;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
public class Main extends Application
{
@FXML
private ImageView imageView;
@Override
public void start(Stage primaryStage)
{
try
{
AnchorPane root = (AnchorPane)FXMLLoader.load(getClass().getResource("Sample.fxml"));
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setTitle("Hello World");
File file = new File("src/Box13.jpg");
Image image = new Image(file.toURI().toString());
imageView = new ImageView(image);
//root.getChildren().add(imageView);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
Parent parent = FXMLLoader.load(Main.class.getResource("JanelaPrincipal.fxml"));
Run Code Online (Sandbox Code Playgroud)
在fxml文件中有一个对控制器类的引用.如何获取控制器对象?
FXML:
<AnchorPane id="AnchorPane" fx:id="root"
prefHeight="768.0" prefWidth="1024.0" xmlns:fx="http://javafx.com/fxml/1"
xmlns="http://javafx.com/javafx/2.2"
fx:controller="br.meuspila.javafx.JanelaPrincipalController">
...
Run Code Online (Sandbox Code Playgroud) 我看了很多页面试图找出如何切换场景,但我没有成功.
我有一个计算器,我的目标是选择一个菜单选项来改变计算器(即:基本和科学).现在我只是测试所以这里是我的代码到目前为止与这个问题相关(我使用的是Scene Builder):
@FXML private MenuItem basic;
@FXML private MenuItem testSwitch;
public static void main(String[] args)
{
Application.launch( args );
}
@Override
public void start(Stage primaryStage) throws Exception
{
Parent pane = FXMLLoader.load(
getClass().getResource( "calculator.fxml" ) );
Scene scene = new Scene( pane );
primaryStage.setScene(scene);
primaryStage.setTitle( "Calculator" );
primaryStage.show();
}
@FXML
public void handleMenuOption(ActionEvent e)
{
if(e.getSource()==basic)
{
changeScene("calculator.fxml");
}
else if(e.getSource()==testSwitch)
{
changeScene("TestSwitch.fxml");
}
}
public void changeScene(String fxml)
{
//this prints out
System.out.println(fxml);
}
Run Code Online (Sandbox Code Playgroud)
编辑 我已经尝试了很多东西.无论如何,我总是得到这个NullPointerException.我觉得它可能与在场景构建器中设置某些内容有关,但我一直无法找到答案
Exception in …Run Code Online (Sandbox Code Playgroud) fxml ×10
javafx ×7
javafx-2 ×5
java ×4
scenebuilder ×3
css ×2
data-binding ×1
imageview ×1
location ×1
markup ×1
menuitem ×1
radio-button ×1
scene ×1
stylesheet ×1