Java Swing 计算器

标签 java swing user-interface calculator

我是一名 Java Noob,正在研究 GUI 计算器,但我刚刚来到这里.. 我已经有了按钮,我需要绑定(bind)这些数字并存储在运算符 ( + - */) 之间的某个位置以显示在我的 JTextArea 上。如何从按钮获取值并应用公式?

感谢您的宝贵时间...:)

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

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class Compute implements ActionListener{

    public String firstNumber;
    public String secondNumber;
    public String resultNumber;
    public double result = 0.0;

    JFrame frame;
    JPanel panel;
    JButton btn_0,btn_1,btn_2,btn_3,btn_4,btn_5,btn_6,btn_7,btn_8,btn_9,btn_C,btn_Add,btn_Sub,btn_Mul,btn_Div,btn_E;
    JTextArea area;

    public Compute() {
        area = new JTextArea();
        frame = new JFrame();
        panel = new JPanel();
        btn_0 = new JButton("0");
        btn_0.addActionListener(this);
        btn_1 = new JButton("1");
        btn_1.addActionListener(this);
        btn_2 = new JButton("2");
        btn_2.addActionListener(this);
        btn_3 = new JButton("3");
        btn_3.addActionListener(this);
        btn_4 = new JButton("4");
        btn_4.addActionListener(this);
        btn_5 = new JButton("5");
        btn_5.addActionListener(this);
        btn_6 = new JButton("6");
        btn_6.addActionListener(this);
        btn_7 = new JButton("7");
        btn_7.addActionListener(this);
        btn_8 = new JButton("8");
        btn_8.addActionListener(this);
        btn_9 = new JButton("9");
        btn_9.addActionListener(this);
        btn_C = new JButton("C");
        btn_C.addActionListener(this);
        btn_Add = new JButton("+");
        btn_Add.addActionListener(this);
        btn_Sub = new JButton("-");
        btn_Sub.addActionListener(this);
        btn_Mul = new JButton("x");
        btn_Mul.addActionListener(this);
        btn_Div = new JButton("/");
        btn_Div.addActionListener(this);
        btn_E = new JButton("=");
        btn_E.addActionListener(this);
        frame.setLayout(null);
        frame.setSize(300,400);
        frame.setTitle("GUI_Calculator");
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.add(btn_C).setBounds(90, 50, 40, 40);
        frame.add(btn_E).setBounds(130, 50, 40, 40);
        frame.add(btn_0).setBounds(50, 50, 40, 40);
        frame.add(btn_1).setBounds(50, 90, 40, 40);
        frame.add(btn_2).setBounds(90, 90, 40, 40);
        frame.add(btn_3).setBounds(130, 90, 40, 40);
        frame.add(btn_4).setBounds(50, 130, 40, 40);
        frame.add(btn_5).setBounds(90, 130, 40, 40);
        frame.add(btn_6).setBounds(130,130, 40, 40);
        frame.add(btn_7).setBounds(50, 170, 40, 40);
        frame.add(btn_8).setBounds(90, 170, 40, 40);
        frame.add(btn_9).setBounds(130, 170, 40, 40);
        frame.add(btn_Add).setBounds(170, 170, 40, 40);
        frame.add(btn_Sub).setBounds(170, 130, 40, 40);
        frame.add(btn_Mul).setBounds(170, 90, 40, 40);
        frame.add(btn_Div).setBounds(170, 50, 40, 40);

        frame.add(area).setBounds(50, 20, 160, 25);


        frame.setVisible(true);
    }

    public static void main(String[] args) {

        new Compute();
    }


    @Override
    public void actionPerformed(ActionEvent e) {


        if(e.getSource() == btn_0)
        {
            area.append("0");
        }
        if(e.getSource() == btn_C)
        {
            area.setText(null);
        }
        if(e.getSource() == btn_1)
        {
            area.append("1");
        }
        if(e.getSource() == btn_2)
        {
            area.append("2");
        }
        if(e.getSource() == btn_3)
        {
            area.append("3");
        }
        if(e.getSource() == btn_4)
        {
            area.append("4");
        }
        if(e.getSource() == btn_5)
        {
            area.append("5");
        }
        if(e.getSource() == btn_6)
        {
            area.append("6");
        }
        if(e.getSource() == btn_7)
        {
            area.append("7");
        }
        if(e.getSource() == btn_8)
        {
            area.append("8");
        }
        if(e.getSource() == btn_9)
        {
            area.append("9");
        }
        if(e.getSource() == btn_E)
        {
        }


    }






}

最佳答案

由于您是 Java 初学者,因此从长远来看,这里有一些技巧可以让您的生活更轻松

  • 通过私有(private)函数初始化您的按钮:

    Private void initializeButtons(){
        //Initialize your buttons...
    }
    

    从构造函数中调用该函数。

  • 对监听器进行同样的操作

  • 在您的 actionPerformed 函数中,将除第一个之外的所有其他 if-case 设为 else if.. 这样可以节省更多的计算时间,因为你不会强制你的程序运行所有的 if-cases,即使它可能会发现第一个情况是合理的。

回到你的问题..只需调用:

String text = theButton.getText();

然后,获取该文本并通过将其解析为“int”来对其进行计算。

<=>

int tempNum = Integer.parseInt(text);

进行必要的计算并将结果保存在您想要的任何位置。

祝你好运!

关于Java Swing 计算器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27809094/

相关文章:

java - 请帮助我在我的 Java 应用程序中过滤掉 Apache Log4j 的第三方日志记录

java - 创建颜色数组然后将其用于 Jbuttons?

java - 如何在 Java 中的 JTextArea 类中添加滚动条?

java - 将 JFrame 加载到 html 中

python - Tkinter 在 Selenium 进程中卡住

java - RESTEasy Mock 对比异常映射器对比上下文

java - 在java中如何提供在压缩和解压缩时使用小区域?

java - 如何从另一个 GUI 访问元素?

java - 康威的生命游戏模拟 - 绘图问题

python - 如何设计多线程GUI网络应用程序?