I have a simple money conversion program that allows the user to enter in one of three textfields in order to have their amount be converted into USD. I want the user to be able to enter information as many times as they want, so I've created a JButton
called continueButton
, but I'm having trouble making loop correctly and reset the program.
Question: How can I write a loop statement to make this program start at the beginning, allowing the user to enter numbers again?
public class MoneyConversionPanel extends JPanel {
JLabel yenLabel = new JLabel();
JLabel poundLabel = new JLabel();
JLabel euroLabel = new JLabel();
JTextField yenText = new JTextField("Enter Yen amount here:");
JTextField poundText = new JTextField("Enter Pound amount here:");
JTextField euroText = new JTextField("Enter Euro amount here:");
JButton continueButton = new JButton("Click to reset");
JButton yenButton = new JButton("Convert");
JButton poundButton = new JButton("Convert");
JButton euroButton = new JButton ("Convert");
MoneyConversion userInput;
public MoneyConversionPanel()
{
Dimension dimension = new Dimension(1200,1000);
setPreferredSize(dimension);
setBackground(Color.cyan);
yenButton.addActionListener(new buttonListener());
add(yenLabel);
add(poundLabel);
add(euroLabel);
add(yenText);
add(poundText);
add(continueButton);
continueButton.setVisible(false);
add(euroText);
add(yenButton);
add(poundButton);
add(euroButton);
}
private class buttonListener implements ActionListener
{
double conversionDouble;
NumberFormat costFmt = NumberFormat.getCurrencyInstance();
@Override
public void actionPerformed(ActionEvent e) {
do {
for(int i = 0; i < 1; i++)
{
if(e.getSource() == yenButton)
{
userInput = new MoneyConversion(Double.parseDouble(yenText.getText()));
conversionDouble = userInput.convertYen();
yenLabel.setText("" + costFmt.format(conversionDouble));
continueButton.setVisible(true);
}
else if(e.getSource() == poundButton)
{
userInput = new MoneyConversion(Double.parseDouble(poundText.getText()));
conversionDouble = userInput.convertPounds();
poundLabel.setText("" + costFmt.format(conversionDouble));
continueButton.setVisible(true);
}
else if(e.getSource() == euroButton)
{
userInput = new MoneyConversion(Double.parseDouble(euroText.getText()));
conversionDouble = userInput.convertEuro();
euroLabel.setText("" + costFmt.format(conversionDouble));
continueButton.setVisible(true);
}
}
} while(e.getSource() == continueButton);
}
}
}
Suggestions:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.