【问题标题】:Get text from JTextField and convert to a double从 JTextField 获取文本并转换为双精度
【发布时间】:2019-11-25 15:33:09
【问题描述】:

如何编写代码以从 JTextField 获取文本并转换为双精度值?

我创建了一个银行账户类和一个带有amountField 的银行账户GUI 来显示我希望提取和存入的金额。 使用public void actionPerformed(ActionEvent e) 方法如何编写代码以从amountField 获取文本并转换为双精度?

我想输入帐户详细信息并能够在将值存储在字符串中的同时进行提款和存款

  • 为存款按钮编写事件处理程序
  • 为撤回按钮编写事件处理程序

public class BankAccountGUI extends JFrame implements ActionListener
{
    private Label amountLabel = new Label("Amount");
    private JTextField amountField = new JTextField(5);
    private JButton depositButton = new JButton("DEPOSIT");
    private JButton withdrawButton = new JButton("WITHDRAW");
    private Label balanceLabel = new Label("Starting Balance = 0" );

    private JPanel topPanel = new JPanel();
    private JPanel bottomPanel = new JPanel();
    private JPanel middlePanel = new JPanel();


    BankAccount myAccount = new BankAccount("James","12345");

    // declare a new BankAccount object (myAccount) with account number and name of your choice here


    public BankAccountGUI()
    {
        setTitle("BankAccount GUI");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setSize(340, 145);
        setLocation(300,300);
        depositButton.addActionListener(this);
        withdrawButton.addActionListener(this);

        topPanel.add(amountLabel);
        topPanel.add(amountField);
        bottomPanel.add(balanceLabel);
        middlePanel.add(depositButton);
        middlePanel.add(withdrawButton);


        add (BorderLayout.NORTH, topPanel);
        add(BorderLayout.SOUTH, bottomPanel);
        add(BorderLayout.CENTER, middlePanel);

        setVisible(true);
    }


    public void actionPerformed(ActionEvent e)
    {
         //What goes here?
    }
}

【问题讨论】:

  • 最好分享你自己的研究,看看你尝试了什么,什么没用。
  • 我实际上不知道如何从金额字段中获取文本
  • 为了将来参考,尽量将问题保留在每个帖子中。您在这里问了两个问题:如何从 JTextField 中获取值,以及如何将 String 转换为 Double。强烈建议您在发布之前尝试搜索您的问题。尤其是使用 Java,在这里找到有用的东西的几率非常高。编码快乐!

标签: java string swing double jtextfield


【解决方案1】:

您可能会想尝试使用 Double.parseDouble(string)。 例如:

String str = "12.0";
Double e = Double.parseDouble(str);
        System.out.println(e * 1.5); //returns 18  

【讨论】:

【解决方案2】:

这里有很多事情你需要做不同的事情。 简而言之,您正在寻找“JTextField.getText()”和“Double.parseDouble()”

depositButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        //Get the Text Value out of the UI Component
        String rawAmount=amountField.getText();

        //Convert that value to Double
        try{
            Double convertedValue = Double.parseDouble(rawAmount);
            //Do stuff with the convertedValue
        }catch(Exception e){
            System.out.println("Not A Double Value");
        }
    }
});

您还应该为每个按钮使用单独的侦听器,以免您尝试以编程方式确定哪个按钮生成了事件。

请参阅JTextField 的文档

【讨论】:

  • 感谢您的回复。但是你能解释一下这将如何将输入存储到字符串中吗?这一切让我现在理解起来很困惑
  • 如果您查看我调用 getText() 的那一行:这称为赋值操作。 GetText 从字段中返回字符串,然后将其存储在 = 左侧声明的变量中
猜你喜欢
  • 2016-07-29
  • 1970-01-01
  • 2021-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多