java实验记事本+计算器文档格式.docx
- 文档编号:20853247
- 上传时间:2023-01-26
- 格式:DOCX
- 页数:23
- 大小:70.64KB
java实验记事本+计算器文档格式.docx
《java实验记事本+计算器文档格式.docx》由会员分享,可在线阅读,更多相关《java实验记事本+计算器文档格式.docx(23页珍藏版)》请在冰豆网上搜索。
四、设计思路:
五、程序源代码:
1题
//****计算器****
importjava.awt.Color;
importjava.awt.Container;
importjava.awt.FlowLayout;
importjava.awt.GridLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JPanel;
importjavax.swing.JTextArea;
importjavax.swing.JTextField;
publicclassCalculatorextendsJFrame{
inti,j,k,p,q;
intz=0;
char[]ch1;
char[]ch2;
doublem,n,x;
doublesum=0;
doublecut=1;
doubleProduct=1;
doubleexcept=1;
JTextAreata;
publicCalculator(){
super("
Calculator"
);
setSize(200,240);
setLayout(newFlowLayout());
JPanel[]pe={newJPanel(),newJPanel(),newJPanel()};
pe[0].setLayout(newFlowLayout());
ta=newJTextArea("
0"
1,17);
ta.setEditable(false);
pe[0].add(ta);
pe[1].setLayout(newFlowLayout(FlowLayout.CENTER,12,4));
pe[2].setLayout(newGridLayout(4,4,4,4));
finalJButton[]bt1={newJButton("
Back"
),newJButton("
CE"
C"
)};
for(i=0;
i<
bt1.length;
i++){
pe[1].add(bt1[i]);
bt1[i].addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
JButtonb=(JButton)e.getSource();
if(b.getActionCommand().equals("
)){
ch1=ta.getText().toCharArray();
ta.setText(null);
if(ch1.length>
1)
for(i=0;
ch1.length-1;
ta.append("
"
+ch1[i]);
}elseif(ch1.length<
=1){
ta.setText("
}
q=1;
}elseif(b.getActionCommand().equals("
ta.setText("
q=1;
sum=0;
cut=1;
Product=1;
except=1;
z=0;
}
}
);
}
JButton[]bt2={
newJButton("
7"
8"
9"
+"
),
4"
5"
6"
-"
1"
2"
3"
*"
."
="
/"
for(j=0;
j<
bt2.length;
j++){
pe[2].add(bt2[j]);
bt2[j].addActionListener(newActionListener(){
if(b.getActionCommand().equals("
if(Double.valueOf(ta.getText())==0){
ch2=ta.getText().toCharArray();
for(p=0;
p<
ch2.length;
p++){
if(ch2[p]=='
.'
)
q=3;
if(q==3)
ta.append("
else
ta.setText("
}
else
ta.append("
elseif(b.getActionCommand().equals("
}
}
m=Double.valueOf(ta.getText());
sum=m;
ta.setText("
k=1;
q=1;
cut=m;
k=2;
Product=m;
k=3;
except=m;
k=4;
ch2=ta.getText().toCharArray();
for(p=0;
if(ch2[p]=='
q=2;
if(q==1)
ta.append("
else
if(k==1){sum=sum+Double.valueOf(ta.getText());
+sum);
}
elseif(k==2){cut=cut-Double.valueOf(ta.getText());
+cut);
elseif(k==3){Product=Product*Double.valueOf(ta.getText());
+Product);
elseif(k==4){except=except/Double.valueOf(ta.getText());
+except);
});
this.add(pe[0]);
this.add(pe[1]);
this.add(pe[2]);
for(j=1;
=2;
j++)
this.add(pe[j]);
this.validate();
this.setVisible(true);
this.setResizable(false);
}
publicstaticvoidmain(String[]args){
Calculatorca=newCalculator();
ca.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
运行结果:
2题
//****记事本****
importjava.awt.*;
importjava.io.BufferedReader;
importjava.io.FileNotFoundException;
importjava.io.FileReader;
importjava.io.FileWriter;
importjava.io.IOException;
importjava.io.PrintWriter;
importjavax.swing.*;
publicclassDocextendsJFrame{
Docdoc;
staticintsize=12;
inti,j,k,m,n;
JTextAreatextarea;
Stringfile="
;
FileDialogfileload=newFileDialog(this,"
打开文件"
FileDialog.LOAD);
FileDialogfilesave=newFileDialog(this,"
保存文件"
FileDialog.SAVE);
publicDoc(){
记事本"
setSize(400,600);
try{
JMenuBarmenuBar=newJMenuBar();
setJMenuBar(menuBar);
//设置菜单栏
//****设置下拉式菜单
//文件
JMenumenu1=newJMenu("
文件"
finalJMenuItem[]menuItem1={
newJMenuItem("
新建"
),newJMenuItem("
打开"
newJMenuItem("
保存"
),newJMenuItem("
另存为"
退出"
};
menuItem1.length;
menu1.add(menuItem1[i]);
menuItem1[i].addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
JMenuItemmi1=(JMenuItem)e.getSource();
if(mi1.getActionCommand().equals("
intcount=JOptionPane.showConfirmDialog(doc,
"
是否保存?
\n选择“是”-保存\n选择“否”-另存\n
选择“取消”-返回"
"
JOptionPane.YES_NO_CANCEL_OPTION);
if(count==0){
saveFile();
textarea.setText("
}
elseif(count==1){
saveAs();
elseif(count==-1)
elseif(mi1.getActionCommand().equals("
openFile();
saveFile();
saveAs();
System.exit(0);
});
//编辑
JMenumenu2=newJMenu("
编辑"
finalJMenuItem[]menuItem2={newJMenuItem("
复制"
剪切"
粘贴"
)
menuItem2.length;
menu2.add(menuItem2[j]);
menuItem2[j].addActionListener(newActionListener(){
JMenuItemmi2=(JMenuItem)e.getSource();
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java 实验 记事本 计算器