效果如下:
代码主要写成三个类了:
类1:
import java.awt.Color;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class CalculatorMain {
public static void main(String[] args) {
JFrame windows = new JFrame("计算器");
JTextField text = new JTextField();
Font f = new Font("楷体", Font.PLAIN, 25);
MyMenu myMenu = new MyMenu(windows, text);
windows.setLayout(null);
windows.setBounds(0, 0, 345, 326);//278
windows.setBackground(new Color(173,216,230));
windows.setResizable(false);//不能放大
windows.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
text.setBounds(10, 10, 322, 52);//244
text.setText("0");//初始化文本内容
text.setBackground(new Color(239,243,252));
text.setFont(f);
//text.setEditable(false);//使控件不可编辑
text.setFocusable(false);//使控件无法获取焦点
// text.setEnabled(false);//使控件不可用
// UIManager.put("TextField.inactiveForeground", new Color(255, 0, 0));
text.setBorder(BorderFactory.createLineBorder(new Color(143,158,176)));//边框颜色
text.setHorizontalAlignment(SwingConstants.RIGHT);
//声明按钮
MyButton Button1 = new MyButton("←", 62, 30, 10, 75, text);//17
MyButton Button2 = new MyButton("CE", 62, 30, 75, 75, text);//68
MyButton Button3 = new MyButton("C", 62, 30, 140, 75, text);//117
MyButton Button4 = new MyButton("±", 62, 30, 205, 75, text);//167
MyButton Button5 = new MyButton("√", 62, 30, 270, 75, text);//216
MyButton Button6 = new MyButton("7", 62, 30, 10, 111, text);
MyButton Button7 = new MyButton("8", 62, 30, 75, 111, text);
MyButton Button8 = new MyButton("9", 62, 30, 140, 111, text);
MyButton Button9 = new MyButton("/", 62, 30, 205, 111, text, 5);
MyButton Button10 = new MyButton("%", 62, 30, 270, 111, text, 4);
MyButton Button11 = new MyButton("4", 62, 30, 10, 147, text);
MyButton Button12 = new MyButton("5", 62, 30, 75, 147, text);
MyButton Button13 = new MyButton("6", 62, 30, 140, 147, text);
MyButton Button14 = new MyButton("*", 62, 30, 205, 147, text, 3);
MyButton Button15 = new MyButton("1/x", 62, 30, 270, 147, text);
MyButton Button16 = new MyButton("1", 62, 30, 10, 183, text);
MyButton Button17 = new MyButton("2", 62, 30, 75, 183, text);
MyButton Button18 = new MyButton("3", 62, 30, 140, 183, text);
MyButton Button19 = new MyButton("-", 62, 30, 205, 183, text, 2);
MyButton Button20 = new MyButton("=", 62, 66, 270, 183, text);
MyButton Button21 = new MyButton("0", 127, 30, 10, 219, text);
MyButton Button22 = new MyButton(".", 62, 30, 140, 219, text);//117
MyButton Button23 = new MyButton("+", 62, 30, 205, 219, text, 1);//167
windows.add(text);
windows.add(Button1);
windows.add(Button2);
windows.add(Button3);
windows.add(Button4);
windows.add(Button5);
windows.add(Button6);
windows.add(Button7);
windows.add(Button8);
windows.add(Button9);
windows.add(Button10);
windows.add(Button11);
windows.add(Button12);
windows.add(Button13);
windows.add(Button14);
windows.add(Button15);
windows.add(Button16);
windows.add(Button17);
windows.add(Button18);
windows.add(Button19);
windows.add(Button20);
windows.add(Button21);
windows.add(Button22);
windows.add(Button23);
windows.setVisible(true);//使窗口可显
}
}
类2:
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JTextField;
public class MyButton extends JButton{
private JTextField textField;
private int s;
static int judge = 0, state;
static double a, b;
MyButton button = this;
MyButton(String name, int width, int height, int x, int y, Object... text) {
Font f = new Font("楷体", Font.PLAIN, 17);
//实现可变参数
if (text.length != 0) {
for (Object t : text) {
if (t instanceof JTextField) {
this.textField = (JTextField)t;
}
if (t instanceof Integer) {
this.s = (Integer)t;
}
}
}
this.setFont(f);
this.setText(name);
this.setLocation(x, y);
this.setSize(width, height);
this.setFocusPainted(false);//去焦点
this.setBackground(new Color(235,238,241));
this.setBorder(BorderFactory.createLineBorder(new Color(143,158,176)));
//this.setBorder(BorderFactory.createLineBorder(new Color(248,220,42)));
//按钮监听事件
this.addMouseListener(new Mouse());
this.addMouseListener(new Mouse2());
this.addMouseListener(new Mouse3());
//this.addMouseListener(new Mouse4());
this.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
Double c = 0.0;
if (getText().equals("+")
|| getText().equals("-")
|| getText().equals("*")
|| getText().equals("/")
|| getText().equals("%")) {
MyButton.state = s;
try {
MyButton.a = Double.parseDouble(textField.getText());
} catch (NumberFormatException e1) {
System.out.println("数据出错!!!");
}
MyButton.judge = 1;
}else if (getText().equals("√")) {
double temp;
temp = Double.parseDouble(textField.getText());
temp = Math.sqrt(temp);
textField.setText(String.valueOf(temp));
MyButton.judge = 1;
}else if (getText().equals("1/x")) {
double temp;
temp = Double.parseDouble(textField.getText());
temp = 1 / temp;
textField.setText(String.valueOf(temp));
MyButton.judge = 1;
}else if (getText().equals("=")) {
try {
MyButton.b = Double.parseDouble(textField.getText());
} catch (NumberFormatException e1) {
System.out.println("数据出错!!!");
}
if (MyButton.state == 1) {
c = MyButton.a + MyButton.b;
}else if (MyButton.state == 2) {
c = MyButton.a - MyButton.b;
}else if (MyButton.state == 3) {
c = MyButton.a * MyButton.b;
}else if (MyButton.state == 5) {
c = MyButton.a / MyButton.b;
}else if (MyButton.state == 4) {
c = MyButton.a % MyButton.b;
}
textField.setText(String.valueOf(c));
MyButton.judge = 1;
}else if (getText().equals("←")) {
String str = textField.getText();
char[] str3 = str.toCharArray();
String str2 = "";
int len = str.length();
if (len != 1) {
for (int i = 0; i < len-1; i++) {
str2 += String.valueOf(str3[i]);
}
textField.setText(str2);
}else {
textField.setText("0");
}
}else if (getText().equals("CE")) {
textField.setText("0");
}else if (getText().equals("C")) {
String str = textField.getText();
char[] str3 = str.toCharArray();
String str2 = "";
int len = str.length();
if (len != 1) {
for (int i = 0; i < len-1; i++) {
str2 += String.valueOf(str3[i]);
}
textField.setText(str2);
}else {
textField.setText("0");
}
}else {
textField.setText(Show());
}
}
});
}
class Mouse extends MouseAdapter {
public void mouseEntered(MouseEvent e) {
// button.setBorder(BorderFactory.createLineBorder(new Color(248,220,42)));
// button.setBackground(new Color(255,212,114));
button.setBorder(BorderFactory.createLineBorder(new Color(143,158,176)));
button.setBackground(new Color(184,207,229));
}
}
class Mouse2 extends MouseAdapter {
public void mouseExited(MouseEvent e) {
button.setBorder(BorderFactory.createLineBorder(new Color(143,158,176)));
button.setBackground(new Color(235,238,241));
}
}
class Mouse3 extends MouseAdapter {
public void mouseClicked(MouseEvent e) {
button.setBackground(new Color(184,220,229));
}
}
public String Show() {
String text = new String();
if (this.judge == 0) {
if (this.textField.getText().equals("0.0") || this.textField.getText().equals("0")) {
if (this.getText().equals("±")) {
text = "-";
}else if (this.getText().equals(".")) {
text = "0";
}else {
text += this.getText();
}
}else {
if (this.getText().equals("±")) {
text = "-";
}else {
text = this.textField.getText();
if (text.indexOf(".") != -1 && this.getText().equals(".")
|| text.equals("-") && this.getText().equals(".")) {
text += "";
}else {
text += this.getText();
}
}
}
}else {
if (this.getText().equals("±")) {
text = "-";
this.judge = 0;
}else if (this.getText().equals(".")) {
text = this.textField.getText();
}else {
text = "";
text += this.getText();
this.judge = 0;
}
}
return text;
}
}
类3:
import java.awt.Color;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextField;
public class MyMenu {
private JMenuBar mb = new JMenuBar();
Font f = new Font("楷体", Font.PLAIN, 19);
Font f1 = new Font("楷体", Font.PLAIN, 18);
private JMenu advanced = new JMenu("高级");
private JMenu edit = new JMenu("编辑");
private JMenuItem advanced1 = new JMenuItem("表达式求值");
private JMenuItem copyItem = new JMenuItem("复制");
private JMenuItem pasteItem = new JMenuItem("粘贴");
private JMenu help = new JMenu("帮助");
private JMenuItem InformationItem = new JMenuItem("关于计算器");
MyMenu(JFrame windows, JTextField text) {
advanced1.setFont(f1);
copyItem.setFont(f1);
pasteItem.setFont(f1);
InformationItem.setFont(f1);
advanced.add(advanced1);
edit.add(copyItem);
edit.add(pasteItem);
edit.setFont(f);
advanced.setFont(f);
help.setFont(f);
help.add(InformationItem);
mb.add(advanced);
mb.add(edit);
mb.add(help);
mb.setBackground(new Color(238,238,238));
advanced1.setEnabled(false);
copyItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
StringSelection st = new StringSelection(text.getText());
clipboard.setContents(st, null);
}
});
pasteItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
if (clipboard.isDataFlavorAvailable(DataFlavor.stringFlavor)) {
try {
String content = (String)clipboard.getData(DataFlavor.stringFlavor);
text.setText(content);
} catch (UnsupportedFlavorException | IOException e1) {
System.out.println("剪切板为空!!!");
}
}
}
});
//信息提示框
JTextField text2 = new JTextField();
text2.setFocusable(false);
text2.setBackground(new Color(235,238,241));
text2.setFont(f);
text2.setText("可能有Bug,见谅!!!复制粘贴功能可用,高级中表达式求值功能不可用。");
JDialog d1 = new JDialog(windows, "提示", false);
d1.setBounds(20, 30, 300, 300);
d1.setResizable(false);
d1.add(text2);
d1.pack();
InformationItem.addActionListener(e -> d1.setVisible(true));
// copyItem.setFont(f1);
// pasteItem.setFont(f1);
// InformationItem.setFont(f1);
windows.setJMenuBar(mb);
}
class Mouse extends MouseAdapter {
public void mouseEntered(MouseEvent e) {
}
}
class Mouse2 extends MouseAdapter {
public void mouseExited(MouseEvent e) {
}
}
}
源码下载地址:http://download.csdn.net/my/uploads