java使用window builder图形界面开发简易计算器
阅读原文时间:2023年07月09日阅读:1

界面效果:

/**
 *
 */
package calculator;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridLayout;
import javax.swing.JLabel;
import java.awt.GridBagLayout;
import javax.swing.JTextField;
import java.awt.GridBagConstraints;
import java.awt.FlowLayout;
import javax.swing.SpringLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
import javax.swing.JSpinner;
import javax.swing.JFormattedTextField;
import javax.swing.JCheckBox;
import javax.swing.JTable;
import javax.swing.JProgressBar;
import javax.swing.JTextArea;
import java.awt.Color;

/**
 * @author Ocean
 *
 */
public class Calc_Main extends JFrame {
    private JTextField textField;
    private JTable table;
    String s = "";// 显示算式
    long[] num = new long[100];// 储存运算数据
    char[] b = new char[100];// 储存运算符
    int i = 0;// 输入运算数据和运算符是给数组计数
    int c;// 计算结果时计数
    long jieguo = 0;// 计算结果

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Calc_Main frame = new Calc_Main();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * 构造方法
     * Create the frame.
     */
    public Calc_Main() {
        setFont(new Font("Dialog", Font.PLAIN, 12));
        setTitle("计算器");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(800, 300, 382, 494);
        getContentPane().setLayout(null);

        textField = new JTextField();
        textField.setForeground(new Color(102, 51, 255));
        textField.setFont(new Font("宋体", Font.PLAIN, 18));
        textField.setBounds(10, 10, 344, 50);
        getContentPane().add(textField);
        textField.setColumns(10);

        JButton btnNewButton = new JButton("C");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                    s="";
                    textField.setText(s);//清空文本框
                    for(c=0;c<100;c++)
                    {
                     num[c]=0;//清空运算数据
                     b[c]=0;//清空运算符
                    }
                    i=0;
                    c=0;
                    jieguo=0;
            }
        });
        btnNewButton.setBounds(20, 99, 65, 40);
        getContentPane().add(btnNewButton);

        JButton button = new JButton("/");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                s=s+"/";
                textField.setText(s);//在文本框中显示相应运算符
                b[i]='/';
                i++;  //输入运算符号后给数组地址+1存放后面的输入
            }
        });
        button.setBounds(107, 99, 65, 40);
        getContentPane().add(button);

        JButton button_1 = new JButton("*");
        button_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                s=s+"*";
                textField.setText(s);//在文本框中显示相应运算符
                b[i]='*';
                i++;
            }
        });
        button_1.setBounds(189, 99, 65, 40);
        getContentPane().add(button_1);

        JButton button_2 = new JButton("-");
        button_2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                s=s+"-";
                textField.setText(s);//在文本框中显示相应运算符
                b[i]='-';
                i++;
            }
        });
        button_2.setBounds(276, 99, 65, 40);
        getContentPane().add(button_2);

        JButton button_3 = new JButton("1");
        button_3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                s += "1";
                textField.setText(s);//在文本框中显示相应数字
                num[i] = num[i] * 10 + 1;
            }
        });
        button_3.setBounds(20, 170, 65, 40);
        getContentPane().add(button_3);

        JButton button_4 = new JButton("2");
        button_4.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                s += "2";
                textField.setText(s);//在文本框中显示相应数字
                num[i] = num[i] * 10 + 2;
            }
        });
        button_4.setBounds(107, 170, 65, 40);
        getContentPane().add(button_4);

        JButton button_5 = new JButton("3");
        button_5.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                s += "3";
                textField.setText(s);//在文本框中显示相应数字
                num[i] = num[i] * 10 + 3;
            }
        });
        button_5.setBounds(189, 170, 65, 40);
        getContentPane().add(button_5);

        JButton button_6 = new JButton("+");
        button_6.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                s=s+"+";
                textField.setText(s);//在文本框中显示相应运算符
                b[i]='+';
                i++;
            }
        });
        button_6.setBounds(276, 170, 65, 116);
        getContentPane().add(button_6);

        JButton button_7 = new JButton("4");
        button_7.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                s += "4";
                textField.setText(s);//在文本框中显示相应数字
                num[i] = num[i] * 10 + 4;
            }
        });
        button_7.setBounds(20, 246, 65, 40);
        getContentPane().add(button_7);

        JButton button_8 = new JButton("5");
        button_8.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                s += "5";
                textField.setText(s);//在文本框中显示相应数字
                num[i] = num[i] * 10 + 5;
            }
        });
        button_8.setBounds(107, 246, 65, 40);
        getContentPane().add(button_8);

        JButton button_9 = new JButton("6");
        button_9.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                s += "6";
                textField.setText(s);//在文本框中显示相应数字
                num[i] = num[i] * 10 + 6;
            }
        });
        button_9.setBounds(189, 246, 65, 40);
        getContentPane().add(button_9);

        JButton button_11 = new JButton("7");
        button_11.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                s += "7";
                textField.setText(s);//在文本框中显示相应数字
                num[i] = num[i] * 10 + 7;
            }
        });
        button_11.setBounds(20, 315, 65, 40);
        getContentPane().add(button_11);

        JButton button_12 = new JButton("8");
        button_12.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                s += "8";
                textField.setText(s);//在文本框中显示相应数字
                num[i] = num[i] * 10 + 8;
            }
        });
        button_12.setBounds(107, 315, 65, 40);
        getContentPane().add(button_12);

        JButton button_13 = new JButton("9");
        button_13.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                s += "9";
                textField.setText(s);//在文本框中显示相应数字
                num[i] = num[i] * 10 + 9;
            }
        });
        button_13.setBounds(189, 315, 65, 40);
        getContentPane().add(button_13);

        JButton button_14 = new JButton("=");
        button_14.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                switch(b[0])//为了方便用循环来计算,我想到的是先把第一次计算提出来
                 {
                 case '+':
                 {
                  jieguo=num[0]+num[1];
                  break;
                 }
                 case '-':
                 {
                  jieguo=num[0]-num[1];
                  break;
                 }
                 case '*':
                 {
                  jieguo=num[0]*num[1];
                  break;
                 }
                 case '/':
                 {
                  jieguo=num[0]/num[1];
                  break;
                 }
                 }
                 c++;

                 // 一个算式多次运算

                 for(c=1;c<i;c++)
                {
                 switch(b[c])
                 {
                 case '+':
                 {
                  jieguo=jieguo+num[c+1];
                  break;
                 }
                 case '-':
                 {
                  jieguo=jieguo-num[c+1];
                  break;
                 }
                 case '*':
                 {
                  jieguo=jieguo*num[c+1];
                  break;
                 }
                 case '/':
                 {
                  jieguo=jieguo/num[c+1];
                  break;
                 }
                 }
                }

                textField.setText(String.valueOf(jieguo));//.setText()的参数需要String,所以用String.valueOf()将long转换为String
            }
        });
        button_14.setBounds(276, 315, 65, 104);
        getContentPane().add(button_14);

        JButton button_15 = new JButton("0");
        button_15.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                s += "0";
                textField.setText(s);//在文本框中显示相应数字
                num[i] = num[i] * 10 + 0;
            }
        });
        button_15.setBounds(20, 379, 152, 40);
        getContentPane().add(button_15);

        JButton button_17 = new JButton(".");
        button_17.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                        //还没想出来怎么整,有知道的大佬还请多指教``
            }
        });
        button_17.setBounds(189, 379, 65, 40);
        getContentPane().add(button_17);

        JLabel label = new JLabel("注意本计算机没有计算优先级(一律从左到右计算)");
        label.setBounds(22, 430, 321, 15);
        getContentPane().add(label);

    }
}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章