计算机2011级 程序语言综合课程设计
HandleExp handleExp; HandleOff handleOff; public CalculatorWindow(){
在主类中定义各个组件、方法。主类中通过调用各个方法进行运算: 2、HandleCos方法:实现cos键运算功能: import java.util.LinkedList; import javax.swing.*; import java.awt.event.*;
public class HandleCos implements ActionListener{ LinkedList
JTextField resultShow,showComputerProcess; JTextArea saveComputerProcess; HandleCos(LinkedList
JTextArea t3){ this.list=list; resultShow=t1;
showComputerProcess=t2; saveComputerProcess=t3; }
public void actionPerformed(ActionEvent e){ if(list.size()==1||list.size()==2){ String numOne=list.getFirst();
try{ double x=Double.parseDouble(numOne); double result=Math.cos(x);
String str=String.valueOf(result); list.set(0,str);
resultShow.setText(str);
String proccess=\ showComputerProcess.setText(proccess); saveComputerProcess.append(\\
if(list.size()==2)
list.removeLast(); //移掉运算符号
list,JTextField
8
计算机2011级 程序语言综合课程设计
}
catch(Exception ee){} }
else if(list.size()==3){
String numTwo=list.getLast();
try{ double x=Double.parseDouble(numTwo); double result=Math.cos(x);
String str=String.valueOf(result); list.set(0,str);
resultShow.setText(str);
String proccess=\ showComputerProcess.setText(proccess); saveComputerProcess.append(\\
list.removeLast(); //移掉第2个运算数 list.removeLast(); //移掉运算符号 }
catch(Exception ee){} }
} }
3、help类:实现帮助文档: import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.tree.*; import java.io.*;
public class Help implements TreeSelectionListener{
JEditorPane editorPane; public Help() {
JFrame help = new JFrame(\计算器\
Container contentPane = help.getContentPane(); DefaultMutableTreeNode
root
=
new
9
计算机2011级 程序语言综合课程设计
DefaultMutableTreeNode(\帮助\
tree.getSelectionModel().setSelectionMode(TreeSelectio
tree.addTreeSelectionListener(this);
JScrollPane scrollPane1 = new JScrollPane(tree); editorPane = new JEditorPane();
JScrollPane scrollPane2 = new JScrollPane(editorPane); JSplitPane
splitPane
=
new
nModel.SINGLE_TREE_SELECTION);
DefaultMutableTreeNode DefaultMutableTreeNode DefaultMutableTreeNode root.add(t1); root.add(t2); t1.add(t1_1);
JTree tree = new JTree(root);
t1 t1_1 t2
= = =
new new new
DefaultMutableTreeNode(\帮助信息 \DefaultMutableTreeNode(\说明\DefaultMutableTreeNode(\简介\
JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true, scrollPane1, scrollPane2);
}
public void valueChanged(TreeSelectionEvent e) {
JTree tree = (JTree) e.getSource(); DefaultMutableTreeNode
selectionNode
=
contentPane.add(splitPane); help.pack();
help.setBounds(100,50,600,400); help.setVisible(true);
(DefaultMutableTreeNode)
tree.getLastSelectedPathComponent();
String nodeName = selectionNode.toString(); if (selectionNode.isLeaf()) {
String
filepath
=
\
+
10
计算机2011级 程序语言综合课程设计
System.getProperty(\
System.getProperty(\ } }
4、HandleDot类:实现小数点事件: import java.util.LinkedList; import javax.swing.*; import java.awt.event.*;
public class HandleDot implements ActionListener{ LinkedList
JTextField resultShow,showComputerProcess; HandleDot(LinkedList
showComputerProcess=t2; }
public void actionPerformed(ActionEvent e){ String dot=e.getActionCommand(); if(list.size()==1){
String num=list.getFirst(); String s=null;
if(num.indexOf(dot)==-1){ s=num.concat(dot);
list.set(0,s); } else
list,JTextField
}
else editorPane.setText(null); }
try {
editorPane.setPage(filepath); System.out.println(\找不到此文件\} catch (IOException ex) {
11
计算机2011级 程序语言综合课程设计
s=num; list.set(0,s);
resultShow.setText(s);
showComputerProcess.setText(\ }
else if(list.size()==3){ String num=list.getLast(); String s=null;
if(num.indexOf(dot)==-1){ s=num.concat(dot); list.set(2,s); } else s=num;
resultShow.setText(s);
showComputerProcess.setText(\+
\ } } }
5 系统运行及使用说明
1、系统运行结果
采用
eclipse
工具对程序进行编译运行;运行结果:
12