【问题标题】:Implement a simple calculator GUI using Swing controls使用 Swing 控件实现一个简单的计算器 GUI
【发布时间】:2020-01-28 20:48:03
【问题描述】:

我想用只有一个JTextField 的 Swing 组件制作一个简单的计算器。在将实现ActionEvents 的actionPerformed 方法中,我想知道:当用户输入特定按钮时,执行特定操作需要什么逻辑?

这是我的代码。

package calculator1;

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

import java.awt.event.*;

class MyFirstGUI extends JFrame implements ActionListener {

    int val1 = 0;
    int val2 = 0;
    int sum = 0;

    JTextField t1 = new JTextField(10);

    JButton b1 = new JButton("+");
    JButton b2 = new JButton("*");
    JButton b3 = new JButton("/");
    JButton b4 = new JButton("=");
    int n1, n2;

    public MyFirstGUI() {
        setLayout(new FlowLayout());
        setVisible(true);
        setSize(500, 500);
        JLabel l1 = new JLabel("Result");

        add(l1);
        add(t1);
        add(b1);
        add(b2);
        add(b3);
        add(b4);

        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);
        b4.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e) {
        int val1, val2, sum = 0;

        val1 = Integer.parseInt(t1.getText());
        if (e.getSource() == b1) {
            t1.setText("");

        }
        if (e.getSource() == b4) {
            val2 = Integer.parseInt(t1.getText());
            sum = val1 + val2;
            t1.setText("" + sum);
        }
    }
}

class Test {
    public static void main(String[] args) {
        MyFirstGUI p = new MyFirstGUI();
    }
}

【问题讨论】:

  • 你的意思是像如何加减乘除?您是在询问执行的操作是什么?
  • 如果所有算术运算都涉及两个值,为什么只有一个输入字段(JTextField)?
  • 我在问执行某些操作需要什么逻辑,例如 b1 用于加法,b2 用于乘法等等。我尝试使用 ifs 和 else 但它们都没有在 ActionPerformed 方法中工作
  • 您从 JTextField 读取文本执行计算并将输出设置为 JTextField。
  • 会是一样的。还将您的按钮命名为有意义的名称。

标签: java swing awt calculator jtextfield


【解决方案1】:

希望对您有所帮助!

       import java.util.Arrays;

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

     import java.awt.event.*;

     class MyFirstGUI extends JFrame implements ActionListener {

int val1;
int val2;
int sum = 0;
int num=0;
JTextField t1 = new JTextField(10);

JButton b1 = new JButton("+");
JButton b2 = new JButton("*");
JButton b3 = new JButton("/");
JButton b4 = new JButton("=");
int n1, n2;

public MyFirstGUI() {
    setLayout(new FlowLayout());
    setVisible(true);
    setSize(500, 500);
    JLabel l1 = new JLabel("Result");

    add(l1);
    add(t1);
    add(b1);
    add(b2);
    add(b3);
    add(b4);

    b1.addActionListener(this);
    b2.addActionListener(this);
    b3.addActionListener(this);

}

public void actionPerformed(ActionEvent e) {

    String[]a = {"+","*","/"};
    if (e.getSource() == b1) {
        val1 = Integer.parseInt(t1.getText());
        t1.setText("");
        b4.addActionListener(this);
    }
    else if (e.getSource() == b2) {
        val1 = Integer.parseInt(t1.getText());
        t1.setText("");
        num=1;
    b4.addActionListener(this);
     }
    else if (e.getSource() == b3) {
        val1 = Integer.parseInt(t1.getText());
        t1.setText("");
        num=2;
      b4.addActionListener(this);
    }
    else if (e.getSource() == b4) {
        val2 = Integer.parseInt(t1.getText());

        switch(a[num]) {
        case "+":
            sum = val1+val2;
            System.out.println(val1+val2);
            t1.setText(sum+"");
            break;
        case "*":
            sum = val1*val2;
            System.out.println(val1*val2);
            t1.setText(sum+"");
            break;
        case "/":
            sum = val1/val2;
            System.out.println(val1/val2);
            t1.setText(sum+"");
            break;
        }
       b4.removeActionListener(this);
       val1=0;val2=0;
    }
}
 }

  class answer4 {
public static void main(String[] args) {
    MyFirstGUI p = new MyFirstGUI();
  }
 }

我也加了。

【讨论】:

    猜你喜欢
    • 2012-08-12
    • 1970-01-01
    • 2016-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-16
    • 1970-01-01
    相关资源
    最近更新 更多