按Enter键进入文本字段时激活JButton

时间:2017-04-28 05:09:22

标签: jbutton jtextfield keylistener

花了一个星期的时间,我意识到这对我来说并不容易。我试图做的就是激活“计算”'在' Enter'之后的JButton键是按下的。我非常确定我应该再次使用ActionListener类,但就我的知识而言,这是我的意思。

package mincoinsgui;

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

class MinCoinsGui extends JFrame
{

    //init
    private static final int QUARTERS = 25;
    private static final int DIMES = 10;
    private static final int NICKELS = 5;

    //change variables
    private int quarters = 0;
    private int dimes = 0;
    private int nickels = 0;
    private int change = 0;

    //change labels
    private JLabel quartersLabel;
    private JLabel dimesLabel;
    private JLabel nickelsLabel;
    private JLabel penniesLabel;

    //text field
    private JTextField enterCoinsTF = new JTextField(2);

    public MinCoinsGui() //contructor method
    {
        // 1. Create/initialize components
        JButton calculateBtn = new JButton("Calculate");
        calculateBtn.addActionListener(new CalculateBtnListener());

        JButton clearBtn = new JButton("Clear");
        clearBtn.addActionListener(new ClearBtnListener());

        enterCoinsTF.setEditable(true);


        // 2. Create panel panel / set grid layout
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(6, 2, 2, 2));

        // 3. Add components to panel
        quartersLabel = new JLabel("");
        dimesLabel = new JLabel("");
        nickelsLabel = new JLabel("");
        penniesLabel = new JLabel("");

        panel.add(new JLabel("Enter Change (1-99):"));
        panel.add(enterCoinsTF);
        panel.add(new JLabel("Quarters"));
        panel.add(quartersLabel);
        panel.add(new JLabel("Dimes"));
        panel.add(dimesLabel);
        panel.add(new JLabel("Nickels"));
        panel.add(nickelsLabel);
        panel.add(new JLabel("Pennies"));
        panel.add(penniesLabel);
        panel.add(calculateBtn);
        panel.add(clearBtn);

        setContentPane(panel); //set window (JFrame) characteristics

        pack(); //do layout
        setTitle("Minimum Coins");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

    }

    class CalculateBtnListener implements ActionListener //Calculate Button Listener
    {

        public void actionPerformed(ActionEvent e)
        {
            try
            {
                String dyStr = enterCoinsTF.getText();
                change = Integer.parseInt(dyStr);
                while (change >= QUARTERS)
                { //Subtact & Count Quarters
                    change = change - QUARTERS;
                    quarters++;
                } //Subtract & Count Dimes
                while (change >= DIMES)
                {
                    change = change - DIMES;
                    dimes++;
                } //Subtract & Count Nickels
                while (change >= NICKELS)
                {
                    change = change - NICKELS;
                    nickels++;
                }

                //output results in JLabels
                quartersLabel.setText("" + quarters);
                dimesLabel.setText("" + dimes);
                nickelsLabel.setText("" + nickels);
                penniesLabel.setText("" + change);

                quarters = 0;
                nickels = 0;
                dimes = 0;
            } catch (NumberFormatException f) //disable letters from being entered
            {
                enterCoinsTF.setText("Invalid");
                enterCoinsTF.requestFocus();
                enterCoinsTF.selectAll();
            }
        }

    }

    class ClearBtnListener implements ActionListener //ClearBtnListener
    {
        //Clear and set focus to text field
        public void actionPerformed(ActionEvent e)
        {
            enterCoinsTF.setText("");
            enterCoinsTF.requestFocus();
        }
    }

    public static void main(String[] args)//main method
    {
        MinCoinsGui window = new MinCoinsGui();//instantiate window object
        window.setVisible(true);
    }
}

1 个答案:

答案 0 :(得分:0)

不需要ActionListener,只需按下the default button for your window按钮:

即可
setDefaultButton(calculateBtn);

然后按钮将获得一个独特的外观,当您在另一个控件(例如TextBox)上按Return键时将调用该按钮。