import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Objects;
public class Computer extends JFrame {
JPanel jPanelText;
JTextField textField;
JPanel jPanelButton;
JButton jButton7;
JButton jButton8;
JButton jButton9;
JButton jButtonchu;
JButton jButton4;
JButton jButton5;
JButton jButton6;
JButton jButtoncheng;
JButton jButton1;
JButton jButton2;
JButton jButton3;
JButton jButtonjian;
JButton jButtondian;
JButton jButton0;
JButton jButtondeng;
JButton jButtonjia;
JButton jButtonCE;
int num1;
int num2;
String operator;
//界面
public Computer(){
jPanelText = new JPanel();
jButtonCE = new JButton("CE");
textField = new JTextField(20);
textField.setText("");
textField.setEditable(true);
jPanelText.add(textField);
jPanelText.add(jButtonCE);
jPanelButton = new JPanel(new GridLayout(4,4));
jButton1 = new JButton("1");
jButton1.addActionListener(buttonListener(jButton1.getText()));
jButton2 = new JButton("2");
jButton2.addActionListener(buttonListener(jButton2.getText()));
jButton3 = new JButton("3");
jButton3.addActionListener(buttonListener(jButton3.getText()));
jButton4 = new JButton("4");
jButton4.addActionListener(buttonListener(jButton4.getText()));
jButton5 = new JButton("5");
jButton5.addActionListener(buttonListener(jButton5.getText()));
jButton6 = new JButton("6");
jButton6.addActionListener(buttonListener(jButton6.getText()));
jButton7 = new JButton("7");
jButton7.addActionListener(buttonListener(jButton7.getText()));
jButton8 = new JButton("8");
jButton8.addActionListener(buttonListener(jButton8.getText()));
jButton9 = new JButton("9");
jButtonjian = new JButton("-");
jButtonjian.addActionListener(optButtonListener(jButtonjian.getText()));
jButtonjia = new JButton("+");
jButtonjia.addActionListener(optButtonListener(jButtonjia.getText()));
jButton9.addActionListener(buttonListener(jButton9.getText()));
jButtoncheng = new JButton("*");
jButtoncheng.addActionListener(optButtonListener(jButtoncheng.getText()));
jButton0 = new JButton("0");
jButton0.addActionListener(buttonListener(jButton0.getText()));
jButtonchu = new JButton("/");
jButtonchu.addActionListener(optButtonListener(jButtonchu.getText()));
jButtondian = new JButton(".");
jButtonCE.addActionListener(clearButtonListener());
jButtondeng = new JButton("=");
jButtondeng.addActionListener(optButtonListener(jButtondeng.getText()));
jPanelButton.add(jButton7);jPanelButton.add(jButton8);jPanelButton.add(jButton9);jPanelButton.add(jButtonchu);
jPanelButton.add(jButton4);jPanelButton.add(jButton5);jPanelButton.add(jButton6);jPanelButton.add(jButtoncheng);
jPanelButton.add(jButton1);jPanelButton.add(jButton2);jPanelButton.add(jButton3);jPanelButton.add(jButtonjian);
jPanelButton.add(jButtondian);jPanelButton.add(jButton0);jPanelButton.add(jButtondeng);jPanelButton.add(jButtonjia);
operator = "";
getContentPane().add("North",jPanelText);
getContentPane().add("Center",jPanelButton);
}
// 数字监听器
public ActionListener buttonListener(String text)
{
return new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
MyLogger.logger.info(operator);
MyLogger.logger.info(text);
switch (operator)
{
case "+":
case "-":
case "*":
case "/": textField.setText(text);break;
default:textField.setText(textField.getText()+text);break;
}
}
};
}
//清除键监听器
public ActionListener clearButtonListener()
{
return new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
textField.setText("");
}
};
}
//加减乘除等号监听器
public ActionListener optButtonListener(String Operation)
{
return new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
if (!Objects.equals(Operation, "="))
{
num1 = Integer.parseInt(textField.getText());
textField.setText("");
operator = Operation;
}
else
{
num2 = Integer.parseInt(textField.getText());
int result = 0;
switch (operator)
{
case "+": result = num1 + num2; break;
case "-": result = num1 - num2; break;
case "*": result = num1 * num2; break;
case "/": result = num1 / num2; break;
}
MyLogger.getLogger().info(String.valueOf(num1) + operator + num2 + " = " + result);
textField.setText(String.valueOf(result));
}
}
};
}
public static void main(String[] args) {
Computer Jisuanqi = new Computer();
Jisuanqi.setTitle("计算器");
Jisuanqi.setVisible(true);
Jisuanqi.pack();
Jisuanqi.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Jisuanqi.setSize(300,300);
Jisuanqi.setLocation(400, 300);
Jisuanqi.setResizable(false);
}
}
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.*;
/**
* 可以自已定义日志打印格式,这样看起来比较方便些
*
*/
class MyFormatter extends Formatter
{
@Override
public String format(LogRecord arg0)
{
//创建StringBuilder对象来存放后续需要打印的日志内容
StringBuilder builder = new StringBuilder();
//获取时间
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");
Date now = new Date();
String dateStr = simpleDateFormat.format(now);
builder.append("[");
builder.append(dateStr);
builder.append(" ");
//拼接日志级别
builder.append(arg0.getLevel()).append(" ");
builder.append(arg0.getSourceClassName()).append(" ");
//拼接方法名
builder.append(arg0.getSourceMethodName()).append(" ");
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
String line = stackTrace[8].toString();
String lineNumber = line.substring(line.indexOf(":") + 1, line.length() - 1);
//拼接方法名
builder.append(lineNumber).append("] ");
//拼接日志内容
builder.append(arg0.getMessage());
//日志换行
builder.append("\r\n");
return builder.toString();
}
}
public class MyLogger {
static Logger logger;
static {
logger = Logger.getLogger(MyLogger.class.getName());
logger.setUseParentHandlers(false);
//如果需要将日志文件写到文件系统中,需要创建一个FileHandler对象
Handler consoleHandler = new ConsoleHandler();
//创建日志格式文件:本次采用自定义的Formatter
consoleHandler.setFormatter(new MyFormatter());
//将FileHandler对象添加到Logger对象中
logger.addHandler(consoleHandler);
}
public static Logger getLogger() {
return logger;
}
public static void main(String[] args) {
logger.info("11");
}
}
运行效果: