11计科 实验十二 记事本制作.docx
- 文档编号:7233680
- 上传时间:2023-01-22
- 格式:DOCX
- 页数:13
- 大小:224.49KB
11计科 实验十二 记事本制作.docx
《11计科 实验十二 记事本制作.docx》由会员分享,可在线阅读,更多相关《11计科 实验十二 记事本制作.docx(13页珍藏版)》请在冰豆网上搜索。
11计科实验十二记事本制作
西安思源学院
Java实验报告
实验名称:
记事本的制作
班级:
11计算机科学与技术
学号:
111102020104
姓名:
指导教师:
王振铎
实验时间:
2013年5月14日
成绩:
实验十二记事本制作
一实验目的
1.掌握图形界面中菜单的制作方法。
2.掌握菜单的事件处理程序的编写。
3.熟悉记事本中JFileChooser,JOptionPane等系统类的使用方法。
二实验内容
1、仿照windows记事本制作菜单。
2、编程记事本的各个功能。
实验内容如下:
实验主要代码为:
只粘贴部分源码
jScrollPane1.setViewportView(mainTextArea);
fileMenu.setMnemonic('F');
fileMenu.setText("文件(F)");
newMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N,java.awt.event.InputEvent.CTRL_MASK));
newMenuItem.setMnemonic('N');
newMenuItem.setText("新建(N)");
newMenuItem.addActionListener(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(java.awt.event.ActionEventevt){
newMenuItemActionPerformed(evt);
}
});
fileMenu.add(newMenuItem)
openMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O,java.awt.event.InputEvent.CTRL_MASK));
openMenuItem.setMnemonic('O');
openMenuItem.setText("打开(O)");
openMenuItem.addActionListener(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(java.awt.event.ActionEventevt){
openMenuItemActionPerformed(evt);
}
});
fileMenu.add(openMenuItem);
saveMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S,java.awt.event.InputEvent.CTRL_MASK));
saveMenuItem.setMnemonic('S');
saveMenuItem.setText("保存(S)");
saveMenuItem.addActionListener(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(java.awt.event.ActionEventevt){
saveMenuItemActionPerformed(evt);
}
});
fileMenu.add(saveMenuItem);
fileMenu.add(jSeparator1);printMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_P,java.awt.event.InputEvent.CTRL_MASK));
printMenuItem.setMnemonic('P');
printMenuItem.setText("打印(P)");
printMenuItem.setEnabled(false);
fileMenu.add(printMenuItem);
fileMenu.add(jSeparator2);
editMenu.add(copyMenuItem);
pasteMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_V,java.awt.event.InputEvent.CTRL_MASK));
pasteMenuItem.setMnemonic('P');
pasteMenuItem.setText("粘贴(P)");
pasteMenuItem.addActionListener(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(java.awt.event.ActionEventevt){
pasteMenuItemActionPerformed(evt);
}
});
editMenu.add(pasteMenuItem);
editMenu.add(searchMenuItem);
searchnextMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F3,0));
searchnextMenuItem.setMnemonic('N');
searchnextMenuItem.setText("查找下一个(N)");
searchnextMenuItem.addActionListener(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(java.awt.event.ActionEventevt){
searchnextMenuItemActionPerformed(evt);
}
});
editMenu.add(searchnextMenuItem);
exchangeMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_H,java.awt.event.InputEvent.CTRL_MASK));
exchangeMenuItem.setMnemonic('R');
exchangeMenuItem.setText("替换(R)");
exchangeMenuItem.addActionListener(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(java.awt.event.ActionEventevt){
exchangeMenuItemActionPerformed(evt);
}
});
editMenu.add(exchangeMenuItem);
editMenu.add(jSeparator5);
selectallMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A,java.awt.event.InputEvent.CTRL_MASK));
selectallMenuItem.setMnemonic('A');
selectallMenuItem.setText("全选(A)");
selectallMenuItem.addActionListener(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(java.awt.event.ActionEventevt){
selectallMenuItemActionPerformed(evt);
}
});
editMenu.add(selectallMenuItem);
formatMenu.add(autodownCheckBoxMenuItem);
fontMenuItem.setMnemonic('F');
fontMenuItem.setText("字体(F)");
fontMenuItem.addActionListener(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(java.awt.event.ActionEventevt){
fontMenuItemActionPerformed(evt);
}
});
formatMenu.add(fontMenuItem);
jMenuBar1.add(formatMenu);
viewMenu.setMnemonic('V');
viewMenu.setText("查看(V)");
stateMenuItem.setMnemonic('S');
stateMenuItem.setText("状态栏(S)");
stateMenuItem.setEnabled(false);
stateMenuItem.addActionListener(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(java.awt.event.ActionEventevt){
stateMenuItemActionPerformed(evt);
}
});
viewMenu.add(stateMenuItem);
jMenuBar1.add(viewMenu);
helpMenu.setMnemonic('H');
helpMenu.setText("帮助(H)");
helpMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F1,0));
helpMenuItem.setMnemonic('H');
helpMenuItem.setText("帮助主题(H)");
helpMenuItem.addActionListener(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(java.awt.event.ActionEventevt){
helpMenuItemActionPerformed(evt);
}
});
helpMenu.add(helpMenuItem);
helpMenu.add(jSeparator6);
aboutMenuItem.setMnemonic('A');
aboutMenuItem.setText("关于(A)");
aboutMenuItem.addActionListener(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(java.awt.event.ActionEventevt){
aboutMenuItemActionPerformed(evt);
}
});
helpMenu.add(aboutMenuItem);
jMenuBar1.add(helpMenu);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayoutlayout=newjavax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1,javax.swing.GroupLayout.DEFAULT_SIZE,536,Short.MAX_VALUE)
);
pack();
}//
privatevoidopenMenuItemActionPerformed(java.awt.event.ActionEventevt){
//TODOaddyourhandlingcodehere:
openText();
}
privatevoidnewText(){
intselection=JOptionPane.showConfirmDialog(this,
"是否保存当前文本文档?
","保存提示",JOptionPane.OK_CANCEL_OPTION,
JOptionPane.WARNING_MESSAGE);
if(selection==JOptionPane.OK_OPTION){
saveText();
}
mainTextArea.setText("");
setTitle("新建文本文档.txt");
}
privatevoidopenText(){
intselection=JOptionPane.showConfirmDialog(this,
"是否保存当前文本文档?
","保存提示",JOptionPane.OK_CANCEL_OPTION,
JOptionPane.WARNING_MESSAGE);
if(selection==JOptionPane.OK_OPTION){
saveText();
}
try{
Framef=newFrame();
FileDialogfd=newFileDialog(f,"打开文件",FileDialog.LOAD);
fd.setVisible(true);
Stringfpath=fd.getDirectory();
finalStringfname=fd.getFile();
BufferedReaderbr=newBufferedReader(newFileReader(fpath+fname));
mainTextArea.setText("");
setTitle(fname);
Strings=br.readLine();
while(s!
=null){
mainTextArea.append(s+"\n");
s=br.readLine();
}
br.close();
}catch(Exceptionex){
}
}
privatevoidsaveText(){
Stringfns=null;
Framef=newFrame("保存");
FileDialogfd=newFileDialog(f,"保存文件",FileDialog.SAVE);
Stringsavepath=fd.getDirectory();
Stringsavename=fd.getFile();
fd.setFile("*.txt");
fd.setVisible(true);
try{
if(savename!
=null){
PrintWriterpw=newPrintWriter(newBufferedWriter(newFileWriter(savepath+savename)));
pw.write(mainTextArea.getText(),0,mainTextArea.getText().length());
pw.flush();
}
}catch(Exceptionesave){
}
}
privatevoidrepeal(){
try{
undo.undo();
//System.out.println(undo.canUndo());
}catch(Exceptioneundo){
}
}
publicvoidsearch(){
try{
finalJDialogjd=newJDialog(this,"查找",true);
GridBagLayoutgbl=newGridBagLayout();
GridBagConstraintsgbc=newGridBagConstraints();
JLabeljlFind=newJLabel("查找内容:
");
gbc.gridx=0;
gbc.gridy=0;
jd.getContentPane().add(jlFind);
JButtonjbFind=newJButton("查找");
jbFind.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventefind){
StringstrA=mainTextArea.getText();
StringstrB=jtf.getText();
if(a>=0){
a=strA.indexOf(strB,StartFindPos);
b=strB.length();
StartFindPos=a+b;
if(a==-1){
JOptionPane.showMessageDialog(null,"没有您要查找的信息","查找结果",1)
a=0;
StartFindPos=0;
}
mainTextArea.select(a,StartFindPos);
}
}
});
JButtonjbCancel=newJButton("取消");
jbCancel.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventejb){
jd.dispose();
}
});
privatevoidautodown(){
if(autodownCheckBoxMenuItem.getState()){
mainTextArea.setLineWrap(true);
mainTextArea.setWrapStyleWord(true);
}else{
mainTextArea.setLineWrap(false);
mainTextArea.setWrapStyleWord(false);
}
}
privatevoidsetFont(){
finalJDialogjd=newJDialog(this,"字体设置");
jd.setLocation(240,200);
GridBagLayoutgbl=newGridBagLayout();
GridBagConstraintsgbc=newGridBagConstraints();
.....{
jtf2.setText((String)jcb2.getSelectedItem());
}
});
finalJComboBoxjcb3=newJComboBox();
jcb3.addItem("14")jcb3.addItem("18");jcb3.addItem("22");jcb3.addItem("26");
jcb3.addItem("30");jcb3.addItem("34");jcb3.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
jtf3.setText((String)jcb3.getSelectedItem());
publicvoidactionPerformed(ActionEvente){
if(jtf2.getText().equals("PLAIN")){
privatevoidexitapp(){
//TODOaddyourhandlingcodehere:
intselection=JOptionPane.showConfirmDialog(this,
"是否保存当前文本文档?
","保存提示",JOptionPane.OK_CANCEL_OPTION,
JOptionPane.WARNING_MESSAGE);
if(selection==JOptionPane.OK_OPTION){
saveText();
}else{
System.exit(0);
}
}
publicstaticvoidmain(Stringargs[]){
java.awt.EventQueue.invokeLater(newRunnable(){
publicvoidrun(){
newMain().setVisible(true);
}
});
程序运行截图如下:
保存文件:
设置字体:
帮助栏:
新建目录栏;
编辑目录栏;
格式目录栏:
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 11计科 实验十二 记事本制作 11 实验 十二 记事本 制作
![提示](https://static.bdocx.com/images/bang_tan.gif)