如何将用户输入作为double,然后使用它执行计算

时间:2016-10-16 09:13:39

标签: java swing

我正在Java eclipse windowbuilder中创建一个小程序。用户输入不等式语句的左侧(LHS)和右侧(RHS),使得LHS <1。 (m / n)&lt; RHS。程序将输出满足条件的最低整数值m和n。我有这样做的逻辑/代码,但我不确定如何在windowbuilder中获取LHS和RHS的用户值。左侧的文本字段称为lhs,右侧的文本字段称为rhs,您可以在下图中看到。如何获取这些用户的输入(作为双数据类型)?当用户输入这两个值时,我将把代码逻辑放在actionPerformed下。谢谢!

JButton btnCompute = new JButton("Compute");
        btnCompute.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }

enter image description here

 package ntheory;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Ntheory {

    private JFrame frame;
    private JTextField lhs;
    private JTextField rhs;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Ntheory window = new Ntheory();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Ntheory() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        lhs = new JTextField();
        lhs.setBounds(6, 46, 68, 26);
        frame.getContentPane().add(lhs);
        lhs.setColumns(10);

        JLabel label = new JLabel("<");
        label.setBounds(86, 51, 10, 16);
        frame.getContentPane().add(label);

        JLabel label_1 = new JLabel("<");
        label_1.setBounds(159, 51, 10, 16);
        frame.getContentPane().add(label_1);

        rhs = new JTextField();
        rhs.setColumns(10);
        rhs.setBounds(181, 46, 68, 26);
        frame.getContentPane().add(rhs);

        JLabel lblmn = new JLabel("(m/n)");
        lblmn.setBounds(108, 51, 61, 16);
        frame.getContentPane().add(lblmn);

        JButton btnCompute = new JButton("Compute");
        btnCompute.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        btnCompute.setBounds(261, 46, 117, 29);
        frame.getContentPane().add(btnCompute);
    }
}

1 个答案:

答案 0 :(得分:1)

JButton btnCompute = new JButton("Compute");
btnCompute.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        try {
            double leftValue = Double.parseDouble(lhs.getText());
            double rightValue = Double.parseDouble(rhs.getText());
            // Do your stuff with it.
        }
        catch ( NumberFormatException ex ) {
            // Do stuff if the input is not a number.
        }
    }
}