Reputation: 35
I'd like to start off by mentioning this is only my 3rd week of programming and my first question here. I have an assignment where I need to have this program calculate the minimum change for a given number and display the amount of each coin after clicking the calculate button.
My question is where do I need to place the calculations and how to get the resulting variables to output into the quarters/dimes/nickels/pennies JLabel fields on the right side of the GUI.
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;
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;
}
}
}
}
Upvotes: 2
Views: 2615
Reputation: 1433
Declare the JLabels:
//init
private JLabel quartersL, dimesL, nickelsL, penniesL;
Replace the JLabels with the declared JLabels:
// 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);
Output the result into the JLabels:
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;
}
// Update the JLabels to display the result
quartersL.setText("" + quarters);
dimesL.setText("" + dimes);
nickelsL.setText("" + nickels);
penniesL.setText("" + pennies);
}
Upvotes: 1
Reputation: 347204
where do I need to place the calculations
Well, the ActionListener
is the correct place
how to get the resulting variables to output
You need to make the JLabel
s you want to change into instance fields, like changeTF
, then you can access them from your actionPerformed
method, like you are the changeTF
field
Upvotes: 1