package com.itcase_eight;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.util.Date;
/**
* @author 罗志刚
* @date 2020/12/16 22:09
*/
public class Calculater {
public static void createAndShowGUI() {
Date date=new Date();
DateFormat format=DateFormat.getDateInstance(DateFormat.SHORT);
// 对计算器整体框架的建立start
JFrame f = new JFrame("计算器");// 窗口
JPanel centerPanel = new JPanel(); // 中间面板
JPanel startPanel=new JPanel();
// 初始化功能键
JButton left=new JButton("(");
JLabel data=new JLabel(format.format(date),JLabel.CENTER);
data.setFont(new Font("Times New Roman",Font.BOLD,17));
JButton clear=new JButton("Clear");
JButton right=new JButton(")");
String button[] = { "7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", ".", "0", "=", "+"};
JButton but0 = new JButton(button[0]);
JButton but1 = new JButton(button[1]);
JButton but2 = new JButton(button[2]);
JButton but3 = new JButton(button[3]);
JButton but4 = new JButton(button[4]);
JButton but5 = new JButton(button[5]);
JButton but6 = new JButton(button[6]);
JButton but7 = new JButton(button[7]);
JButton but8 = new JButton(button[8]);
JButton but9 = new JButton(button[9]);
JButton but10 = new JButton(button[10]);
JButton but11 = new JButton(button[11]);
JButton but12 = new JButton(button[12]);
JButton but13 = new JButton(button[13]);
JButton but14 = new JButton(button[14]);
JButton but15 = new JButton(button[15]);
// 单行输入文本框
JTextField txt = new JTextField();
// 使用网格布局方式
centerPanel.setLayout(new GridLayout(5, 4, 12, 16)); // 左右上下间隔
centerPanel.add(left);
centerPanel.add(clear);
centerPanel.add(right);
centerPanel.add(data);
centerPanel.add(but0);
centerPanel.add(but1);
centerPanel.add(but2);
centerPanel.add(but3);
centerPanel.add(but4);
centerPanel.add(but5);
centerPanel.add(but6);
centerPanel.add(but7);
centerPanel.add(but8);
centerPanel.add(but9);
centerPanel.add(but10);
centerPanel.add(but11);
centerPanel.add(but12);
centerPanel.add(but13);
centerPanel.add(but14);
centerPanel.add(but15);
centerPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
// 设置容器大小
txt.setPreferredSize(new Dimension(465, 40));
// 设置字体,风格和字号
txt.setFont(new Font("宋体", Font.PLAIN, 28));
f.add(startPanel);
f.add(txt, BorderLayout.NORTH); // 将单行文本框添加到窗口的 北部
f.add(centerPanel, BorderLayout.SOUTH); // 将中间面板添加到窗口的南部
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 点X关闭窗口
f.setLocation(400, 200); // 初始化时定位
f.setSize(500, 300);
// 展示JFrame窗口
f.setVisible(true);
f.setResizable(false); // 禁止拖曳改变窗口大小
f.pack(); // 让窗口的大小自适应
// 对计算器整体框架的建立end
// 为按钮事件添加自定义监听器start
StringBuffer r=new StringBuffer();
but0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
r.append("7");
txt.setText(r.toString());
}
});
but1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
r.append("8");
txt.setText(r.toString());
}
});
but2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
r.append("9");
txt.setText(r.toString());
}
});
but4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
r.append("4");
txt.setText(r.toString());
}
});
but5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
r.append("5");
txt.setText(r.toString());
}
});
but6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
r.append("6");
txt.setText(r.toString());
}
});
left.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
r.append("(");
txt.setText(r.toString());
}
});
right.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
r.append(")");
txt.setText(r.toString());
}
});
but8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
r.append("1");
txt.setText(r.toString());
}
});
but9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
r.append("2");
txt.setText(r.toString());
}
});
but10.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
r.append("3");
txt.setText(r.toString());
}
});
but13.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
r.append("0");
txt.setText(r.toString());
}
});
but15.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
r.append("+");
txt.setText(r.toString());
}
});
but3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
r.append("/");
txt.setText(r.toString());
}
});
but7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
r.append("*");
txt.setText(r.toString());
}
});
but12.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
r.append(".");
txt.setText(r.toString());
}
});
but11.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
r.append("-");
txt.setText(r.toString());
}
});
clear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
r.delete(0,r.length()); //清空字符串中的内容
txt.setText(r.toString()); //将结果显示在文本框中
}
});
but14.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
r.append("=");
String str=r.toString();
txt.setText("");
double result= Computer.solution(str);
String string=String.valueOf(result);
r.delete(0,r.length());
r.append(string);
txt.setText(string);
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(Calculater::createAndShowGUI);
}
}
|