我想在组合框和文本字段之间添加间距。我向 Box 添加了间距,但是由于 HBox 中有 4 个节点,因此它向所有节点添加了间距,这不是我想要的。我希望 TextField 位于窗口的右侧。我正在考虑添加一个大宽度的不可见分隔符,但是我读到可以使用区域,但我不确定如何在 FXML 中使用它们。
这是我当前拥有的代码,但不确定如何向其中添加区域。
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.TextFlow?>
<BorderPane prefHeight = "200.0" prefWidth = "300.0" xmlns =
<top>
<HBox alignment = "CENTER_LEFT" prefWidth = "300.0" spacing = "5">
<padding>
<Insets topRightBottomLeft="50" />
</padding>
<Label>Sort by: </Label>
<ComboBox fx:id = "sortOrder" promptText = "Select" />
</HBox>
</top>
</BorderPane>
Run Code Online (Sandbox Code Playgroud) 当我遵循指南和基本教程时,这非常令人沮丧。我可以将 CSS 样式应用于不同的元素,但不能应用于 vbox 或 hbox。
我有以下简单的应用程序,使用 FMXL 和 CSS 创建一个简单的场景:
import java.net.URL;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
public class BingRen extends Application {
@Override
public void start(Stage primaryStage) {
Parent root = null;
FXMLLoader loader = new FXMLLoader();
URL xmlUrl = getClass().getResource("/BingRen.fxml");
loader.setLocation(xmlUrl);
try {
root = loader.load();
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("BingRen.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
Run Code Online (Sandbox Code Playgroud)
使用 FXML,仅创建一个 …
我试图通过我的FXML应用程序中的ProcessBuilder创建并销毁我创建的外部进程,但它无效.这是基于Sergey Grinev给我的有用建议.
我尝试使用/不使用"// myController.setApp(this);"运行 并使用"// super.stop();" 在子类的顶部和底部(请参阅MyApp中该行的注释/输入),但没有组合可行.
这可能与FXML或JavaFX无关,但我认为这是在JavaFX上开发应用程序的常见模式.我想我要求一个Java最佳实践来关闭基于UI的应用程序中的依赖进程(如本例所示:基于FXML/JavaFX),其中有一个控制器类和一个应用程序类.
你能解释一下我做错了什么吗?或者更好:建议我应该做什么呢?谢谢.
在我的应用程序中我这样做:
public class MyApp extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
FXMLLoader fxmlLoader = new FXMLLoader();
Scene scene = (Scene)FXMLLoader.load(getClass().getResource("MyApp.fxml"));
MyAppController myController = (MyAppController)fxmlLoader.getController();
primaryStage.setScene(scene);
primaryStage.show();
// myController.setApp(this);
}
@Override
public void stop() throws Exception {
// super.stop();
// this is called on fx app close, you may call it in an action handler too
if (MyAppController.getScriptProcess() != null) {
MyAppController.getScriptProcess().destroy();
}
super.stop(); …Run Code Online (Sandbox Code Playgroud) 我有以下FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="600.0" prefWidth="1000.0" stylesheets="@../css/fxmlDemo.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="fxmldemo.controller.MainController">
<children>
<TreeTableView fx:id="treeTableView" prefHeight="600.0" prefWidth="1000.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" xmlns:fx="http://javafx.com/fxml">
<columns>
<TreeTableColumn fx:id="nameColumn" prefWidth="600.0" text="Name" />
<TreeTableColumn fx:id="sizeColumn" prefWidth="100.0" text="Size" />
<TreeTableColumn fx:id="lastModifiedColumn" prefWidth="300.0" text="Modified" />
</columns>
</TreeTableView>
</children>
</AnchorPane>
Run Code Online (Sandbox Code Playgroud)
在我的fxmlDemo.css文件中,我尝试过以下方法:
* {
-fx-border-style: solid;
}
Run Code Online (Sandbox Code Playgroud)
和
tree-table-view {
-fx-border-style: solid;
}
Run Code Online (Sandbox Code Playgroud)
和
tree-item {
-fx-border-style: solid;
}
Run Code Online (Sandbox Code Playgroud)
第一个在FXML中围绕一切边界,另外两个什么都不做.
我想要的是显示网格线像Excel电子表格.我正在使用的代码与此示例中看到的代码非常相似,但我的代码已被修改为实际工作:)
以下是Java FX CSS参考指南的链接,但它不包含有关TreeTableView或其子组件的信息.
如何在FXML中定义表格,然后使用我的JavaFX代码在运行时动态填充它?
使用此代码我初始化组合框
@FXML
private ComboBox category;
Run Code Online (Sandbox Code Playgroud)
并使用以下方法获取价
String Category = category.getValue().toString();
Run Code Online (Sandbox Code Playgroud)
并将值插入mysql数据库.现在,在类别组合框中插入下一个值之前,我需要将数据库中的值导入到组合框中的下拉列表中,并且值应该显示在组合框中.
我是javafx的新手.是否可以根据屏幕分辨率在fxml文件中动态设置首选宽度和高度?我知道如何获得屏幕分辨率并将其设置为阶段:
Screen screen = Screen.getPrimary();
Rectangle2D bounds = screen.getVisualBounds();
stage.setWidth(bounds.getWidth());
stage.setHeight(bounds.getHeight());
Run Code Online (Sandbox Code Playgroud)
我的问题是关于在fxml文件中动态设置prefWidth和prefHeight.另外我想知道我是否可以以编程方式更改fxml文件中的属性值,我正在使用场景构建器.
我尝试通过FXML文件将项目添加到组合框中,但是我收到了这个错误.
javafx.fxml.LoadException: String is not a valid type.
/C:/Users/kim/Desktop/JavaFX/ComboboxFx/bin/application/Main.fxml:13
at javafx.fxml.FXMLLoader.constructLoadException(Unknown Source)
at javafx.fxml.FXMLLoader.createElement(Unknown Source)
at javafx.fxml.FXMLLoader.processStartElement(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.load(Unknown Source)
at application.Main.start(Main.java:13)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Run Code Online (Sandbox Code Playgroud)
这是我的FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.collections.FXCollections?>
<?import javafx.scene.control.ComboBox?>
<?import …Run Code Online (Sandbox Code Playgroud) 我可以在Node不知道其父级的情况下从场景图中删除a 吗?
换句话说,我可以这样做吗?
@FXML private ToolBar toolBar;
@FXML
protected void handleCloseButtonAction(ActionEvent actionEvent) {
toolBar.getParent().getChildrenUnmodifiable().remove(toolBar);
actionEvent.consume();
}
Run Code Online (Sandbox Code Playgroud)
如果我这样做,它会抛出一个java.lang.UnsupportedOperationException.