【发布时间】: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