尝试在JavaFX中为按钮设置动作处理程序

时间:2019-04-17 02:58:47

标签: java css javafx pdfhtml

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

我正在创建一个网站,并且我决定使用JavaFX与Java代码一起运行,因为它支持CSS。发布的代码是一个简单的测试,以查看是否一切正常。到目前为止,一切正常,但是当我设置动作处理程序时,出现错误。 “由于: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$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

1 个答案:

答案 0 :(得分:2)

fxml存在2个问题:

  • 缺少fx名称空间的名称空间声明,从而导致属性fx:controller的错误
  • 只允许在根元素上指定控制器。不过,您可以将控制器指定为顶级子控件。
  • 当然也需要用于导入的处理说明,但是我想您已经在发布的代码段中省略了它们。

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

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