java - 如何使用按钮作为 double 据类型将JTextField输入存储为变量?

标签 java swing compiler-errors jtextfield

试图创建一个名为CashInsert的变量为double,然后我做了CashAmounttxt = CashInsert,但是失败了,还如何添加 Action 侦听器,因此当单击按钮时,它将存储插入变量框中的金额?

这是我的代码:

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;

import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class CashPay extends javax.swing.JFrame {
    private JLabel cashamountlbl;
    private JButton Calculatebtn;
    private JLabel CashChangelbl;
    private JTextField CashAmounttxt;
    private double ChangeLeft;
    private double CashInsert;


    /**
    * Auto-generated main method to display this JFrame
    */
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                CashPay inst = new CashPay();
                inst.setLocationRelativeTo(null);
                inst.setVisible(true);
            }
        });
    }

    public CashPay() {
        super();
         CashAmounttxt = CashInsert;
        initGUI();
    }

    private void initGUI() {
        try {
            setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            getContentPane().setLayout(null);

            cashamountlbl = new JLabel();
            getContentPane().add(cashamountlbl);
            cashamountlbl.setText("Enter Cash Amount");
            cashamountlbl.setBounds(41, 65, 105, 34);
            cashamountlbl.setBackground(new java.awt.Color(128,0,255));

            CashAmounttxt = new JTextField();
            getContentPane().add(CashAmounttxt);
            CashAmounttxt.setBounds(215, 71, 87, 23);

            CashChangelbl = new JLabel();
            getContentPane().add(CashChangelbl);
            CashChangelbl.setBounds(190, 117, 158, 109);
            CashChangelbl.setBackground(new java.awt.Color(255,128,64));

            Calculatebtn = new JButton();
            getContentPane().add(Calculatebtn);
            Calculatebtn.setText("Calculate Total");
            Calculatebtn.setBounds(46, 203, 93, 23);

            pack();
            setSize(400, 300);
        } catch (Exception e) {
            //add your error handling code here
            e.printStackTrace();
        }
    }

}

最佳答案

使用Double.toString()将double值转换为字符串,并使用JTextField.setText()将其显示在UI中。对于相反的转换,请使用JTextField.getText()和Double.parseDouble()。

关于java - 如何使用按钮作为 double 据类型将JTextField输入存储为变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29195291/

相关文章:

Java Swing 使用一种方法向框架添加面板,我将如何删除面板?

static_cast< > 中的 C++ 编译错误

java - 如何从终端使用 jar 和 txt 文件编译和运行 Java

java - 迭代 JSTL 中对象的属性,而不是数组列表或集合

java - 如何导入具有使用 "<"和 ">"符号的格式的特定类型的类?

java - 如何将 OutputStream 中的数据放入 ByteBuffer?

java - 缺少返回语句 - int[]

Java swingworker() process() 没有被调用

java - JTable 作为 MVC 框架的一部分

c++ - 将 vector<double> 插入 vector<vector<double>>