JAVA Swing(GUI)中级计算器Jframe
准备操作不会的可以看上一篇:简易计算器的制作
里面包含WindowBuilder的安装JFrame的创建以及基本的创建步骤
功能
具有记忆功能可以进行多个数值的计算,每进行两个数的计算就会保存在text2中,直到最后的计算结束。可回退数据,清空数据,关闭窗口。
当一个式子计算完毕后,可以不点清空直接进行下一个式子进行计算;
当除数为0或者一个数中出现两个小数点时,会出现警告窗口对用户进行提示;
界面展示
在看代码时会发现本应类似的代码块出现了不同的情况,或者出现功能重复的情况,但其实最终目的是一样的。这是因为我在思考时想到了一些不同的方法,所以在部分类似模块用了不同而方法进行编写,大家在编写过程中可自行选择。
特别提醒:该代码长度有些长,有些代码会有冗余,但其实经过优化可以减少大概一两百行,本文中就不进行优化了,大家以了解编写的思想方法为主。
主要思想
每进行一次运算,都要将运算结果保存下来进行下一次的计算。
重点:由于是一个数据一个运算符一个数据进行出现,所以需要等两个数据出现之后才能对其进行计算,这里以第二个运算符出现为标志,取前一个运算符对两数进行计算,所以需要用一个变量将前一个运算符进行保存。
并且每次遇到运算符时需要对text1进行清空,以便下一个值的输入。chose=false表示text1与text2不需要清空,point=false表示运算中没有小数点;当式子完成运算之后,chose会变成true,是为了在不点清空的情况下,能够直接输入下一个数字,并自动清空然后进行下一个式子的运算;
更详细的可以看代码注释进行理解;有许多相似的代码注释了其中一块,就不一一进行注释了;
代码如下
package calculator;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class Calculator extends JFrame {
private JPanel contentPane;
private JTextField text1;
private JTextField text2;
private boolean chose=false,point;//chose=false表示text1与text2不需要清空,point=false表示运算中没有小数点;
private String fuhao,fuhao1;
private double count,count2,count3;//count3用来装text2中的值,从而保存之前运算的结果,进行记忆运算
//count2用来装最后一个运算数
//count用来装除最后一次输入的数
private boolean f=true;//用来判断是否是第一个运算符
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Calculator frame = new Calculator();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Calculator() {
setTitle("\u8BA1\u7B97\u5668");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 500, 490);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
text1 = new JTextField();
text1.setFont(new Font("华文楷体", Font.BOLD, 20));
text1.setBounds(31, 36, 200, 50);
contentPane.add(text1);
text1.setColumns(10);
text2 = new JTextField();
text2.setFont(new Font("华文楷体", Font.BOLD, 20));
text2.setBounds(31, 124, 200, 50);
contentPane.add(text2);
text2.setColumns(10);
JButton Buttondelete = new JButton("\u6E05\u7A7A");
Buttondelete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {//清空
text1.setText("");
text2.setText("");
f=true;//将f重新初始化为true,使系统能重新识别第一个运算符号,以进行正确的计算;
}
});
Buttondelete.setFont(new Font("华文新魏", Font.PLAIN, 30));
Buttondelete.setBounds(360, 36, 100, 50);
contentPane.add(Buttondelete);
JButton Buttonexit = new JButton("\u5173\u95ED");
Buttonexit.setFont(new Font("华文新魏", Font.PLAIN, 30));
Buttonexit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);//退出计算器窗口
}
});
Buttonexit.setBounds(360, 124, 100, 50);
contentPane.add(Buttonexit);
JButton Button_1 = new JButton("1");
Button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(chose==true) {//当chose为true时对text1和text2进行清空;
text1.setText("");
text2.setText("");
}
text1.setText(text1.getText()+""+e.getActionCommand());
chose=false;
}
});
Button_1.setFont(new Font("华文楷体", Font.BOLD, 30));
Button_1.setBounds(30, 200, 100, 50);
contentPane.add(Button_1);
JButton Button_2 = new JButton("2");
Button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(chose==true) {
text1.setText("");
text2.setText("");
}
text1.setText(text1.getText()+""+e.getActionCommand());
chose=false;
}
});
Button_2.setFont(new Font("华文楷体", Font.BOLD, 30));
Button_2.setBounds(140, 200, 100, 50);
contentPane.add(Button_2);
JButton Button_3 = new JButton("3");
Button_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(chose==true) {
text1.setText("");
text2.setText("");
}
text1.setText(text1.getText()+""+e.getActionCommand());
chose=false;
}
});
Button_3.setFont(new Font("华文楷体", Font.BOLD, 30));
Button_3.setBounds(250, 200, 100, 50);
contentPane.add(Button_3);
JButton Button_add = new JButton("+");
Button_add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
count=Double.parseDouble(text1.getText());
text1.setText("");
fuhao=e.getActionCommand();
if(f==true)//判断是否是第一个运算符号
{
fuhao1=fuhao;//其实也可以改成fuhao1=“+”,因为count+0=count本身;
f=false;
}
if((text2.getText()+"")=="") {
count3=0;//当text2为空时,只需要将text1中的值转移到text2中以支持后面的运算;
//当count3=0时,进行下列运算时就能够让count保持不变,直接赋值给text2;
}
else {
count3=Double.parseDouble(text2.getText());
}
switch(fuhao1) {//由于运算需要有两个数值才能计算,所以这里采用了当前运算符的前一个运算符进行计算
//若当前为第一个运算符则直接取当前运算符(其实只要让第一个text1中的值顺利的转移到text2即可)
case "+": text2.setText(count3+count+"");count3+=count;break;
case "-": text2.setText(count3-count+"");count3-=count;break;
case "*": text2.setText(count3*count+"");count3*=count;break;
case "/": if(count==0) {
JOptionPane.showMessageDialog(null,"除数不能为0","警告",JOptionPane.ERROR_MESSAGE);
return;
}
text2.setText(count3/count+"");count3/=count;
}
fuhao1=fuhao;//将取到的符号给fuhao1,当下一个数值结束输入时,利用该运算符进行计算
}
});
Button_add.setFont(new Font("华文楷体", Font.BOLD, 35));
Button_add.setBounds(360, 200, 100, 50);
contentPane.add(Button_add);
JButton Button_4 = new JButton("4");
Button_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(chose==true) {
text1.setText("");
text2.setText("");
}
text1.setText(text1.getText()+""+e.getActionCommand());
chose=false;
}
});
Button_4.setFont(new Font("华文楷体", Font.BOLD, 30));
Button_4.setBounds(30, 260, 100, 50);
contentPane.add(Button_4);
JButton Button_5 = new JButton("5");
Button_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(chose==true) {
text1.setText("");
text2.setText("");
}
text1.setText(text1.getText()+""+e.getActionCommand());
chose=false;
}
});
Button_5.setFont(new Font("华文楷体", Font.BOLD, 30));
Button_5.setBounds(140, 260, 100, 50);
contentPane.add(Button_5);
JButton Button_6 = new JButton("6");
Button_6.setFont(new Font("宋体", Font.BOLD, 30));
Button_6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(chose==true) {
text1.setText("");
text2.setText("");
}
text1.setText(text1.getText()+""+e.getActionCommand());
chose=false;
}
});
Button_6.setBounds(250, 260, 100, 50);
contentPane.add(Button_6);
JButton Button_subtract = new JButton("-");
Button_subtract.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
count=Double.parseDouble(text1.getText());
text1.setText("");
fuhao=e.getActionCommand();
if(f==true)
{
fuhao1="+";
f=false;
}
if((text2.getText()+"")=="") {
count3=0;
}
else {
count3=Double.parseDouble(text2.getText());
}
switch(fuhao1) {
case "+": text2.setText(count3+count+"");count3+=count;break;
case "-": text2.setText(count3-count+"");count3-=count;break;
case "*": text2.setText(count3*count+"");count3*=count;break;
case "/": if(count==0) {
JOptionPane.showMessageDialog(null,"除数不能为0","警告",JOptionPane.ERROR_MESSAGE);
return;
}
text2.setText(count3-count+"");count3-=count;
}
fuhao1=fuhao;
}
});
Button_subtract.setFont(new Font("华文楷体", Font.BOLD, 50));
Button_subtract.setBounds(360, 260, 100, 50);
contentPane.add(Button_subtract);
JButton Button_7 = new JButton("7");
Button_7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(chose==true) {
text1.setText("");
text2.setText("");
}
text1.setText(text1.getText()+""+e.getActionCommand());
chose=false;
}
});
Button_7.setFont(new Font("华文楷体", Font.BOLD, 30));
Button_7.setBounds(30, 320, 100, 50);
contentPane.add(Button_7);
JButton Button_8 = new JButton("8");
Button_8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(chose==true) {
text1.setText("");
text2.setText("");
}
text1.setText(text1.getText()+""+e.getActionCommand());
chose=false;
}
});
Button_8.setFont(new Font("华文楷体", Font.BOLD, 30));
Button_8.setBounds(140, 320, 100, 50);
contentPane.add(Button_8);
JButton Button_9 = new JButton("9");
Button_9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(chose==true) {
text1.setText("");
text2.setText("");
}
text1.setText(text1.getText()+""+e.getActionCommand());
chose=false;
}
});
Button_9.setFont(new Font("华文楷体", Font.BOLD, 30));
Button_9.setBounds(250, 320, 100, 50);
contentPane.add(Button_9);
JButton Button_multiply = new JButton("*");
Button_multiply.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
count=Double.parseDouble(text1.getText());
text1.setText("");
fuhao=e.getActionCommand();
if(f==true)
{
fuhao1=fuhao;
f=false;
}
if((text2.getText()+"")=="") {
count3=1;
}
else {
count3=Double.parseDouble(text2.getText());
}
switch(fuhao1) {
case "+": text2.setText(count3+count+"");count3+=count;break;
case "-": text2.setText(count3-count+"");count3-=count;break;
case "*": text2.setText(count3*count+"");count3*=count;break;
case "/": if(count==0) {
JOptionPane.showMessageDialog(null,"除数不能为0","警告",JOptionPane.ERROR_MESSAGE);
return;
}
text2.setText(count3/count+"");count3/=count;
}
fuhao1=fuhao;
}
});
Button_multiply.setFont(new Font("华文楷体", Font.BOLD, 35));
Button_multiply.setBounds(360, 320, 100, 50);
contentPane.add(Button_multiply);
JButton Button_point = new JButton(".");
Button_point.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {//防止操作者输入多个小数点造成系统崩溃;
point=true;
for(int i=0;i<text1.getText().length();i++) {
if('.'==text1.getText().charAt(i)) {
point=false;
JOptionPane.showMessageDialog(null,"一个数中只能有一个小数点","警告",JOptionPane.ERROR_MESSAGE);//弹框警告一个数中只能有一个小数点;
break;
}
}
if(point==true) {
text1.setText(text1.getText()+".");
}
}
});
Button_point.setFont(new Font("华文楷体", Font.BOLD, 40));
Button_point.setBounds(30, 380, 100, 50);
contentPane.add(Button_point);
JButton Button_0 = new JButton("0");
Button_0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(chose==true) {
text1.setText("");
text2.setText("");
}
text1.setText(text1.getText()+""+e.getActionCommand());
chose=false;
}
});
Button_0.setFont(new Font("华文楷体", Font.BOLD, 30));
Button_0.setBounds(140, 380, 100, 50);
contentPane.add(Button_0);
JButton Button_result = new JButton("=");
Button_result.setFont(new Font("华文楷体", Font.BOLD, 35));
Button_result.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
count2=Double.parseDouble(text1.getText());
text1.setText(count3+" "+fuhao+" "+count2+"=");
switch(fuhao) {
case "+": text2.setText(count3+count2+"");break;
case "-": text2.setText(count3-count2+"");break;
case "*": text2.setText(count3*count2+"");break;
case "/": if(count2==0) {
JOptionPane.showMessageDialog(null,"除数不能为0","警告",JOptionPane.ERROR_MESSAGE);
return;
}
text2.setText(count3/count2+"");
}
chose=true;//点击等于后将chose变为true能够直接进行下一次运算。因为当输入数字后text1和text2都被清空了;
}
});
Button_result.setBounds(250, 380, 100, 50);
contentPane.add(Button_result);
JButton Button_divide = new JButton("/");
Button_divide.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
count=Double.parseDouble(text1.getText());
text1.setText("");
fuhao=e.getActionCommand();
boolean n=false;
if(f==true)
{
n=true;//表示该符号是第一个运算符
fuhao1=fuhao;
f=false;
}
if((text2.getText()+"")=="") {
count3=1;
}
else {
count3=Double.parseDouble(text2.getText());
}
switch(fuhao1) {
case "+": text2.setText(count3+count+"");count3+=count;break;
case "-": text2.setText(count3-count+"");count3-=count;break;
case "*": text2.setText(count3*count+"");count3*=count;break;
case "/": if(count==0) {
JOptionPane.showMessageDialog(null,"除数不能为0","警告",JOptionPane.ERROR_MESSAGE);//会进行弹框提示;
return;
}
if(n==true)//当/为运算符时将count直接赋给count3;若不这样做会导致给text2的值出现错误(变成倒数),例如5,会变成1/5;
{
text2.setText(count+"");count3=count;
}
else {
text2.setText(count3/count+"");count3/=count;
}
}
fuhao1=fuhao;
}
});
Button_divide.setFont(new Font("华文楷体", Font.BOLD, 30));
Button_divide.setBounds(360, 380, 100, 50);
contentPane.add(Button_divide);
JLabel lblNewLabel = new JLabel("\u7ED3\u679C");
lblNewLabel.setFont(new Font("华文新魏", Font.PLAIN, 30));
lblNewLabel.setBounds(271, 124, 100, 50);
contentPane.add(lblNewLabel);
JButton btnNewButton = new JButton("\u56DE\u9000");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {//回退一格
String s = text1.getText();
text1.setText("");
for(int i=0;i<s.length()-1;i++) {
char a=s.charAt(i);
text1.setText(text1.getText()+a);//每次取出前面已经加入的内容+后面的字符,再存入(更新)
}
}
});
btnNewButton.setFont(new Font("华文新魏", Font.PLAIN, 30));
btnNewButton.setBounds(250, 36, 100, 50);
contentPane.add(btnNewButton);
}
}
结尾
希望对大家有帮助!!!
有问题可在下面留言,谢谢…