繁体   English   中英

如何在javaFx中连接按钮侦听器

[英]How to connect button listener in javaFx

这个问题看起来很简单,但是我必须以一种特定的方式来实现我的代码,这导致了混乱。 因此,在第 3 步中,我需要向事件处理程序注册源对象。 ButtonHandler 类已经为我设置好了,但我不知道如何连接这些来注册按钮。 我得到的资源似乎使用不同的逻辑来连接 javaFx 事件,我无法在此代码应该使用的逻辑与我得到的逻辑之间建立连接。

如果需要,我可以进一步详细说明并提供更多代码。

import java.util.ArrayList;
import javax.swing.plaf.basic.BasicButtonListener;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
//import all other necessary javafx classes here
//----

public class InputPane extends HBox
{
    //GUI components
    private ArrayList<Laptop> laptopList;

    //The relationship between InputPane and PurchasePane is Aggregation
    private PurchasePane purchasePane;
    //----
    private GridPane Gpane, RightPane;
    private Label label, l2, l3, l4, l5, errL;
    private Button btn1;
    private TextField text, t2, t3, t4, t5;
    private TextArea ta;

    //constructor
    public InputPane(ArrayList<Laptop> list, PurchasePane pPane)
    {
        laptopList = list;
        purchasePane = pPane;

        //Step #1: initialize each instance variable and set up the layout
        //----
        //create a GridPane hold those labels & text fields
        //consider using .setPadding() or setHgap(), setVgap()
        //to control the spacing and gap, etc.
        //----

        Gpane = new GridPane();
        Gpane.setHgap(10);
        Gpane.setVgap(10);
        Gpane.setPadding(new Insets(30, 30, 10, 20));

        label = new Label("Brand");
        l2 = new Label("Model");
        l3 = new Label("CPU(GHz)");
        l4 = new Label("RAM(GB)");
        l5 = new Label("Price($)");

        Gpane.add(label, 0, 0);
        Gpane.add(l2, 0, 1);
        Gpane.add(l3, 0, 2);
        Gpane.add(l4, 0, 3);
        Gpane.add(l5, 0, 4);

        //You might need to create a sub pane to hold the button
        //----
        btn1 = new Button("Enter a Laptop Info");
        btn1.setOnAction(new ButtonHandler());

        Gpane.add(btn1, 1, 5);
        //Set up the layout for the left half of the InputPane.
        //----
        text = new TextField();
        t2 = new TextField();
        t3 = new TextField();
        t4 = new TextField();
        t5 = new TextField();
        Gpane.add(text, 1, 0);
        Gpane.add(t2, 1, 1);
        Gpane.add(t3, 1, 2);
        Gpane.add(t4, 1, 3);
        Gpane.add(t5, 1, 4);

        errL = new Label("");
        errL.setVisible(false);
        Gpane.add(errL, 0, 0);
        //the right half of the InputPane is simply a TextArea object
        //Note: a ScrollPane will be added to it automatically when there are no
        //enough space
        RightPane = new GridPane();
        ta = new TextArea();
        ta.setPromptText("No laptops");
        ta.setPrefColumnCount(30);
        ta.setPrefRowCount(20);
        RightPane.add(ta, 8, 0);
        //Add the left half and right half to the InputPane
        getChildren().add(Gpane);

        getChildren().add(RightPane);
        //Note: InputPane extends from HBox
        //----

        //Step #3: register source object with event handler
        //---

    } //end of constructor

    //Step #2: Create a ButtonHandler class
    //ButtonHandler listens to see if the buttont "Enter a Laptop Info." is
    //pushed or not,
    //When the event occurs, it get a laptop's brand, model, CPU, RAM and price
    //information from the relevant text fields, then create a new Laptop
    //object and add it inside
    //the laptopList. Meanwhile it will display the laptop's information
    //inside the text area.
    //It also does error checking in case any of the textfields are empty or
    // wrong data was entered.
    private class ButtonHandler implements EventHandler<ActionEvent>
    {
        //Override the abstact method handle()
        @Override
        public void handle(ActionEvent e)
        {
            //declare any necessary local variables here
            //---
            String Brand, Model, CPU, RAM, Price;
            Brand = text.getText();
            Model = t2.getText();
            CPU = t3.getText();
            RAM = t4.getText();
            Price = t5.getText();

            //when a text field is empty and the button is pushed
            if
            (text.equals("")||t2.equals("")||t3.equals("")||t4.equals("")||
                    t5.equals(""))
            {
                errL.setText("Empty Fields");
                errL.setVisible(true);
            }

            else    //for all other cases
            {
                try {
                    Laptop lap = new Laptop(Brand, Model, Double.parseDouble(CPU),
                            Double.parseDouble(RAM), Double.parseDouble(Price));
                    laptopList.add(lap);
                    ta.appendText(lap.toString());
                    errL.setText("Laptop added");
                    text.setText(""); t2.setText(""); t3.setText("");
                    t4.setText(""); t5.setText("");
                    //----
                    //at the end, don't forget to update the new arrayList
                    //information on the ListView of the Purchase Pane
                    //----
                    purchasePane.updateLaptopList(lap);
                    //Also somewhere you will need to use try & catch block to catch
                    //the NumberFormatException

                }catch (NumberFormatException l) {
                    System.err.println("Numbers only");
                }
            }

        } //end of handle() method
    } //end of ButtonHandler class
}

使用 Button 类的setOnAction方法来定义当用户单击按钮时会发生什么。

这段代码解释了我们如何在方法中使用匿名类:

button.setOnAction(new EventHandler<ActionEvent>() {
    @Override public void handle(ActionEvent e) {
        label.setText("Accepted");
    }
});

我们可以覆盖自定义类中的 handle 方法并像这样添加它:

button.setOnAction(new CustomHandle());

你应该知道ActionEvent是一种由 EventHandler 处理的事件类型。 EventHandler 对象提供 handle 方法来处理为按钮触发的操作。

您可以使用 Button 类设置尽可能多的事件处理方法,以引起特定行为或应用视觉效果。在这种情况下,我们使用button.addEventHandler(EventType,EventObject)

button.addEventHandler(MouseEvent.MOUSE_ENTERED, 
    new EventHandler<MouseEvent>() {
        @Override public void handle(MouseEvent e) {
            button.setEffect(shadow);
        }
});

如@c0der btn1.setOnAction(new ButtonHandler());所指出的那样,向btn1.setOnAction(new ButtonHandler());注册ButtonHandler btn1.setOnAction(new ButtonHandler());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM