简体   繁体   English

将JTextField中输入的值传递给ActionListener

[英]Passing entered Value in JTextField to ActionListener

GUI Purpose: GUI目的:

User should be able to input a number into the number field and click on the Square button. 用户应该能够在数字字段中输入数字,然后单击方形按钮。 Once the button is clicked the program then calculates the square of whatever entered number and displays it in the Square field. 单击按钮后,程序将计算输入的任何数字的平方并显示在“平方”字段中。

My Question: How exactly do I pass the number entered to use in the ActionListener class? 我的问题:如何准确传递在ActionListener类中输入的数字?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.ActionListener;

public class ButtonDemo extends JFrame {
    private JButton SquareButton;
    private JButton ResetButton;
    private JLabel NumberLabel;
    private JLabel SquareLabel;

    private JTextField NumberField;
    private  JTextField SquareField;


    public ButtonDemo()
    {

        Container container=getContentPane();
        container.setLayout(new FlowLayout());

        Panel panel1= new Panel();
        Panel panel2= new Panel();
        Panel panel3 = new Panel();
        panel1.setLayout(new FlowLayout());

        SquareButton = new  JButton("Square");
        ResetButton = new  JButton("Reset");

        NumberLabel = new JLabel("Number");
        SquareLabel = new JLabel("Square");

        NumberField = new JTextField();
        NumberField.setColumns(8);

        SquareField = new JTextField();
        SquareField.setColumns(8);

        panel3.add(panel1);
        panel3.add(panel2);

        panel1.add(NumberLabel);
        panel1.add(SquareLabel);
        panel1.add(NumberField);
        panel1.add(SquareField);

        panel2.add(SquareButton);
        panel2.add(ResetButton);



        container.add(panel1);
        container.add(panel2);
        container.add(panel3);

        bhandler bhandler1 = new bhandler();

        SquareButton.addActionListener(bhandler1);
        ResetButton.addActionListener(bhandler1);

        SquareField.addActionListener(bhandler1);
        NumberField.addActionListener(bhandler1);

        setSize(500,500);
        setVisible(true);
    }

    public static void main(String args[])
    {
        ButtonDemo application = new ButtonDemo();
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    private class bhandler implements ActionListener
    {

        public void actionPerformed(ActionEvent event)
        {
            if(event.getSource() == SquareButton)
            {

            }

        }

    }
}//end of  class Buttondemo

You have several options here: 您在这里有几种选择:

  • If you want the action class be a separate class then pass both the NumberField and the Square fields into the bhandler's class constructor; 如果要让动作类成为一个单独的类,则将NumberFieldSquare字段都传递给bhandler's类构造函数;
  • You can also implement the ActionListener as an inner class of the ButtonDemo class. 您还可以将ActionListener实现为ButtonDemo类的内部类。 In this case you'll have the access to the parent class private fields; 在这种情况下,您将有权访问父类的私有字段。
  • My favorite way of defining action listeners is using Java 8 Lambdas. 我最喜欢的定义动作侦听器的方法是使用Java 8 Lambda。 Here's how you add such an action listener: 这是添加此类动作侦听器的方法:

SquareButton.addActionListener(e -> this::handleSquareButtonClicked);

Then add the following method to your ButtonDemo class: 然后将以下方法添加到ButtonDemo类中:

private void handleSquareButtonClicked(ActionEvent event)
{
       <your event handling coded here.>
}

And here's the working example with your code using lambdas: 这是使用lambdas的代码的工作示例:

package examples;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class ButtonDemo extends JFrame 
{
    private JButton squareButton;
    private JButton resetButton;
    private JLabel numberLabel;
    private JLabel squareLabel;

    private JTextField numberField;
    private  JTextField squareField;


    public ButtonDemo()
    {

        Container container=getContentPane();
        container.setLayout(new FlowLayout());

        Panel panel1= new Panel();
        Panel panel2= new Panel();
        Panel panel3 = new Panel();
        panel1.setLayout(new FlowLayout());

        squareButton = new  JButton("Square");
        resetButton = new  JButton("Reset");

        numberLabel = new JLabel("Number");
        squareLabel = new JLabel("Square");

        numberField = new JTextField();
        numberField.setColumns(8);

        squareField = new JTextField();
        squareField.setColumns(8);

        panel3.add(panel1);
        panel3.add(panel2);

        panel1.add(numberLabel);
        panel1.add(squareLabel);
        panel1.add(numberField);
        panel1.add(squareField);

        panel2.add(squareButton);
        panel2.add(resetButton);

        container.add(panel1);
        container.add(panel2);
        container.add(panel3);

        squareButton.addActionListener(e -> handleSquareButtonClick(e));
        resetButton.addActionListener(e -> handleresetButtonClick(e));

        setSize(500,500);
        setVisible(true);
    }

    private void handleresetButtonClick(ActionEvent e) 
    {
        this.numberField.setText(null);
        this.squareField.setText(null);
    }

    private void handleSquareButtonClick(ActionEvent e)
    {
        try
        {
            double number = Double.parseDouble(this.numberField.getText());
            this.squareField.setText(String.valueOf(number * number));
        }
        catch (NumberFormatException ex)
        {
            System.out.println(ex.getMessage());
        }

    }

    public static void main(String args[])
    {
        ButtonDemo application = new ButtonDemo();
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}//end of  class Buttondemo

Please note that I've renamed some variables to adhere the Java naming convention. 请注意,我已经重命名了一些变量以遵守Java命名约定。

In ButtonDemo constructor for attaching the listener , there is no need to attach the listener to the text fields , just for buttons like this : 在用于附加侦听器的ButtonDemo构造函数中,无需将侦听器附加到文本字段,仅对于如下所示的按钮:

SquareButton.addActionListener(new bhandler1(Integer.valueOf(NumberField.getText())));

In the handler class add a constructor like this 在处理程序类中添加这样的构造函数

private class bhandler implements ActionListener
    {
        int userIput ;
        public bhandler(int userInput){
             this.userInput = userInput ;
        }

        public void actionPerformed(ActionEvent event)
        {
            if(event.getSource() == SquareButton)
            {
                // Do what ever you want with userInput 
            }

        }

    }

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

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