如何在javaFX中获取这个@override方法?

时间:2017-01-23 17:11:15

标签: java javafx override

我们正在学习JavaFX,我不在本课中!我现在正在努力学习,但问题是:

如果我使用其他名称编写此init()方法,则此方法不起作用。 我看到它是一个Override如何轻松制作这个Override方法?有人可以说我是JavaFX的来源吗?

以下是我的课程:

public class CounterModel {

    private int counter = 0;

    public int getCounter() {
        return counter;
    }

    public void setCounter(int counter) {
        this.counter = counter;
    }

    public void increase() {
        counter++;
    }
}

package counter;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class CounterUI extends Application  {

    private Button clickme;
    private Label output;
    private static String PREFIX = "Number of Clicks: ";
    private CounterModel model;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void init() throws Exception {
        model = new CounterModel();
        initButton();
        initLabel();
    }

    private void initButton() {
        clickme = new Button("Click Me!");
        clickme.setPrefWidth(100);
        clickme.setPrefHeight(50);
        clickme.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent arg0) {
                model.increase();   
                output.setText(PREFIX+model.getCounter());  
            }}
        );  
    }

    private void initLabel() {
        output=new Label(PREFIX+model.getCounter());
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("Click to Count");
        BorderPane root = new BorderPane(); 
        BorderPane.setAlignment(output, Pos.CENTER);
        root.setCenter(clickme);
        root.setBottom(output);
        primaryStage.setScene(new Scene(root,300,250));
        primaryStage.show();        
    }
}

1 个答案:

答案 0 :(得分:1)

如果要覆盖父类的init方法,则无法更改名称。您应该查看下面的链接,以获得有关OOP和Java的一些知识:https://www.tutorialspoint.com/java/ http://www.javatpoint.com/java-oops-concepts