GUI用于计算所需的最小硬币数量

时间:2015-04-12 01:14:54

标签: java user-interface

我是GUI构建的新手并且对下面的代码略感混淆......当我运行程序时,它肯定会运行但是当你输入更改量时,它不会计算多少个季度,角钱等等它只是放了一个" 0" in。我查看了我的代码,没有任何错误。

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

public class MinCoinsGUI extends JFrame
{

    private JTextField changeTF = new JTextField(3);
    private JTextField quartersTF = new JTextField(3);
    private JTextField dimesTF = new JTextField(3);
    private JTextField nickelsTF = new JTextField(3);
    private JTextField penniesTF = new JTextField(3);

    public MinCoinsGUI()
    {
        JButton CalcChangeBTN = new JButton("Calculate");
        JButton ClearBTN = new JButton("Clear");

        CalcChangeBTN.addActionListener(new ChangeBTNListener());
        ClearBTN.addActionListener(new ClearBTNListener());

        quartersTF.setEditable(false);
        dimesTF.setEditable(false);
        nickelsTF.setEditable(false);
        penniesTF.setEditable(false);

        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new GridLayout(6, 2, 2, 2));

        mainPanel.add(new JLabel("Enter Change (1-99)"));
        mainPanel.add(changeTF);

        mainPanel.add(new JLabel("Quarters"));
        mainPanel.add(quartersTF);

        mainPanel.add(new JLabel("Dimes"));
        mainPanel.add(dimesTF);

        mainPanel.add(new JLabel("Nickels"));
        mainPanel.add(nickelsTF);

        mainPanel.add(new JLabel("Pennies"));
        mainPanel.add(penniesTF);

        mainPanel.add(CalcChangeBTN);
        mainPanel.add(ClearBTN);

        setContentPane(mainPanel);
        pack();
        setTitle("Minimum Coins");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }

    class ChangeBTNListener implements ActionListener
    {

        public void actionPerformed(ActionEvent e)
        {
            String changeString = changeTF.getText();
            int change = Integer.parseInt(changeString);

            String quartersString = changeTF.getText();
            int quarters = Integer.parseInt(quartersString);

            String dimesString = changeTF.getText();
            int dimes = Integer.parseInt(dimesString);

            String nickelsString = changeTF.getText();
            int nickels = Integer.parseInt(nickelsString);

            String penniesString = changeTF.getText();
            int pennies = Integer.parseInt(penniesString);

            while (change >= 25)
            {
                quarters++;
                change -= 25;
            }

            while (change >= 10)
            {
                dimes++;
                change -= 10;
            }

            while (change >= 5)
            {
                nickels++;
                change -= 5;
            }

            while (change >= 1)
            {
                pennies++;
                change -= 1;
            }

            changeTF.setText("" + change);
        }
    }

    class ClearBTNListener implements ActionListener
    {

        public void actionPerformed(ActionEvent e)
        {
            changeTF.setText("");
        }
    }

    public static void main(String[] args)
    {
        MinCoinsGUI window = new MinCoinsGUI();
        window.setVisible(true);
    }
}

1 个答案:

答案 0 :(得分:0)

您根本没有在quartersTF, dimesTF中设置值,依此类推。所有你正在做的就是计算四分之一,角钱等等的数量,一直在减少change直到它达到零,然后将该零设置回changeTF。如果您想查看quartersTF, dimesTF中的值等,则需要设置它们。