简体   繁体   English

如何从同一文本字段中获得两个值?

[英]How do I get two values from same text field?

I am new to GUI and trying to make a calculator in JFrame but whenever I perform the addition operation it says that the string is empty.If I remove "jTextField1.setText("");" 我是GUI的新手,正在尝试在JFrame中创建一个计算器,但是每当执行加法运算时,它都说该字符串为空。如果删除了"jTextField1.setText("");" part it works fine but the previous text is still on the text field. 部分可以正常工作,但先前的文本仍在文本字段中。 Is there a way I could create another instance of text field or something like that to accommodate the second string? 有没有办法创建文本字段的另一个实例或类似的东西来容纳第二个字符串?

 private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {                                          
        a=Float.parseFloat(jTextField1.getText());
         jTextField1.setText("");
        b=Float.parseFloat(jTextField1.getText());
        res=a+b; 

    }  

I think you need this code. 我认为您需要此代码。

enter code here

import java.awt.*;
import javax.swing.*;

class Calculation_ActionEvent extends JFrame implements ActionListener {
JFrame f;
JLabel l;
JTextField tf1, tf3;
JButton b1, b2, b3, b4, b5, b6;

Calculation_ActionEvent(String s) {
    f = new JFrame(s);
    f.setLayout(null);
    l = new JLabel("Enter two numbers : ");
    tf1 = new JTextField();
    // tf2 = new JTextField();
    tf3 = new JTextField();
    b1 = new JButton("+");
    b2 = new JButton("-");
    b3 = new JButton("*");
    b4 = new JButton("/");
    b5 = new JButton("equals");
    b6 = new JButton("C");
    f.add(l);
    f.add(tf1);
    f.add(tf3);
    f.add(b1);
    f.add(b2);
    f.add(b3);
    f.add(b4);
    f.add(b5);
    f.add(b6);
    tf1.setBounds(160, 100, 250, 30);
    // tf2.setBounds(320,100,50,30);
    tf3.setBounds(250, 420, 50, 30);
    b1.setBounds(250, 180, 50, 30);
    b2.setBounds(350, 260, 50, 30);
    b3.setBounds(150, 260, 50, 30);
    b4.setBounds(250, 340, 50, 30);
    b5.setBounds(230, 260, 90, 30);
    b6.setBounds(420, 100, 50, 30);
    b1.addActionListener(this);
    b2.addActionListener(this);
    b3.addActionListener(this);
    b4.addActionListener(this);
    b5.addActionListener(this);
    b6.addActionListener(this);
    f.setSize(550, 550);
    f.setVisible(true);
}

int total = 0;
String lastAction = "";

public void actionPerformed(ActionEvent e) {
    int num = 0;
    try {
        num = Integer.parseInt(tf1.getText());
    } catch(NumberFormatException ex) {}
    String s1 = e.getActionCommand();
    if (s1.equals("C")) {
        tf1.setText("");
        tf3.setText("0");
        tf1.requestFocus();
        total = 0;
    } else if (s1.equals("equals")) {
        tf3.setText(String.valueOf(calc(total, lastAction, num)));
        tf1.setText("");
        tf1.requestFocus();
        total = 0;
    } else {
        total = total==0 ? num : calc(total, lastAction, num);
        tf3.setText(String.valueOf(total));
        tf1.setText("");
        tf1.requestFocus();
        lastAction = s1;
    }
}

private int calc(int num1, String action, int num2) {
    switch (action) {
    case "+":
        return num1 + num2;
    case "-":
        return num1 - num2;
    case "*":
        return num1 * num2;
    case "/":
        return num1 / num2;
    default:
        return num1;
    }
}

public static void main(String... s) {
    new Calculation_ActionEvent("Calculator");
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将文本从计算得出的字符串传递到TextView字段? - How do I get text from a calculated String to a TextView field? 我如何教杰克逊从两个分离的 JSON 对象中获取相同的字段? - How do I teach Jackson to source the same field from two disjunctive JSON objects? 如何检索从HTTP POST到PHP的两个字段值? - How do I retrieve two field values received from an HTTP POST to PHP? 如何在 GET 请求中检索同一字段的多个值 - How can I retrieve many values of a same field in a GET request 如何将具有相同键的两个 HashMap 中的值(不同数据类型)放入新的第三个 HashMap? - How do I put in the values (of different data types) from two HashMaps with the same keys into a new third HashMap? 如何从调用同一个变量的两个线程中获得相同的值? - How do I get an identical value from two threads calling on the same variable? 如何使用文本字段在GUI中获得用户输入? - How do I get user input in a GUI using text field? 如何将用户名和密码字段输入Blackberry文本框? - How do I get the username and password field into a Blackberry Text Box? 如何使用XSL从XML中的同一变量获取不同的值 - How do I get different values from the same variable in XML using XSL 如何使用 Java 从 MongoDB 中的所有文档中获取相同键的值? - How do I get values of same key from all documents in MongoDB using Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM