【问题标题】:How to make program reset back to the beginning with a loop如何使用循环使程序重置回到开头
【发布时间】:2014-12-10 03:09:28
【问题描述】:

我有一个简单的货币转换程序,允许用户输入三个文本字段之一,以便将其金额转换为美元。我希望用户能够根据需要多次输入信息,因此我创建了一个名为 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);
        } 

        }

}

【问题讨论】:

  • 您能缩小到您提出问题的部分吗?
  • 摆脱 do-while 循环,因为它会占用您的 Swing 事件线程。这不是您想要进行事件驱动编程的方式。相反,为什么还要有一个“继续”按钮?只是在他们完成之前不要结束程序。不,不要像@Jonjongot 建议的那样“从一个while循环开始”——这同样糟糕,而且对于事件驱动的程序也不起作用。
  • 我特指actionPerformed body所在部分的底部。
  • @HovercraftFullOfEels 好的。只是说。我什至没有阅读整篇文章-_-
  • @HovercraftFullOfEels,那么我该如何让这个 GUI 允许用户输入任意次数?

标签: java loops user-interface button text


【解决方案1】:

建议:

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

【讨论】:

  • 非常感谢您的帮助!我只是在循环中想多了。
  • @Jordan:不客气。有时很难换档,一下子从线性控制台程序转到事件驱动的 GUI,但一旦你学会了基本概念,它们就永远属于你了。
  • 除此之外,也许@Jordan 可以在计算后将文本字段设置为空!这样会更好看!只需someTextField.setText("");
  • @user3248168:这可以很好地工作,感谢您的建议。或者,如果他想要保留数字但转换为另一个数字的选项,他可以使用 FocusListener 并在 focusGained() 上的 JTextfield 上调用 selectAll(),以便在需要时轻松更改文本。
  • 是的,这看起来会更加用户友好!但可能有点难! :)
猜你喜欢
  • 1970-01-01
  • 2021-04-06
  • 1970-01-01
  • 2015-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多