Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在AWT/SWING中从同一文本字段输入两位数字_Java_Swing - Fatal编程技术网

Java 如何在AWT/SWING中从同一文本字段输入两位数字

Java 如何在AWT/SWING中从同一文本字段输入两位数字,java,swing,Java,Swing,我已经创建了一个简单的计算器,在其中我在两个不同的文本字段中获取两个操作数的输入,它运行良好。但我需要从同一文本字段中获取两个输入。我应该做什么改变 import java.awt.*; import java.awt.event.*; import javax.swing.*; class Calculation_ActionEvent extends JFrame implements ActionListener { JFrame f; JLabel l; JTe

我已经创建了一个简单的计算器,在其中我在两个不同的文本字段中获取两个操作数的输入,它运行良好。但我需要从同一文本字段中获取两个输入。我应该做什么改变

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

class Calculation_ActionEvent extends JFrame implements ActionListener
{
    JFrame f;
    JLabel l;
    JTextField tf1, tf2, tf3;
    JButton b1, b2, b3, b4 ,b5, b6;
    Calculation_ActionEvent(String s)
    {
        f = new JFrame("Calculation");
        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(tf2);
        f.add(tf3);
        f.add(b1);
        f.add(b2);
        f.add(b3);
        f.add(b4);
        f.add(b5);
        f.add(b6);
        tf1.setBounds(180,100,50,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(250,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);
    }

    public void actionPerformed(ActionEvent e)
    {
        int num1= Integer.parseInt(tf1.getText());
        int num2= Integer.parseInt(tf2.getText());
        String s1 = e.getActionCommand();
        if(s1.equals("C"))
        {
            tf1.setText("0");
            tf2.setText("0");
            tf3.setText("0");
        }
        else
        {
         if(s1=="+")
        {
            tf3.setText(String.valueOf(num1+num2)); 
        }
        if(s1=="-")
        {
            tf3.setText(String.valueOf(num1-num2)); 
        }
        if(s1=="*")
        {
            tf3.setText(String.valueOf(num1*num2)); 
        }
        if(s1=="/")
        {
            tf3.setText(String.valueOf(num1/num2)); 
        }
        }
 }

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

只需从两个文本框中获取值,然后执行所有的计算加、乘、减、除并存储在不同的变量中,并在文本框中设置该变量,您就不能直接在下面的textbox.ex中执行操作

String val1=textbox1.gettext();
String val2=textbox2.gettext();
int addition=Integer.ParseInt(val1)+Integer.ParseInt(val2);
textbox3.settext(addition.toString());

只需从两个文本框中获取值,然后执行所有的计算加、乘、减、除并存储在不同的变量中,并在文本框中设置该变量,您就不能直接在下面的textbox.ex中执行操作

String val1=textbox1.gettext();
String val2=textbox2.gettext();
int addition=Integer.ParseInt(val1)+Integer.ParseInt(val2);
textbox3.settext(addition.toString());

这是你想要的吗?我为你做了一个快速版本

简单地说,我将最后一个运算符(
+
-
*
/
)存储在
字符串变量(
lastAction
)中,
总计
作为
整数
。当按下“等于”时,我计算出总数并重置
total
变量和
lastAction
。当按下一个运算符时,我会更改总数,但将最后一个值保留在变量中

package cf.pgmann.calc;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

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");
    }
}

这是你想要的吗?我为你做了一个快速版本

简单地说,我将最后一个运算符(
+
-
*
/
)存储在
字符串变量(
lastAction
)中,
总计
作为
整数
。当按下“等于”时,我计算出总数并重置
total
变量和
lastAction
。当按下一个运算符时,我会更改总数,但将最后一个值保留在变量中

package cf.pgmann.calc;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

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");
    }
}

实际上,这是我在Swing中的第一个程序。请帮我完成它。你是说“4+4”、“8”(即一个文本字段中的两个输入)而不是“4”、“4”、“8”吗?你应该包括一个例子,这会有所帮助。我的意思是有一个文本字段,首先我将在其中写入一个操作数,然后按运算符,然后在同一文本字段中写入第二个操作数,同时将第一个值存储在某个变量中。然后在第二个文本字段中打印结果。示例:在文本字段中键入5时,按“+”并在同一文本字段中键入3。。要在第二个文本字段中得到答案8实际上这是我在Swing中的第一个程序。请帮我完成它。你的意思是“4+4”、“8”(即一个文本字段中的两个输入)而不是“4”、“4”、“8”吗?你应该包括一个例子,这会有所帮助。我的意思是有一个文本字段,首先我将在其中写入一个操作数,然后按运算符,然后在同一文本字段中写入第二个操作数,同时将第一个值存储在某个变量中。然后在第二个文本字段中打印结果。示例:在文本字段中键入5时,按“+”并在同一文本字段中键入3。。在第二个文本字段中获得答案8