java - 尝试为 JavaFX 中的按钮设置 Action 处理程序

标签 java css javafx pdfhtml

我正在尝试使用 Javafx 设置操作处理程序,但在编译代码时出现错误。

我正在创建一个网站,我决定使用 JavaFX 来运行我的 Java 代码,因为它支持 CSS。发布的代码是一个简单的测试,以查看是否一切正常。到目前为止,一切正常,但是当我设置操作处理程序时出现错误。 “引起:javafx.fxml.LoadException:”。我希望在用户按下按钮时在控制台上打印“Hello World”。

Controller

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$launchApplication$154(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$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(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 有两个问题:

  • 缺少 fx 命名空间的命名空间声明,导致属性 fx:controller 出错
  • 您只能在根元素上指定 Controller 。不过,您可以在它的顶部子级指定 Controller 。
  • 当然也需要导入处理说明,但我假设您在发布的代码段中遗漏了它们。

您发布的 fxml 片段的第一部分应更改为:

<BorderPane xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafx.Controller">
    <top>
        <BorderPane styleClass="header-section">

关于java - 尝试为 JavaFX 中的按钮设置 Action 处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55719248/

相关文章:

java - 如何解决这个 Gradle 构建问题?

java - 在 FTP 服务器 (Commons-net) 中创建文本文件

html - 关于html中img标签的问题

java - 如何使用 Java 11 运行已编译的 Java 8 JavaFX 应用程序 jar?

java - 如何减慢时间轴关键帧持续时间

java - 访问 JavaFX 中的 Controller ,然后执行另一个类中的函数

java - 简单的 Java OAuth 示例

java - java注释中如何读取unicode字符

html - 如何制作可滚动的动画下拉菜单?

css - 如何在 headless Chrome 中更改纸张大小 --print-to-pdf