首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >如何用循环使程序复位回到开头

如何用循环使程序复位回到开头
EN

Stack Overflow用户
提问于 2014-12-10 03:09:28
回答 1查看 197关注 0票数 1

我有一个简单的货币转换程序,允许用户输入三个文本框中的一个,以便将它们的金额转换为美元。我希望用户能够输入他们想输入的信息多少次,所以我创建了一个名为JButtoncontinueButton,但是我无法正确地完成循环并重新设置程序。

问题:我如何写一个循环语句使这个程序从一开始就开始,允许用户再次输入数字?

代码语言:javascript
代码运行次数:0
运行
复制
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);
        } 

        }

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-10 03:20:49

建议:

  • 只使用一个JTextField输入窗口。
  • 在此窗口左侧使用JLabel提示符。
  • 有三种换算JButtons,每种货币一种。
  • 或者一个转换JButton和3个JRadioButtons,以允许用户选择要转换的货币。
  • 没有必要循环或重置。只要简单地进行转换,每次按下按钮就会显示出来--就是这样。
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27392714

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档