我是FXML的新手,我正在构建一个应用程序.现在我遇到了一个我无法解决的问题.
我在FXML中定义了一个组合框,并在控制器类中创建了necesarry关联.但我想在这个组合框中添加图像.
仍然经过几个小时的谷歌搜索,我仍然无法解决这个问题.
你能帮助我一个关于如何实现目标的"简单"例子吗?
非常感谢!
我目前的代码是:(确定有一种更简单的方法可以做到这一点,但它有效!)
ImageView img1 = new ImageView("Layout/nl.png");
ImageView img2 = new ImageView("Layout/en.png");
AnimalBoxLanguage.getItems().addAll(img1, img2);
AnimalBoxLanguage.setCellFactory(new Callback<ListView<ImageView>, ListCell<ImageView>>() {
@Override public ListCell<ImageView> call(ListView<ImageView> p) {
return new ListCell<ImageView>() {
private final ImageView rectangle;
{
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
rectangle = new ImageView();
}
@Override protected void updateItem(ImageView item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setGraphic(null);
} else {
rectangle.setImage(item.getImage());
setGraphic(rectangle);
}
}
};
}
});
Run Code Online (Sandbox Code Playgroud) 我是javaFX的新手.我使用fxml创建了一个注册表单.现在我想实现字段验证功能.我正在尝试实现TextField的验证,但我仍然没有得到它.请任何人都可以帮助我.
谢谢Naresh
我是JavaFX的新手,我在JavaFX中遇到了2-3种设计UI的方法.
哪个更好?为什么?
我正在尝试加载我的应用程序的主屏幕,实际上它从未实际运行并显示屏幕。经过进一步调查(通过 NetBeans 调试器运行),我发现我的代码在 FXMLLoader.load(url); 之后永远不会执行。-- 它停在那里,并且不会抛出任何错误。我确实知道该网址是正确的 - 我检查了它的值,它是正确的。谁知道怎么修它?提前致谢!
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.text.*?>
<AnchorPane 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="graphics.MainScreenController">
<children>
<Text fx:id="funds" layoutX="489.0" layoutY="383.0" strokeType="OUTSIDE" strokeWidth="0.0" text="USD 52,356,000.07">
</Text>
</children></AnchorPane>
Run Code Online (Sandbox Code Playgroud)
package graphics;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.MenuButton;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.VBox;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.stage.Stage;
/**
* FXML Controller class
*
*/ …Run Code Online (Sandbox Code Playgroud) 你好Stackeroverflowers,
我想问一下如何更换RadioButton Dot?我需要定义3个具有3种不同颜色的RadioButton来显示状态.我必须与Css合作吗?如果有人能告诉我我怎么能意识到这一点,那将是完美的吗?
一个例子是:
@FXML
public RadioButton redState = new RadioButton();
.
.
.
redState.setDotColor(red);
Run Code Online (Sandbox Code Playgroud)
或者在Fxml中定义Color静态:
style="-fx-dot-color: rgb(255,0,0);"
Run Code Online (Sandbox Code Playgroud)
我希望每个人都理解我的计划.它只是在javafx RadioButton中着色Dot.
我很感谢每一个答案.
如何在`.fxml文件中编辑text,TextArea我可以使用换行符和制表符.Textarea是预定义的,无法编辑.
要支持的图像
FXML文件

风景

使用JavaFX作为应用程序,我有一个Main.fxml文件,里面有一些fxml子文件.
我想从子控制器访问Main.fxml的MainController类.
我将尝试用一个例子更好地解释:
MainFxml:
<HBox fx:controller="MainController.java">
<fx:include source="child.fxml"/>
</HBox>
Run Code Online (Sandbox Code Playgroud)
MainController:
public class MainController implements Initializable {
private String string;
public void setString (String string) {
this.string = string;
}
Run Code Online (Sandbox Code Playgroud)
ChildFxml:
<HBox fx:id="child" fx:controller="ChildController.java">
<Button text="hello" onAction="#selectButton"></Button>
</HBox>
Run Code Online (Sandbox Code Playgroud)
ChildController:
public class ChildController implements Initializable {
@FXML HBox child;
@FXML Button button;
@FXML
public void selectButton (ActionEvent event) {
// here call MainController.setString("hello");
}
Run Code Online (Sandbox Code Playgroud)
我尝试在StackOverflow上找到这个解决方案,但我需要获取已经加载的Main.fxml的Controller引用.是否有任何方法可以从特定的窗格启动Controller?就像是:
// child.getParent().getController();
Run Code Online (Sandbox Code Playgroud) IntelliJ checkstyle 指示应删除此标记,因为它不是必需的,如 in 而不是
<VBox>
<children>
<HBox>
...
</HBox>
<HBox>
...
</HBox>
</children>
</VBox>
Run Code Online (Sandbox Code Playgroud)
你可以简单地拥有
<VBox>
<HBox>
...
</HBox>
<HBox>
...
</HBox>
</VBox>
Run Code Online (Sandbox Code Playgroud)
这确实有助于在某些时候避免臃肿的代码,但我没有在这里找到关于约定的好的参考。
此链接提到该标记是可选的,因为“加载程序将自动将 VBox 的子元素添加到容器的“子”集合中”。但这并不清楚是否可以从代码中删除此标签作为惯例?
此外,Scene Builder 似乎将这些标签添加到新创建的控件中,但它不介意您是否删除它们。在编辑这样一个修改过的文件时,它不会重新添加它们。
在没有直接和明确替换的情况下排除StyleManager的想法真是太棒了 - 我看到它打破了许多程序!
我需要为整个应用程序(自定义组件,自定义伪类,自定义平台特定的补丁等)进行一些重新设计.在JavaFX 8中,我可以使用:com.sun.javafx.css.StyleManager.getInstance().addUserAgentStylesheet("MyShit.css");
但是在Java 9中,StyleManager不可用.那么有没有办法为每个场景设置CSS?
使用"Application.setUserAgentStylesheet"不是一个选项,因为我不想丢失默认的lookandfeel,我已经调用它来设置MODENA样式.我想扩展默认CSS而不是替换它.我也不想重写整个MODENA(显然).
我真的不想手动为应用程序中的每个表单/场景设置相同的样式表(或将其添加到每个FXML文件).有许多形式,动态构造的对话框.主要原因是,有几个应用程序之间共享的部分(和库),所以我不想硬编码任何样式表(可能有不同的应用程序的名称和路径).
所以我需要一种方法在程序的一个点为应用程序的每个可能场景设置一个额外的样式表.FX9有可能吗?
PS我研究了自定义FXML加载器,CSS加载器,场景加载器和拦截场景创建的其他方法 - 没有办法!
这是我的场景构建器的样子:
这是图形用户界面:
独立的场景构建器:
我只是从 Java SDK 演示中运行以下源代码:
package sample;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
Button btn = new Button();
btn.setText("Say 'Hello World'!");
StackPane root_ctn = new StackPane();
root_ctn.getChildren().add(btn);
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
System.out.println("Hello World!");
}
});
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root_ctn, 300, 275));
primaryStage.show(); …Run Code Online (Sandbox Code Playgroud)