java 取得textfield_java – 如何从TextField获取用户输入并将其转换为Double?

我正在使用Eclipse构建计算器而我遇到了麻烦,因为我需要输入2个值.这是我的run类的代码.

import display.Gui;

public class Main {

public static void main(String argsp[]) {

Gui window = new Gui();

double a = 0, b = 0, c = 0;

String operator;

boolean calculate = true;

window.setVisible(true);

window.setSize(500, 400);

window.setResizable(false);

window.setLocationRelativeTo(null);

while (calculate) {

window.textArea_1.append("Enter an equation.\n");

a = Double.parseDouble(window.textField.getText());

operator = window.textField.getText();

b = Double.parseDouble(window.textField.getText());

if (operator.contains("+"))

c = a + b;

if (operator.contains("-"))

c = a - b;

if (operator.contains("*"))

c = a * b;

if (operator.contains("/"))

c = a / b;

if (operator.contains("x^2"))

c = a * a;

if (operator.contains("sqrt"))

c = Math.sqrt(a);

if (operator.contains("%"))

c = a / 100;

window.textArea.append("" + c + "\n");

window.textArea.append("");

window.textArea.append("Would you like to make another calculation? [Yes/No]\n");

String calculation = window.textField.getText();

try {

if (calculation.equalsIgnoreCase("Yes"))

calculate = true;

if (calculation.equalsIgnoreCase("No"))

calculate = false;

} catch (Exception e) {

window.textArea_1.append("Please enter yes or no");

}

}

}

}

这是我的JFrame类:

import java.awt.Dimension;

import java.awt.EventQueue;

import javax.swing.JFrame;

import java.awt.BorderLayout;

import javax.swing.JTextField;

import javax.swing.JTextArea;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

public class Gui extends JFrame {

public JTextArea textArea, textArea_1;

public JTextField textField;

/**

* Launch the application.

*/

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {

try {

Gui frame = new Gui();

frame.setVisible(false);

} catch (Exception e) {

e.printStackTrace();

}

}

});

}

/**

* Create the frame.

*/

public Gui() {

setBounds(100, 100, 450, 300);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

getContentPane().setLayout(new BorderLayout(0, 0));

textField = new JTextField();

textField.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent Ev) {

textArea.append(textField.getText() + "\n");

textField.setText("");

}

});

textField.requestFocus();

getContentPane().add(textField, BorderLayout.SOUTH);

textField.setColumns(10);

textArea = new JTextArea();

textArea.setEditable(false);

textArea.setPreferredSize(new Dimension(215, 200));

getContentPane().add(textArea, BorderLayout.WEST);

textArea_1 = new JTextArea();

textArea_1.setEditable(false);

textArea_1.setPreferredSize(new Dimension(215, 200));

getContentPane().add(textArea_1, BorderLayout.EAST);

}

我尝试使用Double.parseDouble(window.textField.getText());

但那没用.我怎样才能使它工作?

提前致谢.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值