ImageVerifierCode 换一换
格式:DOCX , 页数:14 ,大小:44.37KB ,
资源ID:12221041      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/12221041.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(java 简单记事本编辑设计 5.docx)为本站会员(b****4)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

java 简单记事本编辑设计 5.docx

1、java 简单记事本编辑设计 5GDOU-B-11-112广东海洋大学学生实验报告书(学生用表)实验名称简单记事本编辑设计课程名称Java实验课课程号学院(系)数学与计算机学院专业计算机科学与技术班级计科11XX 学生姓名林XX学号20151162XXXX实验地点钟04002实验日期2017/11/16一、实验目的:熟悉Swing组件及相关方法:掌握常用Swing组件的菜单的使用方法、组件的事件监听方法以及主要布局管理器的使用。本实验需4学时二、实验内容:编写如下图的一个记事本,并可实现图中列出的功能。 答:源码/import java.awt.FileDialog;/import java.

2、awt.event.ActionEvent;/import java.awt.event.ActionListener;import java.awt.event.*;import java.awt.*;/import java.awt.event.KeyEvent;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.nio.file.*;import java.io.FileReader;import java.io.FileWriter;import java

3、.io.IOException;import javax.swing.*;public class NotePadDemo extends JFrame implements ActionListener JMenuBar menubar; JMenu menu1,menu2,menu3,menu4,menu5; JMenuItem item1,item2,item3,item4,item5,item6,item7,item8,item9,item10; JTextArea area; public NotePadDemo() public NotePadDemo(String s,int x

4、,int y,int h,int w) this.setTitle(s); menubar = new JMenuBar(); menu1 = new JMenu(文件); menu2 = new JMenu(编辑); menu3 = new JMenu(格式); menu4 = new JMenu(关于); item1 = new JMenuItem(新建); item2 = new JMenuItem(打开); item3 = new JMenuItem(保存); item4 = new JMenuItem(退出); item1.setAccelerator(KeyStroke.getKe

5、yStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK); item2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK); item3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK); item4.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,InputEvent.CTRL_MASK); menu1.add(it

6、em1); menu1.add(item2); menu1.add(item3); menu1.addSeparator(); menu1.add(item4); item5 = new JMenuItem(剪切); item5.addActionListener(new ActionListener() Override public void actionPerformed(ActionEvent e) area.cut(); ); item6 = new JMenuItem(复制); item6.addActionListener(new ActionListener() Overrid

7、e public void actionPerformed(ActionEvent e) area.copy(); ); item7 = new JMenuItem(粘贴); item7.addActionListener(new ActionListener() Override public void actionPerformed(ActionEvent e) area.paste();/ 粘贴 ); item8 = new JMenuItem(查找); item9 = new JMenuItem(替换); item8.addActionListener(new ActionListen

8、er() public void actionPerformed(ActionEvent e) final JDialog dialog = new JDialog(NotePadDemo.this,查找字符串.,true); dialog.setBounds(560,250,310,130); JLabel find = new JLabel(请输入字符串 :); final JTextField findtext = new JTextField(1); JButton jbu = new JButton(查找); dialog.setLayout(null); find.setBound

9、s(10,30,90,20); findtext.setBounds(100,30,90,20); jbu.setBounds(200,30,80,20); dialog.add(find); dialog.add(findtext); dialog.add(jbu); jbu.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) String text = area.getText(); String str = findtext.getText(); int end = text.

10、length(); int len = str.length(); int start = area.getSelectionEnd(); if(start = end) start = 0; for(;start=end-len;start+) if(text.substring(start,start+len).equals(str) area.setSelectionStart(start); area.setSelectionEnd(start+len); return; /若找不到待查字符串,则将光标置于末尾 area.setSelectionStart(end); area.set

11、SelectionEnd(end); ); dialog.addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) dialog.dispose(); ); dialog.setResizable(false); dialog.setVisible(true); ); item9.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) final JDialog dialog = new

12、JDialog(NotePadDemo.this,字符串替换.,true); dialog.setBounds(560,250,310,180); final JLabel tihuan = new JLabel(请输入要替换的字符串 :); JLabel mubiao = new JLabel(请输入替换后的字符串 :); JTextField jtf1 = new JTextField(10); JTextField jtf2 = new JTextField(10); JButton jb = new JButton(替换); dialog.setLayout(null); tihuan

13、.setBounds(10,30,150,20); mubiao.setBounds(10,70,150,20); jtf1.setBounds(160,30,110,20); jtf2.setBounds(160,70,110,20); jb.setBounds(100,110,80,20); dialog.add(tihuan); dialog.add(mubiao); dialog.add(jtf1); dialog.add(jtf2); dialog.add(jb); final String text = area.getText(); final String str1 = tih

14、uan.getText(); final String str2 = mubiao.getText(); jb.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) if(area.getSelectedText().equals(tihuan.getText() area.replaceRange(str2,area.getSelectionStart(),area.getSelectionEnd(); else int end=text.length(); int len=str1

15、.length(); int start=area.getSelectionEnd(); if(start=end) start=0; for(;start=end-len;start+) if(text.substring(start,start+len).equals(str1) area.setSelectionStart(start); area.setSelectionEnd(start+len); return; /若找不到待查字符串,则将光标置于末尾 area.setSelectionStart(end); area.setSelectionEnd(end); ); dialog

16、.setResizable(false); dialog.setVisible(true); ); item10 = new JMenuItem(全选); item10.addActionListener(new ActionListener() Override public void actionPerformed(ActionEvent e) area.selectAll(); ); item5.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK); item6.setAccelerator(K

17、eyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK); item7.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK); item8.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F,InputEvent.CTRL_MASK); item9.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R,InputEvent.CTRL_MASK)

18、; item10.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,InputEvent.CTRL_MASK); menu2.add(item5); menu2.add(item6); menu2.add(item7); menu2.add(item8); menu2.add(item9); menu2.addSeparator(); menu2.add(item10); menubar.add(menu1); menubar.add(menu2); menubar.add(menu3); menubar.add(menu4); this.

19、setJMenuBar(menubar); item1.addActionListener(this); item2.addActionListener(this); item3.addActionListener(this); item4.addActionListener(this); item5.addActionListener(this); item6.addActionListener(this); item7.addActionListener(this); item8.addActionListener(this); item9.addActionListener(this);

20、 item10.addActionListener(this); area = new JTextArea(6,12); area.setForeground(Color.WHITE); area.setBackground(Color.BLACK); area.setFont(new Font(微软雅黑, Font.PLAIN, 24); area.setLineWrap(true); this.add(new JScrollPane(area); this.setLocation(x, y); this.setSize(w, h); this.setVisible(true); this.

21、setDefaultCloseOperation(DISPOSE_ON_CLOSE); public static void main(String args) NotePadDemo jsb = new NotePadDemo(记事本,50,50,500,600); Override public void actionPerformed(ActionEvent e) / TODO Auto-generated method stub if(e.getSource() = item1) area.setText(); if(e.getSource() = item2) JFileChoose

22、r choose = new JFileChooser(); choose.setDialogTitle(打开); File file; int result = choose.showOpenDialog(NotePadDemo.this); if (result = JFileChooser.APPROVE_OPTION) area.setText(); try file = choose.getSelectedFile(); String pathStr = file.getPath(); byte b = Files.readAllBytes(Paths.get(pathStr); /

23、FileReader fr = new FileReader(file); /System.out.println(encoding: + fr.getEncoding(); /char ch = null; /fr.read(ch); String str = null; if(b!=null) str = new String(b, UTF-8); if(str!=null) area.setText(str);/ BufferedReader br = new BufferedReader(fr); / String str = null; / while (str = br.readL

24、ine() != null) / area.append(str + rn); / / 设置记事本标题 setTitle(file.getName() + 记事本); / br.close(); catch (Exception ee) ee.printStackTrace(); if(e.getSource() = item3) if (area.getText() != null & !area.getText().equals() JFileChooser choose = new JFileChooser(); int result = choose.showSaveDialog(NotePadDemo.this); if (result = JFileChooser.APPROVE_OPTION) File file; try file = choose.getSelectedFile(); BufferedWriter bw = new BufferedWriter(new FileWriter(file); bw.write(area.getText(); bw.close(); catch (IOException e1) e1.printStackTrace(); 截图成绩指导教师日期注:请用A4纸书写,不够另附纸。 第 页,共 页

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1