如何在JLabel中输出变量

时间:2017-04-20 04:07:15

标签: java swing variables jlabel

我想首先提一下这只是我的第三周编程和我的第一个问题。我有一个任务,我需要让这个程序计算给定数字的最小变化,并在点击计算按钮后显示每个硬币的数量。

我的问题是我需要在哪里进行计算以及如何将结果变量输出到GUI右侧的四分之一/硬币/镍币/便士JLabel字段。

包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;
    private static final int PENNIES = 1;

    int quarters = 0;
    int dimes = 0;
    int nickels = 0;
    int pennies = 0;
    int change = 0;
//text field
    private JTextField changeTF = new JTextField(2);

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

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

//        JButton clearBtn = new JButton("Clear");
//        clearBtn.addActionListener(new ClearBtnListener());
        changeTF.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 panel
        panel.add(new JLabel("Enter Change (1-99):"));
        panel.add(changeTF);
        panel.add(new JLabel("Quarters"));
        panel.add(new JTextField(quarters));
        panel.add(new JLabel("Dimes"));
        panel.add(new JTextField(dimes));
        panel.add(new JLabel("Nickels"));
        panel.add(new JTextField(nickels));
        panel.add(new JLabel("Pennies"));
        panel.add(new JTextField(pennies));
        panel.add(calculateBtn);
        //content.add(clearBtn);

        //set window (JFrame) characteristics
        setContentPane(panel);
        //do layout
        pack();
        setTitle("Minimum Coins");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //center window
        setLocationRelativeTo(null);

    }

    //CalculateBtnListener
    class CalculateBtnListener implements ActionListener
    {

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

        }
    }
}

2 个答案:

答案 0 :(得分:1)

  

我需要在哪里进行计算

嗯,ActionListener是正确的地方

  

如何获得结果变量

您需要将JLabel更改为实例字段,例如changeTF,然后您可以使用actionPerformed方法访问它们,就像您{{1}字段

答案 1 :(得分:1)

声明JLabel:

0x8086

用声明的JLabel替换JLabel:

//init
private JLabel quartersL, dimesL, nickelsL, penniesL;    

将结果输出到JLabel:

// 3. Add components to panel panel
quartersL = new JLabel("Quarters");
dimesL = new JLabel("Dimes");
nickelsL = new JLabel("Nickels");
penniesL = new JLabel("Pennies");

panel.add(new JLabel("Enter Change (1-99):"));
panel.add(changeTF);
panel.add(quartersL);
panel.add(new JTextField(quarters));
panel.add(dimesL);
panel.add(new JTextField(dimes));
panel.add(nickelsL);
panel.add(new JTextField(nickels));
panel.add(penniesL);
panel.add(new JTextField(pennies));
panel.add(calculateBtn);
//content.add(clearBtn);