首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在以下代码中修复此错误?

如何在以下代码中修复此错误?
EN

Stack Overflow用户
提问于 2018-10-05 17:18:17
回答 1查看 155关注 0票数 0

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

代码如下所示:

代码语言:javascript
复制
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的子类(源代码为DescriptionPane.main(DescriptionPane.java:69) )

EN

回答 1

Stack Overflow用户

发布于 2018-10-06 03:15:01

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

由于这是一个JavaFX应用程序,因此实现start-method的类必须从Application-class派生,正如注释中已经说明的那样。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52662289

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档