如何在<ActionEvent>单独的类中实现事件处理程序以获得多个按钮?



我正试图为我的事件处理程序创建一个单独的类文件,因为现在我有相同的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);
}   
}

如有任何帮助,我们将不胜感激。

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

在这种情况下,这是:

  • 按钮编号
  • 标签

这意味着您可以简单地创建一个包含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));

您可以创建一个方法,返回一个处理程序对象并获取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);
}
};
}

最新更新