如何在单独的类中为一个以上的按钮实现EventHandler <actionevent>?

时间:2019-02-13 23:42:26

标签: java user-interface javafx

我正在尝试为我的事件处理程序提供一个单独的类文件,因为现在我有相同的6行代码,但数字稍有变化。这是很多重复的代码,我不确定该如何处理。

public class GUIDemo extends Application
{
    private Button btn1 = new Button("Button 1");
    private Button btn2 = new Button("Button 2");
    private Button btn3 = new Button("Button 3");
    private Button btn4 = new Button("Button 4");
    private Button btn5 = new Button("Button 5");
    private Button btn6 = new Button("Button 6");
    private Label lbl1 = new Label();
    private Label lbl2 = new Label();
    private Label lbl3 = new Label();
    private Label lbl4 = new Label();
    private Label lbl5 = new Label();
    private Label lbl6 = new Label();

    @Override
    public void start(Stage primaryStage) throws Exception
    {
        GridPane myPane = new GridPane();
        myPane.setHgap(10);
        myPane.setVgap(10);
        Scene myScene = new Scene(myPane, 500, 300);
        primaryStage.setScene(myScene);
        primaryStage.show();

        primaryStage.setTitle("GUI Demo");

        myPane.setAlignment(Pos.CENTER);

        myPane.add(btn1, 0, 1);
        myPane.add(btn2, 1, 1);
        myPane.add(btn3, 2, 1);
        myPane.add(btn4, 0, 3);
        myPane.add(btn5, 1, 3);
        myPane.add(btn6, 2, 3);

        myPane.add(lbl1, 0, 2);
        myPane.add(lbl2, 1, 2);
        myPane.add(lbl3, 2, 2);
        myPane.add(lbl4, 0, 4);
        myPane.add(lbl5, 1, 4);
        myPane.add(lbl6, 2, 4);

        btn1.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent e) {
                System.out.println("Button 1 clicked");
                lbl1.setText("Button 1 clicked");
            }
        });
        btn2.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent e) {
                System.out.println("Button 2 clicked");
                lbl2.setText("Button 2 clicked");
            }
        });
        btn3.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent e) {
                System.out.println("Button 3 clicked");
                lbl3.setText("Button 3 clicked");
            }
        });
        btn4.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent e) {
                System.out.println("Button 4 clicked");
                lbl4.setText("Button 4 clicked");
            }
        });
        btn5.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent e) {
                System.out.println("Button 5 clicked");
                lbl5.setText("Button 5 clicked");
            }
        });
        btn6.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent e) {
                System.out.println("Button 6 clicked");
                lbl6.setText("Button 6 clicked");
            }
        });
    }

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

我尝试了以下操作(以上代码中未实现),但是它不起作用。它不会在控制台上显示“单击按钮”。我还需要能够打印“已单击按钮#”,因为所有六个按钮的结果都必须不同。

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class ButtonHandler implements EventHandler<ActionEvent>
{
    private final Stage window;
    private final Scene scene;

    ButtonHandler(Stage window, Scene scene)
    {
        this.window = window;
        this.scene = scene;
    }

    @Override
    public void handle(ActionEvent e) {
        System.out.println("Button clicked");
        window.setScene(scene);
    }   
}

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

首先分析事件处理程序需要访问哪些数据。

在这种情况下,这是:

  • 按钮编号
  • 标签

这意味着您可以简单地创建一个包含int字段的类:

public class ButtonHandler implements EventHandler<ActionEvent> {
    private final String text;
    private final Label label;

    public ButtonHandler(int number, Label label) {
        this.text = "Button " + number + " clicked";
        this.label = label;
    }

    @Override
    public void handle(ActionEvent e) {
        System.out.println(text);
        label.setText(text);
    }
}
btn1.setOnAction(new ButtonHandler(1, lbl1));

答案 1 :(得分:0)

您可以创建一个返回处理程序对象并采用String(文本或正义按钮N.O)的方法

private  EventHandler <ActionEvent> getHandler (String str)
{
    return new EventHandler<ActionEvent>(){
        @Override
        public void handle(ActionEvent event) {
            Button b = ((Button)event.getSource());
            b.setText(b.getText()+"have been clicked");
            System.out.print(str);
        }

    };
}