import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.*;
public class Calculator
{
// main函数入口
public static void main(String[] args)
{
// 声明 框架
CalculatorFrame frame = new CalculatorFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 设置框架可见
frame.setVisible(true);
}
}
class CalculatorFrame extends JFrame
{
// 初始化 框架
public CalculatorFrame()
{
setTitle("计算器");
// 窗口大小不可变
setResizable(false);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
themeMenu = new JMenu("观感(T)");
themeMenu.setMnemonic('T');
menuBar.add(themeMenu);
// 获得已安装的所有观感
UIManager.LookAndFeelInfo[] infos = UIManager.getInstalledLookAndFeels();
makeMenuItem("Metal", infos[0].getClassName());
makeMenuItem("Motif", infos[1].getClassName());
makeMenuItem("Windows", infos[2].getClassName());
makeMenuItem("Classic", infos[3].getClassName());
// 菜单及 快捷键
JMenu helpMenu = new JMenu("帮助(H)");
helpMenu.setMnemonic('H');
menuBar.add(helpMenu);
JMenuItem aboutItem = new JMenuItem("About", 'A');
// 添加消息处理,弹出对话框
aboutItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
if (dialog == null)
dialog = new AboutDialog(CalculatorFrame.this);
dialog.setVisible(true);
}
});
// 添加快捷键CTRL+A
aboutItem.setAccelerator(KeyStroke.getKeyStroke("ctrl A"));
helpMenu.add(aboutItem);
try{
UIManager.setLookAndFeel(infos[2].getClassName());
SwingUtilities.updateComponentTreeUI(CalculatorFrame.this);
}
catch(Exception e) {
e.printStackTrace();
}
// 计算器面板
CalculatorPanel panel = new CalculatorPanel();
Border raised = BorderFactory.createRaisedBevelBorder();
panel.setBorder(raised);
add(panel);
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
}
void makeMenuItem(String name, final String plafName){
char mnemonic = name.charAt(0);
JMenuItem menuItem = new JMenuItem(name, mnemonic);
themeMenu.add(menuItem);
menuItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event)
{
try
{
//设置更新观感
UIManager.setLookAndFeel(plafName);
SwingUtilities.updateComponentTreeUI(CalculatorFrame.this);
} catch(Exception e)
{
e.printStackTrace();
}
}
});
}
JMenu themeMenu = new JMenu();
private AboutDialog dialog;
private static final int DEFAULT_WIDTH =400;
private static final int DEFAULT_HEIGHT =300;
}
//"关于 "对话框
class AboutDialog extends JDialog
{
public AboutDialog(JFrame owner)
{
super(owner, "About DialogTest", true);
add(
new JLabel(
"<html><h1><i>Calculator</i></h1><hr>By 严希炤 谭志超 吕汉青</html>"),
BorderLayout.CENTER);
JButton ok = new JButton("Ok");
ok.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
setVisible(false);
}
});
JPanel panel = new JPanel();
panel.add(ok);
add(panel, BorderLayout.SOUTH);
setSize(250, 150);
}
}
//以下为面板按钮 及 计算器显示的实现
class CalculatorPanel extends JPanel
{
public CalculatorPanel()
{
setLayout(new BorderLayout());
// 计算结果 double
result = 0;
lastCommand = "=";
// 控制键之后的数 输入
start = true;
// 显示条的声明
display = new JTextField("0");
display.setFont(new Font("宋体",Font.BOLD,30));
display.setEnabled(false);
display.setBackground(Color.white);
// TextField内容设置为右对齐
display.setHorizontalAlignment(JTextField.RIGHT);
add(display, BorderLayout.NORTH);
ActionListener insert = new InsertAction();
ActionListener command = new CommandAction();
panel = new JPanel();
// 4行5列的按钮网格布局
panel.setLayout(new GridLayout(4, 5, 5, 5));
// 数字键及控制键的 加入(利用自定义函数)
addButton("7", insert);
addButton("8", insert);
addButton("9", insert);
addButton("/", command);
addButton("C", command);
addButton("4", insert);
addButton("5", insert);
addButton("6", insert);
addButton("*", command);
addButton("CE", command);
addButton("1", insert);
addButton("2", insert);
addButton("3", insert);
addButton("-", command);
addButton("←", command);
addButton("0", insert);
addButton(".", insert);
addButton("=", command);
addButton("+", command);
// 时间键的控制 按钮 (T)
JButton t=new JButton("Time");
t.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (sw){
sw = !sw ;
time.start();
}
else
{
sw = !sw ;
time.stop();
display.setText(0 + "");
}
}
});
panel.add(t);
add(panel, BorderLayout.CENTER);
// 以下两个函数用于调用时间 的实现
time =new javax.swing.Timer(1,new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
display.setText(pt());
}
});
}
// GregorianCalendar 类中时间的调用
String pt()
{
Calendar calendar = new GregorianCalendar();
return (calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH )+1) + "-"
+ calendar.get(Calendar.DAY_OF_MONTH) + " " + calendar.get(Calendar.HOUR)+":" +
calendar.get(Calendar.MINUTE)+":" + calendar.get(Calendar.SECOND));
}
// 添加按钮的函数
private void addButton(String label, ActionListener listener)
{
JButton button = new JButton(label);
button.addActionListener(listener);
panel.add(button);
}
// 数字键(1到9 .) 的各种功能实现
private class InsertAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
// 标签内容传给 input
String input = event.getActionCommand();
a=1;
// 用于判断sw 来控制是否显示时间
if(!sw)
{Toolkit.getDefaultToolkit().beep();}
else {
// �