繁体   English   中英

如何通过循环使程序重置回到开头

[英]How to make program reset back to the beginning with a loop

我有一个简单的货币转换程序,该程序允许用户输入三个文本字段之一,以便将其金额转换为USD。 我希望用户能够输入他们想要的多次信息,因此我创建了一个名为continueButtonJButton ,但是我无法正确进行循环并重置程序。

问题:如何编写循环语句以使该程序从头开始,从而允许用户再次输入数字?

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);
        } 

        }

}

建议:

  • 仅使用一个JTextField输入窗口。
  • 在此窗口的左侧提示一个JLabel。
  • 具有三个转换JButton,每种货币一个
  • 或一个转换JButton和3个JRadioButtons允许用户选择要转换成哪种货币。
  • 无需循环或重置。 只需进行转换并在每次按下按钮时显示它就可以了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM