尝试为 JavaFX 中的按钮设置动作处理程序
Trying to setup an action handler for a button in JavaFX
我正在尝试使用 Javafx 设置动作处理程序,但在编译代码时出现错误。
我正在创建一个网站,我决定使用 JavaFX to 运行 和我的 Java 代码,因为它支持 CSS。发布的代码是一个简单的测试,以查看是否一切正常。到目前为止,一切正常,但是当我设置动作处理程序时出现错误。 "Caused by: javafx.fxml.LoadException:"。我希望 "Hello World" 在用户按下按钮时在控制台上打印。
控制器
import javafx.fxml.FXML;
public class Controller
{
@FXML
private void initialize()
{
}
@FXML
private void actionHandler() {
System.out.println("Hello, World!");
}}
主要
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("css_demo.fxml"));
Scene scene = new Scene(root, 800, 500);
primaryStage.setScene(scene);
primaryStage.show();
scene.getStylesheets().add(getClass().getResource("demo.css").toExternalForm());
}
public static void main(String[] args) {
launch(args);
}}
FXML 文件
<BorderPane >
<top>
<BorderPane styleClass="header-section" fx:controller="javafx.Controller" >
<left>
<Label id="header-text" text="Test Header"></Label>
</left>
<right>
<Button id="btn" text="Button" onAction="#actionHandler">
<graphic>
<ImageView fitHeight="24" fitWidth="24"
pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@javafx_account_example.png" />
</image>
</ImageView>
</graphic>
</Button>
</right>
</BorderPane>
</top>
<left>
<VBox styleClass="sidebar-section">
<children>
<Label text="Sidebar Item1"></Label>
</children>
</VBox>
</left>
<center>
<VBox styleClass="content-section">
<children>
<Label id="content-header2" text="Header2"></Label>
<Label id="header2">
<text>
test
</text>
</Label>
</children>
</VBox>
</center>
</BorderPane>
CSS
.root {
-fx-font-size: 14px;
-fx-font-family: sans-serif;
-fx-background-color: #ffffff;}
.header-section {
-fx-padding: 10px;
-fx-font-size: 20px;
-fx-background-color: blue;}
.header-section Label {
-fx-text-fill: #ffffff;
-fx-padding: 10px;}
#btn {
-fx-background-color: transparent;
-fx-text-fill: #ffffff;
-fx-font-size: 16px;
-fx-cursor: hand;}
.sidebar-section {
-fx-min-width: 200px;
-fx-pref-width: 200px;
-fx-max-width: 200px;
-fx-border-width: 1;
-fx-border-color: transparent #E8E8E8 transparent transparent;}
.sidebar-section Label {
-fx-font-size: 18px;
-fx-padding: 10 15 10 15;
-fx-border-width: 1;
-fx-border-color: transparent transparent #E8E8E8 transparent;
-fx-min-width: 200px;
-fx-pref-width: 200px;
-fx-max-width: 200px;}
.content-section {
-fx-padding: 10 20 10 20;
-fx-wrap-text: true;}
#content-header {
-fx-font-size: 18px;
-fx-padding: 0 0 10 0;
-fx-font-weight: 700;}
#content {
-fx-wrap-text: true;
-fx-font-size: 16px;}
这是我在编译代码时遇到的错误。
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$launchApplication4(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: javafx.fxml.LoadException:
/User/eclipse-workspace/demo/target/classes/demo/css_demo.fxml:15
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2543)
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 javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at demo.Main.start(Main.java:13)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication11(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait4(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null2(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater3(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[15,84]
Message: http://www.w3.org/TR/1999/REC-xml-names-19990114#AttributePrefixUnbound?BorderPane&fx:controller&fx
at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(XMLStreamReaderImpl.java:604)
at javax.xml.stream.util.StreamReaderDelegate.next(StreamReaderDelegate.java:88)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2513)
... 14 more
Exception running application demo.Main
fxml 有 2 个问题:
- 缺少
fx
命名空间的命名空间声明导致属性 fx:controller
出错
- 您只能在根元素上指定控制器。不过,您可以在其顶部子节点指定控制器。
- 当然也需要导入处理说明,但我假设您在发布的代码段中遗漏了它们。
您发布的 fxml 片段的第一部分应更改为:
<BorderPane xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafx.Controller">
<top>
<BorderPane styleClass="header-section">
我正在尝试使用 Javafx 设置动作处理程序,但在编译代码时出现错误。
我正在创建一个网站,我决定使用 JavaFX to 运行 和我的 Java 代码,因为它支持 CSS。发布的代码是一个简单的测试,以查看是否一切正常。到目前为止,一切正常,但是当我设置动作处理程序时出现错误。 "Caused by: javafx.fxml.LoadException:"。我希望 "Hello World" 在用户按下按钮时在控制台上打印。
控制器
import javafx.fxml.FXML;
public class Controller
{
@FXML
private void initialize()
{
}
@FXML
private void actionHandler() {
System.out.println("Hello, World!");
}}
主要
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("css_demo.fxml"));
Scene scene = new Scene(root, 800, 500);
primaryStage.setScene(scene);
primaryStage.show();
scene.getStylesheets().add(getClass().getResource("demo.css").toExternalForm());
}
public static void main(String[] args) {
launch(args);
}}
FXML 文件
<BorderPane >
<top>
<BorderPane styleClass="header-section" fx:controller="javafx.Controller" >
<left>
<Label id="header-text" text="Test Header"></Label>
</left>
<right>
<Button id="btn" text="Button" onAction="#actionHandler">
<graphic>
<ImageView fitHeight="24" fitWidth="24"
pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@javafx_account_example.png" />
</image>
</ImageView>
</graphic>
</Button>
</right>
</BorderPane>
</top>
<left>
<VBox styleClass="sidebar-section">
<children>
<Label text="Sidebar Item1"></Label>
</children>
</VBox>
</left>
<center>
<VBox styleClass="content-section">
<children>
<Label id="content-header2" text="Header2"></Label>
<Label id="header2">
<text>
test
</text>
</Label>
</children>
</VBox>
</center>
</BorderPane>
CSS
.root {
-fx-font-size: 14px;
-fx-font-family: sans-serif;
-fx-background-color: #ffffff;}
.header-section {
-fx-padding: 10px;
-fx-font-size: 20px;
-fx-background-color: blue;}
.header-section Label {
-fx-text-fill: #ffffff;
-fx-padding: 10px;}
#btn {
-fx-background-color: transparent;
-fx-text-fill: #ffffff;
-fx-font-size: 16px;
-fx-cursor: hand;}
.sidebar-section {
-fx-min-width: 200px;
-fx-pref-width: 200px;
-fx-max-width: 200px;
-fx-border-width: 1;
-fx-border-color: transparent #E8E8E8 transparent transparent;}
.sidebar-section Label {
-fx-font-size: 18px;
-fx-padding: 10 15 10 15;
-fx-border-width: 1;
-fx-border-color: transparent transparent #E8E8E8 transparent;
-fx-min-width: 200px;
-fx-pref-width: 200px;
-fx-max-width: 200px;}
.content-section {
-fx-padding: 10 20 10 20;
-fx-wrap-text: true;}
#content-header {
-fx-font-size: 18px;
-fx-padding: 0 0 10 0;
-fx-font-weight: 700;}
#content {
-fx-wrap-text: true;
-fx-font-size: 16px;}
这是我在编译代码时遇到的错误。
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$launchApplication4(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: javafx.fxml.LoadException:
/User/eclipse-workspace/demo/target/classes/demo/css_demo.fxml:15
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2543)
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 javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at demo.Main.start(Main.java:13)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication11(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait4(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null2(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater3(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[15,84]
Message: http://www.w3.org/TR/1999/REC-xml-names-19990114#AttributePrefixUnbound?BorderPane&fx:controller&fx
at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(XMLStreamReaderImpl.java:604)
at javax.xml.stream.util.StreamReaderDelegate.next(StreamReaderDelegate.java:88)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2513)
... 14 more
Exception running application demo.Main
fxml 有 2 个问题:
- 缺少
fx
命名空间的命名空间声明导致属性fx:controller
出错
- 您只能在根元素上指定控制器。不过,您可以在其顶部子节点指定控制器。
- 当然也需要导入处理说明,但我假设您在发布的代码段中遗漏了它们。
您发布的 fxml 片段的第一部分应更改为:
<BorderPane xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafx.Controller">
<top>
<BorderPane styleClass="header-section">