import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//主函数
public class ScienceCalculator {
public static void main(String[] args)
{
try {
UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) {
System.err.println("Couldn't use the system look and feel:"+e);
}
EventQueue.invokeLater(new Runnable()
{
public void run()
{
ScienceCalculatorFrame frame=new ScienceCalculatorFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
//界面设计
class ScienceCalculatorFrame extends JFrame implements ActionListener
{
public static final int DEFAULT_WIDTH=580;
public static final int DEFAULT_HEIGHT=260;
JMenuItem sItem;
JMenuItem tItem;
JMenuItem copyItem ;
JMenuItem pasteItem;
JMenuItem topHelp;
JMenuItem aboutCal;
JTextArea help;
JScrollPane scrollHelp;
String copy;
JButton buttonBac;
JButton buttonC;
JButton buttonCE;
JButton buttonLeftbracket;
JButton buttonRightbracket;
private JTextField textAnswer;
private boolean start;
private String lastCommand;
private double result;
private ButtonGroup group;
//菜单设计
public ScienceCalculatorFrame()
{
setTitle("计算器");
this.setLocation(400, 300);
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
setResizable(false);
this.setBackground(Color.BLUE);
ScienceCalculatorPanel panel = new ScienceCalculatorPanel();
add(panel);
JMenu viewMenu = new JMenu("查看(V)");
tItem=new JMenuItem("●标准型(T)");
viewMenu.add(tItem);
viewMenu.addSeparator();
sItem = new JMenuItem(" ●科学型(S)");
viewMenu.add(sItem);
viewMenu.addSeparator();
JMenu editMenu = new JMenu("编辑(E)");
copyItem = new JMenuItem(" 复制(C) Ctrl+C");
editMenu.add(copyItem);
editMenu.addSeparator();
pasteItem = new JMenuItem(" 粘贴(V) Ctrl+V");
editMenu.add(pasteItem);
editMenu.addSeparator();
JMenu helpMenu = new JMenu("帮助(H)");
topHelp = new JMenuItem(" 使用方法(H)");
helpMenu.add(topHelp);
helpMenu.addSeparator();
help = new JTextArea(5, 20);
scrollHelp = new JScrollPane(help);
help.setEditable(false);
help.append("执行简单计算\n ");
help.append("1. 键入计算的第一个数字。\n ");
help.append("2. 单击“+”执行加、“-”执行减、“*”执行乘或“/”执行除。\n ");
help.append("3. 键入计算的下一个数字。\n ");
help.append("4. 输入所有剩余的运算符和数字。\n ");
help.append("5. 单击“=”。\n ");
aboutCal = new JMenuItem(" 关于计算器");
helpMenu.add(aboutCal);
JMenuBar mainMenu = new JMenuBar();
setJMenuBar(mainMenu);
mainMenu.add(viewMenu);
mainMenu.add(editMenu);
mainMenu.add(helpMenu);
//ActionListener menu = new MenuAction();
tItem.addActionListener(this);
copyItem.addActionListener(this);
pasteItem.addActionListener(this);
topHelp.addActionListener(this);
aboutCal.addActionListener(this);
}
//菜单监听函数
public void actionPerformed(ActionEvent event)
{
Object temp = event.getSource();
if (temp == tItem)
{
//JOptionPane.showMessageDialog(sItem,"当前是标准型计算器, 科学型计算器有待更新。");
this.dispose();
CalculatorFrame cal = new CalculatorFrame();
cal.setVisible(true);
}
if(temp == copyItem )
{
copy = textAnswer.getText();
}
if (temp == pasteItem)
{
textAnswer.setText(copy);
}
if (temp == topHelp)
{
JOptionPane.showMessageDialog(topHelp, scrollHelp,"使用方法",JOptionPane.WARNING_MESSAGE);
}
if (temp == aboutCal)
{
//JOptionPane.showMessageDialog(aboutCal, "简易计算器 \n开发者:胡雪钦 \n版权所有");
JOptionPane.showMessageDialog(aboutCal, "科学计算器 \n开发者:胡雪钦 \n版权所有", "关于计算器",JOptionPane.WARNING_MESSAGE);
}
}
//界面按钮的设计
private class ScienceCalculatorPanel extends JPanel
{
public ScienceCalculatorPanel()
{
setLayout(new BorderLayout());
start = true;
lastCommand = "=";
result = 0;
GridBagLayout layout=new GridBagLayout();
setLayout(layout);
//输出框
textAnswer= new JTextField(37);
textAnswer.setText("0");
textAnswer.setEditable(false);
textAnswer.setHorizontalAlignment(JTextField.RIGHT);
textAnswer.setFont(new Font("Serif",Font.BOLD,17));
textAnswer.setBackground(Color.WHITE);
add(textAnswer,new GBC(0,0,20,1).setFill(GBC.BOTH));
JPanel n1= new JPanel();
group = new ButtonGroup();
n1.setBorder(BorderFactory.createEtchedBorder());
//addRadioButton("度",BorderFactory.createLoweredBevelBorder());
JRadioButton buttona = new JRadioButton("度",true);
JRadioButton buttonb = new JRadioButton("弧度");
JRadioButton buttonc = new JRadioButton("梯度");
group.add(buttona);
group.add(buttonb);
group.add(buttonc);
n1.setLayout(new GridLayout(1,3));
n1.add(buttona);
n1.add(buttonb);
n1.add(buttonc);
add(n1,new GBC(0,1,5,1).setAnchor(GBC.EAST).setFill(GBC.BOTH));
JButton buttonSpace = new JButton(" ");
add(buttonSpace,new GBC(0,2).setAnchor(GBC.EAST).setFill(GBC.BOTH));
JButton buttonINV = new JButton("INV");
add(buttonINV,new GBC(1,2).setAnchor(GBC.EAST).setFill(GBC.BOTH));
JButton buttonLn= new JButton(" ln ");
add(buttonLn,new GBC(2,2).setAnchor(GBC.EAST).setFill(GBC.BOTH));
buttonLeftbracket = new JButton("(");
add(buttonLeftbracket,new GBC(3,2).setAnchor(GBC.EAST).setFill(GBC.BOTH));
buttonRightbracket = new JButton(")");
add(buttonRightbracket,new GBC(4,2).setAnchor(GBC.EAST).setFill(GBC.BOTH));
JButton buttonInt = new JButton(" Int ");
add(buttonInt,new GBC(0,3).setAnchor(GBC.EAST).setFill(GBC.BOTH));
JButton buttonSinh = new JButton("sinh");
add(buttonSinh,new GBC(1,3).setAnchor(GBC.EAST).setFill(GBC.BOTH));
JButton buttonSin= new JButton(" sin");
add(buttonSin,new GBC(2,3).setAnchor(GBC.EAST).setFill(GBC.BOTH));
JButton buttonSquare = new JButton("x^2");
add(buttonSquare,new GBC(3,3).setAnchor(GBC.EAST).setFill(GBC.BOTH));
JButton buttonL = new JButton("n!");
add(buttonL,new GBC(4,3).setAnchor(GBC.EAST).setFill(GBC.BOTH));
JButton buttonDms = new JButton(" dms ");
add(buttonDms,new GBC(0,4).setAnchor(GBC.EAST).setFill(GBC.BOTH));
JButton buttonCosh = new JButton("cosh");
add(buttonCosh,new GBC(1,4).setAnchor(GBC.EAST).setFill(GBC.BOTH));
JButton buttonCos= new JButton(" cos");
add(buttonCos,new GBC(2,4).setAnchor(GBC.EAST).setFill(GBC.BOTH));
JButton buttonX = new JButton("x^y");
add(buttonX,new GBC(3,4).setAnchor(GBC.EAST).setFill(GBC.BOTH));
JButton buttonXSqrt = new JButton("y√x");
add(buttonXSqrt,new GBC(4,4).setAnchor(GBC.EAST).setFill(GBC.BOTH));
JButton buttonPI = new JButton(" π ");
add(buttonPI,new GBC(0,5).setAnchor(GBC.EAST).setFill(GBC.BOTH));
JButton buttonTanh = new JButton("tanh");
add(buttonTanh,new GBC(1,5).setAnchor(GBC.EAST).setFill(GBC.BOTH));
JButton buttonTan= new JButton("tan");
add(buttonTan,new GBC(3,5).setAnchor(GBC.EAST).setFill(GBC.BOTH));
JButton buttonCube = new JButton("x^3");
add(buttonCube,new GBC(4,5).setAnchor(GBC.EAST).setFill(GBC.BOTH));
JButton button3Sqrt = new JButton("y√x");
add(button3Sqrt,new GBC(2,5).setAnchor(GBC.EAST).setFill(GBC.BOTH));
JButton buttonFE = new JButton("F-E ");
add(buttonFE,new GBC(0,6).setAnchor(GBC.EAST).setFill(GBC.BOTH));
JButton buttonExp = new JButton("Exp");
add(buttonExp,new GBC(1,6).setAnchor(GBC.EAST).setFill(GBC.BOTH));
JButton buttonMOD= new JButton("Mod");
add(buttonMOD,new GBC(3,6).setAnchor(GBC.EAST).setFill(GBC.BOTH));
JButton buttonLog = new JButton("log");
没有合适的资源?快使用搜索试试~ 我知道了~
java写的简单 计算器(包括科学计算器的部分功能)
共23个文件
class:16个
java:4个
project:1个
需积分: 35 8 下载量 156 浏览量
2011-01-01
21:59:40
上传
评论 1
收藏 34KB ZIP 举报
温馨提示
一个简易的计算器,可以进行简单的四则运算:加、减、乘、除等(限于十进制下) (1)接收终端输入的某种四则运算式,例如“2*(3+1)”,当用户敲回车时,即给出运算结果,显示为:“2*(3+1)=8” (2)运算符号定为:“+”,“-”,“*”,“/”。
资源推荐
资源详情
资源评论
收起资源包目录
Calculator.zip (23个子文件)
Calculator
.project 386B
bin
CalculatorFrame$InsertAction.class 1KB
Expcalculation.class 4KB
ScienceCalculatorFrame$CommandAction.class 2KB
GBC.class 1KB
ScienceCalculator.class 1KB
CalculatorFrame$MemoryAction.class 2KB
ScienceCalculatorFrame.class 5KB
CalculatorFrame$CommandAction.class 1KB
Calculator.class 1KB
ScienceCalculatorFrame$InsertAction.class 1KB
ScienceCalculatorFrame$ScienceCalculatorPanel.class 8KB
CalculatorFrame$CalculatorPanel.class 5KB
Calculator$1.class 632B
ScienceCalculator$1.class 674B
ScienceCalculatorFrame$MemoryAction.class 2KB
CalculatorFrame.class 5KB
src
ScienceCalculator.java 14KB
Calculator.java 11KB
GBC.java 967B
Expcalculation.java 4KB
.fatjar 339B
.classpath 232B
共 23 条
- 1
资源评论
DreamNextStop
- 粉丝: 2
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功