Javafx代码 - 主类错误?

时间:2015-01-18 20:39:40

标签: java javafx mainclass

以下是我的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);
   }   

}

1 个答案:

答案 0 :(得分:0)

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

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);