【问题标题】:How to fix this error in the following code?如何在以下代码中修复此错误?
【发布时间】:2018-10-05 09:18:16
【问题描述】:

我有一个密码。它有错误,我想知道如何修复错误。错误对应这一行:public void start(Stage primaryStage)

代码如下:

import javafx.application.Application;
import javafx.geometry.Insets;

import javafx.scene.control.Label;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;

import javafx.scene.image.ImageView;

import javafx.scene.layout.BorderPane;
import javafx.scene.Scene;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class DescriptionPane extends BorderPane{
    /** Label for displaying an image and a title */
    private Label lblImageTitle = new Label();

    /** Text area for displaying text */
    private TextArea taDescription = new TextArea();

    public DescriptionPane() {
        // Center the icon and text and place the text under the icon
        lblImageTitle.setContentDisplay(ContentDisplay.TOP);
        lblImageTitle.setPrefSize(200, 100);

        // Set the font in the label and the text field
        lblImageTitle.setFont(new Font("SansSerif", 16));
        taDescription.setFont(new Font("Serif", 14));

        taDescription.setWrapText(true);
        taDescription.setEditable(false);

        // Create a scroll pane to hold the text area
        ScrollPane scrollPane = new ScrollPane(taDescription);

        // Place label and scroll pane in the border pane
        setLeft(lblImageTitle);
        setCenter(scrollPane);
        setPadding(new Insets(5, 5, 5, 5));
    }

    /** Set the title */
    public void setTitle(String title) {
        lblImageTitle.setText(title);
    }

    /** Set the image view */
    public void setImageView(ImageView icon) {
        lblImageTitle.setGraphic(icon);
    }

    /** Set the text description */
    public void setDescription(String text ) {
        taDescription.setText(text);
    }

    @Override
    public void start(Stage primaryStage) {
        Scene scene = new Scene(taDescription, 400, 200);
        primaryStage.setTitle("RadioButtonDemo");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        Application.launch(args);
    }
}

我也在此处粘贴错误。错误如下所示。谁能帮我解决这个问题。非常感谢!

线程“main”中的异常 java.lang.RuntimeException:错误:类 DescriptionPane 不是 javafx.application.Application 的子类 在 javafx.graphics/javafx.application.Application.launch(未知来源) 在 DescriptionPane.main(DescriptionPane.java:69)

【问题讨论】:

  • extends Application
  • 根据报错是这个:DescriptionPane extends BorderPane{,应该是这个:DescriptionPane extends java.fx.application.Application{...话虽如此,不知道会不会出现新的问题:D
  • @npinti,不,不对
  • @achAmháin,不,不对

标签: java


【解决方案1】:

代码似乎有缺陷。 start-method 的实现与 setLeft-、setCenter- 和 setPadding-method 的调用相互排斥。最后三个方法属于 BorderPane 类,因此需要这个类作为基类。另一方面,BorderPane 类没有可以被覆盖的启动方法。

由于这是一个 JavaFX 应用程序,因此实现启动方法的类必须派生自 Application-class,因为它已在 cmets 中说明。

假设这是一个 JavaFX 项目,解决问题的最简单方法是:创建一个新的公共类,例如Main,它扩展了 Application 类。在这个类中复制你的 start- 和 main- 方法。在复制的启动方法中,还将“Scene scene = new Scene(taDescription, 400, 200)”替换为“Scene scene = new Scene(new DescriptionPane(), 400 , 200)”。从 DescriptionPane 类中删除 start- 和 main-method。

【讨论】:

    猜你喜欢
    • 2018-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-08
    • 1970-01-01
    相关资源
    最近更新 更多