Javafx 代码 - 主类错误



以下是我的Javafx代码。它无法正常工作,并且似乎找不到主类的错误。当我将其作为 Java 文件运行时,出现错误"错误:找不到或加载主类 assignment_11.Assignment_11"。当我将其作为Javafx文件运行时,我会收到一整串错误。 我做错了什么? 谢谢!

package Assignment_11_1;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.shape.Line;
import javafx.geometry.Insets;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.FontPosture;

public class Assignment_11_1 extends Application {
@Override 
public void start(Stage primaryStage) {
   Scene scene = new Scene(new LinePane(), 200, 200);
   primaryStage.setTitle("ShowLine"); 
   primaryStage.setScene(scene);
   primaryStage.show();
}
public static void main(String[] args) {
       Application.launch(args);
   }   
    }

class LinePane extends Pane {
    public LinePane() {
        Line line1 = new Line (10, 10, 10, 200);
        line1.setStrokeWidth(5);
        line1.setStroke(Color.BLUEVIOLET);
        getChildren().add(line1);
        Line line2 = new Line(10, 10, 100, 10);
        line2.setStrokeWidth(5);
        line2.setStroke(Color.CORAL);
        getChildren().add(line2);
        Line line3 = new Line(100, 10, 100, 100);
        line3.setStrokeWidth(5);
        line3.setStroke(Color.BLUE);
        getChildren().add(line3);
        Line line4 = new Line(10, 100, 100, 100);
        line3.setStrokeWidth(5);
        line3.setStroke(Color.DARKGREEN);
        getChildren().add(line3);
}
}

class ShowText extends Application {
    @Override
    public void start(Stage primaryStage) {
        Pane pane = new Pane();
        pane.setPadding(new Insets (5, 5, 5, 5));
        Text text1 = new Text(20, 20, "The grass is always greener on the other side");
        text1.setFont(Font.font("Courier", FontWeight.BOLD, FontPosture.ITALIC, 15));
        pane.getChildren().add(text1);
        Text text2 = new Text(60, 60, "The grass is always greener on the other side");
        text1.setFont(Font.font("Arial", FontWeight.LIGHT, FontPosture.REGULAR, 20));
        pane.getChildren().add(text2);
        Text text3 = new Text(50, 50, "The grass is always greener on the other side");
        text1.setFont(Font.font("Calibri", FontWeight.NORMAL, FontPosture.REGULAR, 25));
        pane.getChildren().add(text3);
        Scene scene = new Scene(pane);
        primaryStage.setTitle("ShowText");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    public void main(String[] args) {
       Application.launch(args);
   }   
}

您将line3添加到根目录两次,从而产生运行时异常

Line line3 = new Line(100, 10, 100, 100);
        line3.setStrokeWidth(5);
        line3.setStroke(Color.BLUE);
        getChildren().add(line3); // <---you can remove this
        Line line4 = new Line(10, 100, 100, 100);
        line3.setStrokeWidth(5);
        line3.setStroke(Color.DARKGREEN);
        getChildren().add(line3);

相关内容

最新更新