Java FX新手,但我已经尝试了所有已知知识,并且可以从网上其他人那里找到。我已经验证了JVM的版本为1.8,并清理/重建了整个软件包。
试图在一个名为Painter的类中创建一个程序,该程序一个接一个地显示3个对象,永远在空背景上,直到程序崩溃/关闭。 它由同一包中的Controller类调用/驱动。
Painter中的方法是start(),run()和main(),前两个方法以及类声明本身都有错误。
类声明具有名义错误。 run()被覆盖,但仍然有“必须覆盖或继承超类型方法”的抱怨。 start()有一些错误。我试图使它直接实现Application,但是它仍然抱怨它。
package anonAssignment2; //@author anon, course#
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.*;
import javafx.scene.paint.*;
import javafx.scene.canvas.*;
import javafx.scene.canvas.Canvas;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.scene.text.*;
import javafx.scene.paint.Color;
import javafx.event.EventHandler;
import javafx.event.ActionEvent;
import javafx.animation.KeyFrame;
//Handles animation timing
import javafx.util.Duration;
import java.util.Timer; // ->
import java.lang.*; //Thread.sleep(int milliseconds)
/*
* Painter- handles shape drawing
* Extends application to be within spec, called by controller
* Implements EventHandler for 3000ms changing of shape
*/
public class Painter extends Application {
public static void main(String[] args) {
Application.launch(args);
}
Painter painter = new Painter();
Pane pane = new Pane();
private Canvas canvas = new Canvas(300, 300);
GraphicsContext gc = canvas.getGraphicsContext2D();
/** Method to start continuously displaying GUI shapes/objects
*
*/
@Override
public void run() {
while(true) {
//Always true, runs until program killed/crashes
gc.setStroke(Color.RED);
gc.strokeOval(150, 150, 100, 100); //circle
java.lang.Thread.sleep(3000);
gc.clearRect(100, 100, 100, 100); //For purposes of this program, blank background, clearing rectangle works
gc.setStroke(Color.GREEN);
gc.strokeRect(100, 100, 100, 100); //Square
java.lang.Thread.sleep(3000);
gc.clearRect(100, 100, 100, 100);
gc.setStroke(Color.BLUE);
gc.strokeText("Course # and Title", 100, 140); //Text, approximately centered
java.lang.Thread.sleep(3000);
gc.clearRect(100, 100, 100, 100);
}
}
/**Start command, uses primaryStage instance
* Called by Controller
*
*/
@Override
public void start(Stage primaryStage) implements Application throws Exception {
Pane pane = new Pane();
pane.setPrefWidth(300);
pane.setPrefHeight(300);
Scene scene = new Scene(pane);
primaryStage.setTitle("Rheault Project 2");
primaryStage.setScene(scene);
primaryStage.show();
Controller instance = new Controller(painter);
//Controller constructor with argument of painter instance
}
}
//controller.java class:
//@author anon, course #
package anonAssignment2;
public class Controller extends Thread {
private Painter painter;
public Controller(Painter painter) {
this.painter = painter;
Painter.start();
}
public void run() {
while (true) {
painter.paint();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
''''
答案 0 :(得分:0)
首先,将代码复制到IDE中 您正在尝试从主要内部运行应用程序。 像这样:
public class Painter extends javafx.application.Application {
public static void main(String[] args) {
javafx.application.Application.launch(args);
}
我认为将Painter类分成自己的类会更好。 您仍然可以将这个主要部分保留在自己的主类中,并打破绘画者的逻辑。
然后我看了一下您的代码:
像这样更改启动方法,至少不会给我IDE中的错误:
@Override
public void start(javafx.stage.Stage primaryStage) throws Exception {
javafx.scene.layout.Pane pane = new javafx.scene.layout.Pane();
pane.setPrefWidth(300);
pane.setPrefHeight(300);
javafx.scene.Scene scene = new javafx.scene.Scene(pane);
primaryStage.setTitle("Rheault Project 2");
primaryStage.setScene(scene);
primaryStage.show();
Controller instance = new Controller(painter);
//Controller constructor with argument of painter instance
}
我已经跳过了Implements部分,因为已经很清楚您正在覆盖超类的方法,甚至可以在IDE中单击它,然后它将转到超类方法进行确认。
最后,我还查看了run方法。它不会覆盖任何内容,因为它在超类应用程序中不存在。尝试单击它,您会看到。
public void run
但这也许是因为我复制了错误的代码。好吧,这些是我在查看您的代码时发现的一些东西。希望对您有所帮助。
答案 1 :(得分:0)
这似乎是一项任务,所以我不知道需要什么,不需要什么。如果您不需要Threads
,请不要使用它们。如果可以使用Timeline
,我建议您使用它。该示例应用程序很可能不会执行您需要做的事情,但是希望它可以提供帮助。
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Duration;
/**
*
* @author blj0011
*/
public class JavaFXApplication335 extends Application
{
@Override
public void start(Stage primaryStage)
{
Canvas canvas = new Canvas(300, 300);
GraphicsContext gc = canvas.getGraphicsContext2D();
Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(3), (ActionEvent event1) -> {
gc.setStroke(Color.RED);
gc.strokeOval(150, 150, 100, 100); //circle
System.out.println("running 1");
}), new KeyFrame(Duration.seconds(6), (ActionEvent event1) -> {
gc.clearRect(100, 100, 100, 100); //For purposes of this program, blank background, clearing rectangle works
gc.setStroke(Color.GREEN);
gc.strokeRect(100, 100, 100, 100); //Square
System.out.println("running 2");
//java.lang.Thread.sleep(3000);
}), new KeyFrame(Duration.seconds(9), (ActionEvent event1) -> {
gc.clearRect(100, 100, 100, 100);
gc.setStroke(Color.BLUE);
gc.strokeText("Course # and Title", 100, 140); //Text, approximately centered
System.out.println("running 3");
//java.lang.Thread.sleep(3000);
}), new KeyFrame(Duration.seconds(12), (ActionEvent event1) -> {
gc.clearRect(100, 100, 100, 100);
System.out.println("running 4");
}));
timeline.setCycleCount(Timeline.INDEFINITE);
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction((ActionEvent event) -> {
timeline.play();
});
StackPane root = new StackPane();
root.getChildren().add(new VBox(canvas, btn));
Scene scene = new Scene(root, 500, 500);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}