Javafx 代码- main class 错误?
Javafx code- main class error?
以下是我的 Javafx 代码。它不能正常工作,似乎是找不到主要 class 的错误。当我 运行 它作为 Java 文件时,我收到错误 "Error: Could not find or load main class 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
添加到 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);
以下是我的 Javafx 代码。它不能正常工作,似乎是找不到主要 class 的错误。当我 运行 它作为 Java 文件时,我收到错误 "Error: Could not find or load main class 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
添加到 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);