我有一个简单的货币转换程序,允许用户输入三个文本框中的一个,以便将它们的金额转换为美元。我希望用户能够输入他们想输入的信息多少次,所以我创建了一个名为JButton
的continueButton
,但是我无法正确地完成循环并重新设置程序。
问题:我如何写一个循环语句使这个程序从一开始就开始,允许用户再次输入数字?
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);
}
}
}
发布于 2014-12-09 19:20:49
建议:
https://stackoverflow.com/questions/27392714
复制