本文是对上一个简单的计算器的优化和改进。
效果如图
代码如下:
import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.event.ActionEvent;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Robot;
import java.awt.SystemColor;
import javax.swing.SwingConstants;
import java.awt.Color;
import javax.swing.JLabel;
public class Calculator extends JFrame {
private JPanel contentPane;
private JTextField result;
private int operator = 0;
String num1 = "", num2 = "";
private double number1, number2;
private boolean flag = false;
double value;
private JTextField showNumber;
double m1, m2, m3;
int pointNumber = 0;
private JTextField time;
private JTextField texttime;
public void count() {
if (operator != 0) {
number1 = Double.parseDouble(num1);
number2 = Double.parseDouble(result.getText());
if (operator == 1) {
BigDecimal p1 = new BigDecimal(num1);
BigDecimal p2 = new BigDecimal(result.getText());
value = p1.add(p2).doubleValue();
} else if (operator == 2) {
BigDecimal p1 = new BigDecimal(num1);
BigDecimal p2 = new BigDecimal(result.getText());
value = p1.subtract(p2).doubleValue();
} else if (operator == 3) {
BigDecimal p1 = new BigDecimal(num1);
BigDecimal p2 = new BigDecimal(result.getText());
value = p1.multiply(p2).doubleValue();
} else if (operator == 4) {
BigDecimal p1 = new BigDecimal(num1);
BigDecimal p2 = new BigDecimal(result.getText());
value = p1.divide(p2).doubleValue();
}
if ((int) value == value) {
int exp1 = (int) value;
result.setText(exp1 + "");
} else {
String exp0 = String.valueOf(value);
result.setText(exp0);
}
}
}
public void number(ActionEvent e)
{
if (showNumber.getText().equals("0")) {
showNumber.setText(e.getActionCommand());
} else
showNumber.setText(showNumber.getText() + e.getActionCommand());
if (flag) {
result.setText(e.getActionCommand());
flag = false;
} else {
if (result.getText().equals("0"))
result.setText(e.getActionCommand());
else
result.setText(result.getText() + e.getActionCommand());
}
}
/**
* 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, 318, 435);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setBackground(SystemColor.menu);
contentPane.setLayout(null);
result = new JTextField();
result.setFont(new Font("宋体", Font.BOLD, 20));
result.setText("0");
result.setHorizontalAlignment(SwingConstants.RIGHT);
result.setBounds(10, 99, 284, 34);
contentPane.add(result);
result.setColumns(10);
showNumber = new JTextField();
showNumber.setHorizontalAlignment(SwingConstants.RIGHT);
showNumber.setFont(new Font("宋体", Font.BOLD, 20));
showNumber.setText("0");
showNumber.setBounds(10, 67, 284, 34);
contentPane.add(showNumber);
showNumber.setColumns(10);
//数字部分
JButton btn0 = new JButton("0");
btn0.setBackground(Color.WHITE);
btn0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (showNumber.getText().equals("0")) {
} else
showNumber.setText(showNumber.getText() + "0");
if (flag) {
result.setText("0");
flag = false;
} else {
if (result.getText().equals("0")) {
result.setText("0");
} else
result.setText(result.getText() + 0);
}
}
});
btn0.setFont(new Font("黑体", Font.BOLD, 15));
btn0.setBounds(39, 266, 53, 43);
contentPane.add(btn0);
JButton btn1 = new JButton("1");
btn1.setBackground(Color.WHITE);
btn1.setFont(new Font("黑体", Font.BOLD, 15));
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
number(e);
}
});
btn1.setBounds(39, 143, 53, 43);
contentPane.add(btn1);
JButton btn2 = new JButton("2");
btn2.setBackground(Color.WHITE);
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
number(e);
}
});
btn2.setFont(new Font("黑体", Font.BOLD, 15));
btn2.setBounds(90, 143, 53, 43);
contentPane.add(btn2);
JButton btn3 = new JButton("3");
btn3.setBackground(Color.WHITE);
btn3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
number(e);
}
});
btn3.setFont(new Font("黑体", Font.BOLD, 15));
btn3.setBounds(141, 143, 53, 43);
contentPane.add(btn3);
JButton btn4 = new JButton("4");
btn4.setBackground(Color.WHITE);
btn4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
number(e);
}
});
btn4.setFont(new Font("黑体", Font.BOLD, 15));
btn4.setBounds(39, 184, 53, 43);
contentPane.add(btn4);
JButton btn5 = new JButton("5");
btn5.setBackground(Color.WHITE);
btn5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
number(e);
}
});
btn5.setFont(new Font("黑体", Font.BOLD, 15));
btn5.setBounds(90, 184, 53, 43);
contentPane.add(btn5);
JButton btn6 = new JButton("6");
btn6.setBackground(Color.WHITE);
btn6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
number(e);
}
});
btn6.setFont(new Font("黑体", Font.BOLD, 15));
btn6.setBounds(141, 184, 53, 43);
contentPane.add(btn6);
JButton btn7 = new JButton("7");
btn7.setBackground(new Color(255, 255, 255));
btn7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
number(e);
}
});
btn7.setFont(new Font("黑体", Font.BOLD, 15));
btn7.setBounds(39, 225, 53, 43);
contentPane.add(btn7);
JButton btn8 = new JButton("8");
btn8.setBackground(Color.WHITE);
btn8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
number(e);
}
});
btn8.setFont(new Font("黑体", Font.BOLD, 15));
btn8.setBounds(90, 225, 53, 43);
contentPane.add(btn8);
JButton btn9 = new JButton("9");
btn9.setBackground(Color.WHITE);
btn9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
number(e);
}
});
btn9.setFont(new Font("黑体", Font.BOLD, 15));
btn9.setBounds(141, 225, 53, 43);
contentPane.add(btn9);
// 运算符
JButton btnAdd = new JButton("+");
btnAdd.setBackground(Color.WHITE);
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
count();
if (showNumber.getText().equals("0")) {
showNumber.setText("0");
} else
showNumber.setText(showNumber.getText() + "+");
num1 = result.getText();
operator = 1;
flag = true;
pointNumber = 0;
}
});
btnAdd.setFont(new Font("黑体", Font.BOLD, 15));
btnAdd.setBounds(192, 143, 53, 43);
contentPane.add(btnAdd);
JButton btnsubtract = new JButton("-");
btnsubtract.setBackground(Color.WHITE);
btnsubtract.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
count();
if (showNumber.getText().equals("0")) {
showNumber.setText("-");
result.setText("-");
} else
showNumber.setText(showNumber.getText() + "-");
num1 = result.getText();
operator = 2;
flag = true;
pointNumber = 0;
}
});
btnsubtract.setFont(new Font("黑体", Font.BOLD, 15));
btnsubtract.setBounds(192, 184, 53, 43);
contentPane.add(btnsubtract);
JButton btnmultiply = new JButton("*");
btnmultiply.setBackground(Color.WHITE);
btnmultiply.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
count();
if (showNumber.getText().equals("0")) {
showNumber.setText("0");
} else
showNumber.setText(showNumber.getText() + "*");
num1 = result.getText();
operator = 3;
flag = true;
pointNumber = 0;
}
});
btnmultiply.setFont(new Font("黑体", Font.BOLD, 15));
btnmultiply.setBounds(192, 225, 53, 43);
contentPane.add(btnmultiply);
JButton btndivide = new JButton("/");
btndivide.setBackground(Color.WHITE);
btndivide.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
count();
if (showNumber.getText().equals("0")) {
showNumber.setText("0");
} else
showNumber.setText(showNumber.getText() + "/");
num1 = result.getText();
operator = 4;
flag = true;
pointNumber = 0;
}
});
btndivide.setFont(new Font("黑体", Font.BOLD, 15));
btndivide.setBounds(192, 266, 53, 43);
contentPane.add(btndivide);
// 符号部分
JButton btnpoint = new JButton(".");
btnpoint.setBackground(Color.WHITE);
btnpoint.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (showNumber.getText().equals("0")) {
showNumber.setText("0.");
} else
showNumber.setText(showNumber.getText() + ".");
if (flag) {
result.setText(".");
pointNumber++;
flag = false;
} else {
if (result.getText().equals("0")) {
result.setText("0.");
pointNumber++;
} else {
result.setText(result.getText() + ".");
pointNumber++;
}
}
flag = false;
if (pointNumber > 1) {
new JOptionPane();
JOptionPane.showMessageDialog(null, "一个数不能有多个小数点");
}
}
});
btnpoint.setFont(new Font("黑体", Font.BOLD, 15));
btnpoint.setBounds(141, 266, 53, 43);
contentPane.add(btnpoint);
JButton btnC = new JButton("C");
btnC.setBackground(Color.WHITE);
btnC.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
showNumber.setText("0");
result.setText("0");
number1 = 0;
number2 = 0;
value = 0;
operator = 0;
pointNumber = 0;
}
});
btnC.setFont(new Font("黑体", Font.BOLD, 15));
btnC.setBounds(90, 266, 53, 43);
contentPane.add(btnC);
JButton btnequal = new JButton("=");
btnequal.setBackground(SystemColor.activeCaption);
btnequal.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
showNumber.setText(showNumber.getText() + "=");
// num1 operator num2 =
number1 = Double.parseDouble(num1);
number2 = Double.parseDouble(result.getText());
if (operator == 1) {
BigDecimal p1 = new BigDecimal(num1);
BigDecimal p2 = new BigDecimal(result.getText());
value = p1.add(p2).doubleValue();
} else if (operator == 2) {
BigDecimal p1 = new BigDecimal(num1);
BigDecimal p2 = new BigDecimal(result.getText());
value = p1.subtract(p2).doubleValue();
} else if (operator == 3) {
BigDecimal p1 = new BigDecimal(num1);
BigDecimal p2 = new BigDecimal(result.getText());
value = p1.multiply(p2).doubleValue();
} else if (operator == 4) {
value = number1 / number2;
if (number2 == 0) {
new JOptionPane();
JOptionPane.showMessageDialog(null, "除数不能为0!");
}
}
if ((int) value == value) {
int exp1 = (int) value;
result.setText(exp1 + "");
} else {
String exp0 = String.valueOf(value);
result.setText(exp0);
}
operator = 0;
}
});
btnequal.setFont(new Font("黑体", Font.PLAIN, 37));
btnequal.setBounds(39, 307, 207, 43);
contentPane.add(btnequal);
JLabel lblNewLabel = new JLabel("\u7528\u6765\u8BA1\u7B97\u591A\u9879\u5F0F\u8FD0\u7B97");
lblNewLabel.setHorizontalAlignment(SwingConstants.RIGHT);
lblNewLabel.setForeground(Color.LIGHT_GRAY);
lblNewLabel.setBounds(164, 10, 130, 24);
contentPane.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("\u4E0D\u8003\u8651\u4F18\u5148\u7EA7");
lblNewLabel_1.setHorizontalAlignment(SwingConstants.RIGHT);
lblNewLabel_1.setForeground(Color.LIGHT_GRAY);
lblNewLabel_1.setBounds(152, 29, 142, 24);
contentPane.add(lblNewLabel_1);
texttime = new JTextField();
texttime.setHorizontalAlignment(SwingConstants.RIGHT);
texttime.setBounds(10, 12, 153, 24);
contentPane.add(texttime);
texttime.setColumns(10);
Timer timer=new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
SimpleDateFormat time = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
texttime.setText(time.format(new Date()));
}
}, 0, 1000);
}
}
相比之前的简单计算器这个可以进行一些简单的多项式运算,并且用count()和number()方法相比之前大大减少了重复代码量。最后为了丰富页面添加了一个显示时间的文本框。
这个计算器仍存在不好的地方后面有时间会继续改进。