s
o
u
l
s
j
i
e

案例2:JAVA GUI 简易计算器

使用javaGUI实现计算器的基本功能,包含一个帮助说明页面,提示用户如何使用。包含一个计算器主界面,要实现基本的加法、减法、乘法、除法运算。

1.帮助界面

 

 

 Help.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.*;
import javax.swing.*;
 
//计算器的实现
public class Help extends JFrame {
    JButton btnOK = new JButton("我知道了,开始使用");
     
    public Help() {
        super("使用说明");
        JLabel lab1 = new JLabel("1.输入第一个操作数");
        JLabel lab2= new JLabel("2.选择一个运算符号");
        JLabel lab3 = new JLabel("3.输入第二个操作数");
        JLabel lab4 = new JLabel("4.点击【计算】按钮进行计算");
        JLabel lab5 = new JLabel("5.点击【计算】按钮进行计算");
        JLabel lab6 = new JLabel("6.点击【计算】按钮进行计算");
 
        lab1.setBounds(10, 10, 300, 25);
        lab2.setBounds(10, 35, 300, 25);
        lab3.setBounds(10, 60, 300, 25);
        lab4.setBounds(10, 85, 300, 25);
        lab5.setBounds(10, 110, 300, 25);
        lab6.setBounds(10, 135, 300, 25);
         
        btnOK.setBounds(30, 160, 200, 25);
        btnOK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ee) {
                Calculator c = new Calculator();
                c.CenterPanel();
            }
        });
        JPanel p = new JPanel();
        p.setLayout(null);
        p.add(lab1);
        p.add(lab2);
        p.add(lab3);
        p.add(lab4);
        p.add(lab5);
        p.add(lab6);
        p.add(btnOK);
 
        getContentPane().add(p);
        setSize(380, 250);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
 
    public static void main(String[] args) {
        Help s = new Help();
        s.CenterPanel();
    }
 
    public void CenterPanel() {
        int width = Toolkit.getDefaultToolkit().getScreenSize().width;
        int height = Toolkit.getDefaultToolkit().getScreenSize().height;
        this.setLocation(width / 2, height / 4);
    }
 
    //判断字符串是不是数字
    public static boolean isNumeric(String str){
        for (int i = str.length();--i>=0;){ 
            if (!Character.isDigit(str.charAt(i))){
                return false;
            }
        }
    return true;
    }
}

 

2.计算界面

 

 Calculator.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.*;
import javax.swing.*;
 
//计算器的实现
public class Calculator extends JFrame {
    JTextField txtNum1 = new JTextField();
    JTextField txtNum2 = new JTextField();
    JTextField txtResult = new JTextField();
    JButton btnCalcul = new JButton("计算");
    JButton btnClear = new JButton("清屏");
    JButton btnExit = new JButton("退出");
     
    public Calculator() {
        super("计算器");
        JLabel lab1 = new JLabel("请输入操作数1:");
        JLabel lab2= new JLabel("请选择运算符:");
        JLabel lab3 = new JLabel("请输入操作数2:");
         
        final JRadioButton radioButton1=new JRadioButton("加");
        final JRadioButton radioButton2=new JRadioButton("减");
        final JRadioButton radioButton3=new JRadioButton("乘");
        final JRadioButton radioButton4=new JRadioButton("除");
        ButtonGroup bg=new ButtonGroup();
        bg.add(radioButton1);
        bg.add(radioButton2);
        bg.add(radioButton3);
        bg.add(radioButton4);
        JPanel jp1=new JPanel(new GridLayout(1,4));
        jp1.add(radioButton1);
        jp1.add(radioButton2);
        jp1.add(radioButton3);
        jp1.add(radioButton4);
 
 
        JLabel lab4= new JLabel("计算结果:");
        lab1.setBounds(10, 10, 100, 25);
        lab2.setBounds(10, 40, 100, 25);
        lab3.setBounds(10, 70, 100, 25);
        lab4.setBounds(10, 100, 100, 25);
 
        txtNum1.setBounds(110, 10, 200, 25);
        jp1.setBounds(110, 40, 200, 25);
        txtNum2.setBounds(110, 70, 200, 25);
        txtResult.setBounds(110, 100, 200, 25);
 
        btnCalcul.setBounds(30, 130, 90, 25);
        btnClear.setBounds(130, 130, 90, 25);
        btnExit.setBounds(230, 130, 90, 25);
        //计算
        btnCalcul.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ee) {
                String num1=txtNum1.getText().trim();
                String num2=txtNum2.getText().trim();
                String Result="";
                if (!isNumeric(num1)){
                JOptionPane.showMessageDialog(null, "操作数1只允许为数字");
                return;
                }
                if (!isNumeric(num2))
                {
                JOptionPane.showMessageDialog(null, "操作数2只允许为数字");
                return;
                }
                if(radioButton1.isSelected()){
                Result=num1+" + "+num2+" = "+(Integer.parseInt(num1)+Integer.parseInt(num2));
                }
                if(radioButton2.isSelected()){
                    Result=num1+" - "+num2+" = "+(Integer.parseInt(num1)-Integer.parseInt(num2));
                }
                if(radioButton3.isSelected()){
                    Result=num1+" * "+num2+" = "+(Integer.parseInt(num1)*Integer.parseInt(num2));
                }
                if(radioButton4.isSelected()){
                    Result=num1+" / "+num2+" = "+(Integer.parseInt(num1)/Integer.parseInt(num2));
                }
                txtResult.setText(Result);
            }
        });
        //清屏
        btnClear.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ee) {
                txtNum1.setText("");
                txtNum2.setText("");
                txtResult.setText("");
            }
        });
        //退出程序
        btnExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ee) {
                System.exit(0);
            }
        });
        JPanel p = new JPanel();
        p.setLayout(null);
        p.add(lab1);
        p.add(lab2);
        p.add(lab3);
        p.add(lab4);
 
        p.add(txtNum1);
        p.add(txtNum2);
        p.add(jp1);
        p.add(txtResult);
 
        p.add(btnCalcul);
        p.add(btnClear);
        p.add(btnExit);
 
        getContentPane().add(p);
        setSize(380, 250);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
 
    public static void main(String[] args) {
        Calculator s = new Calculator();
        s.CenterPanel();
    }
 
    public void CenterPanel() {
        int width = Toolkit.getDefaultToolkit().getScreenSize().width;
        int height = Toolkit.getDefaultToolkit().getScreenSize().height;
        this.setLocation(width / 2, height / 4);
    }
 
    //判断字符串是不是数字
    public static boolean isNumeric(String str){
        for (int i = str.length();--i>=0;){ 
            if (!Character.isDigit(str.charAt(i))){
                return false;
            }
        }
    return true;
    }
}

  

 

posted @   soulsjie  阅读(514)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下
历史上的今天:
2017-11-04 jQuery学习之------html()、text()和val()
2017-11-04 jQuery学习之------对标签属性的操作
2017-11-04 jQuery学习之------选择器
你累吗?累就对了,当你觉得累时证明你在走上坡路!-----NotFoundObject - 2016-12-14 08:43
点击右上角即可分享
微信分享提示