为另一个类的Object(作为组件)内的组件创建动作事件到javaFx中创建对象的类中?

时间:2015-09-02 13:46:11

标签: java object javafx javafx-2 actionevent

CustomItem类:

public class MenuItem {
private GridPane menuItem;
private Label itemCode,itemPrice;
private TextArea itemName;

public MenuItem(){
    itemCode=new Label();
    itemName=new TextArea();
    itemPrice=new Label();
}

public GridPane getMenuItemBox(){
    menuItem=new GridPane();

    menuItem.add(itemCode, 0, 0);
    menuItem.add(itemName, 0, 1);
    menuItem.add(itemPrice, 0, 2);


    return menuItem;
}

控制器类:(将创建并添加上述类的对象)

public class ItemController implements Initializable{
@FXML
private FlowPane mainCategory,itemSection;

@Override
public void initialize(URL arg0, ResourceBundle arg1) {
for(int i=0;i<30;i++){
        MenuItem mi=new MenuItem();
        itemSection.getChildren().add(mi.getMenuItemBox());

    }
  }
}

现在我要添加

setOnMouseClicked(new EventHandler<MouseEvent>() {
  @Override
    public void handle(MouseEvent t) {
        System.out.println("click successful !");
    }
});

MenuItem类中定义的标签的操作。我怎么能在ItemController班级做到这一点? 我希望action events应该在ItemController中,因为我们为已经定义的组件创建(button,label,etc.) 这可能类似于mi.getItemCodeLabel.setOnMouseClicked.

我怎么能做到这一点? 提前谢谢。

1 个答案:

答案 0 :(得分:0)

只需在MenuItem类中定义方法即可注册处理程序。 e.g。

public class MenuItem {

    private GridPane menuItem;
    private Label itemCode,itemPrice;
    private TextArea itemName;

    public MenuItem(){
        itemCode=new Label();
        itemName=new TextArea();
        itemPrice=new Label();
    }

    public GridPane getMenuItemBox(){
        menuItem=new GridPane();

        menuItem.add(itemCode, 0, 0);
        menuItem.add(itemName, 0, 1);
        menuItem.add(itemPrice, 0, 2);


        return menuItem;
    }

    public <E extends Event> void addCodeHandler(EventType<E> eventType, EventHandler<E> handler) {
        itemCode.addEventHandler(eventType, handler);
    }

    public <E extends Event> void addPriceHandler(EventType<E> eventType, EventHandler<E> handler) {
        itemPrice.addEventHandler(eventType, handler);
    }

    public <E extends Event> void removeCodeHandler(EventType<E> eventType, EventHandler<E> handler) {
        itemCode.removeEventHandler(eventType, handler);
    }

    public <E extends Event> void removePriceHandler(EventType<E> eventType, EventHandler<E> handler) {
        itemPrice.removeEventHandler(eventType, handler);
    }

}

然后你可以做

menuItem.addCodeHandler(MouseEvent.MOUSE_CLICKED, e -> 
    System.out.println("Click successful!"));

这种方法可以避免暴露您的类中的标签,无论是作为字段还是通过方法。

以下是一个完整的示例,您可以运行它:

import javafx.application.Application;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.event.EventType;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class CustomComponentWithEventHandling extends Application {

    @Override
    public void start(Stage primaryStage) {
        FlowPane itemSelection = new FlowPane();
        for (int i = 1 ; i <= 10; i++)  {
            MenuItem item = new MenuItem();
            String itemCode = "Item "+i;
            item.setItemCode(itemCode);
            item.setItemPrice(100);

            item.addCodeHandler(MouseEvent.MOUSE_CLICKED, e -> {
                System.out.println("Clicked on label for "+itemCode);
            });

            item.addPriceHandler(MouseEvent.MOUSE_CLICKED, e -> {
                System.out.println("Clicked on price for "+itemCode);
            });

            itemSelection.getChildren().add(item.getMenuItemBox());
        }

        Scene scene = new Scene(new ScrollPane(itemSelection), 800, 800);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public class MenuItem {

        private GridPane menuItem;
        private Label itemCode,itemPrice;
        private TextArea itemName;

        public MenuItem(){
            itemCode=new Label();
            itemName=new TextArea();
            itemPrice=new Label();
        }

        public GridPane getMenuItemBox(){
            menuItem=new GridPane();

            menuItem.add(itemCode, 0, 0);
            menuItem.add(itemName, 0, 1);
            menuItem.add(itemPrice, 0, 2);


            return menuItem;
        }

        public void setItemCode(String itemCode) {
            this.itemCode.setText(itemCode);
        }

        public void setItemPrice(double itemPrice) {
            this.itemPrice.setText(String.valueOf(itemPrice));
        }

        public <E extends Event> void addCodeHandler(EventType<E> eventType, EventHandler<E> handler) {
            itemCode.addEventHandler(eventType, handler);
        }

        public <E extends Event> void addPriceHandler(EventType<E> eventType, EventHandler<E> handler) {
            itemPrice.addEventHandler(eventType, handler);
        }

        public <E extends Event> void removeCodeHandler(EventType<E> eventType, EventHandler<E> handler) {
            itemCode.removeEventHandler(eventType, handler);
        }

        public <E extends Event> void removePriceHandler(EventType<E> eventType, EventHandler<E> handler) {
            itemPrice.removeEventHandler(eventType, handler);
        }

    }

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