In: Computer Science
Hello I'm currently having an error that says "main is not abstract and does not override abstract method" is there a way to fix the error?
import java.io.IOException;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
import static javafx.application.Application.launch;
import javafx.scene.layout.StackPane;
public class Main extends Application implements
EventHandler<ActionEvent>{
public static void main(String[] args){
launch(args);
}
@Override
public void Start(Stage stage){
stage.setTitle("Test");
Button calcButton = new
Button("Steam");
StackPane root = new
StackPane();
calcButton.setOnAction(this);
Scene scene = new
Scene(root, 100, 100);
stage.setScene(scene);
stage.setTitle("Test");
stage.show();
}
class CalcButtonHandle {
public void
handle(ActionEvent event) throws IOException
{
Main fo = new
Main();
fo.runOt("D:\\Steam\\Steam.exe");
}
}
public void runOt(String path) throws
IOException{
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " +
path);
}
}
If an interface is implemented by a class, it is necessary for the class to have function body for all the function definitions of the interface, unless the class is abstract or an interface itself.
The Main class implements EventHandler<ActionEvent> interface, therefore, it must also have the body of handle(ActionEvent event) function which is a function definition in EventHandler interface.
There was no need of a separate class CalcButtonHandle to wrap the handle() function. Below is the edited code (in bold), hope it helps!
Code:
import java.io.IOException;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
import static javafx.application.Application.launch;
import javafx.scene.layout.StackPane;
public class Main extends Application implements
EventHandler<ActionEvent>{
public static void main(String[] args){
launch(args);
}
@Override
public void Start(Stage stage){
stage.setTitle("Test");
Button calcButton = new
Button("Steam");
StackPane root = new
StackPane();
calcButton.setOnAction(this);
Scene scene = new
Scene(root, 100, 100);
stage.setScene(scene);
stage.setTitle("Test");
stage.show();
}
public void handle(ActionEvent event)
throws IOException
{
Main fo = new
Main();
fo.runOt("D:\\Steam\\Steam.exe");
}
public void runOt(String path) throws
IOException{
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " +
path);
}
}