开发工具:
文件大小: 6kb
下载次数: 0
上传时间: 2015-08-13
详细说明: package vacc; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.InputEvent; import java.awt.event.KeyEvent; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.PrintWriter; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.KeyStroke; import javax.swing.filechooser.FileNameExtensionFilter; public class TestNotepad { private JFrame jframe;// 主窗体 JPanel jpanel;// 面板 JMenuBar jmenuBar = new JMenuBar();// 菜单面板 JMenu jmenuFile, jmenuEdit;// 主菜单 JMenuItem jmenuItemOpen, jmenuItemNew, jmenuItemSave, jmenuItemSaveAs, jmenuItemExit;// 菜单中的子菜单 JTextArea jtextArea;// 文本框 JScrollPane jscrollPane;// 带滑动条的 JFileChooser jfileChooser;//文件选择器 File currentFile; public TestNotepad() { // TODO Auto-generated constructor stub jframe = new JFrame("我的记事本"); jpanel = new JPanel(); jmenuBar = new JMenuBar(); jtextArea = new JTextArea(); jtextArea.setLineWrap(true);//设置自动换行 jscrollPane = new JScrollPane(jtextArea); jfileChooser = new JFileChooser(); jfileChooser.setFileFilter(new FileNameExtensionFilter("文本文件(*.txt)", "txt")); jmenuFile = new JMenu("文件(F)"); jmenuFile.setMnemonic('F'); jmenuEdit = new JMenu("编辑(E)"); jmenuEdit.setMnemonic('E'); jmenuItemNew = new JMenuItem("新建(N)", 'N'); jmenuItemNew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_DOWN_MASK)); jmenuItemOpen = new JMenuItem("打开(O)", 'O'); jmenuItemOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_DOWN_MASK)); jmenuItemOpen.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub int res = jfileChooser.showOpenDialog(jframe); if(res == jfileChooser.APPROVE_OPTION) { currentFile = jfileChooser.getSelectedFile(); jtextArea.setText(""); openFile(currentFile); jframe.setTitle(currentFile + "-记事本"); System.out.println("s"); } } }); jmenuItemSave = new JMenuItem("保存(S)", 'S'); jmenuItemSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_DOWN_MASK)); jmenuItemSave.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub saveFile(); } }); jmenuItemSaveAs = new JMenuItem("另存为(A)", 'A'); jmenuItemSaveAs.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub int res = jfileChooser.showSaveDialog(jframe); if(res == jfileChooser.APPROVE_OPTION) { currentFile = jfileChooser.getSelectedFile(); jframe.setTitle(currentFile + "-记事本"); saveFileAs(currentFile, jtextArea.getText().replaceAll("\n", "\r\n")); } } }); jmenuBar.add(jmenuFile); jmenuBar.add(jmenuEdit); jmenuFile.add(jmenuItemNew); jmenuFile.add(jmenuItemOpen); jmenuFile.add(jmenuItemSave); jmenuFile.add(jmenuItemSaveAs); jpanel.setLayout(new BorderLayout(2, 1)); jpanel.add(jmenuBar, "North"); jpanel.add(jscrollPane, "Center"); jframe.add(jpanel); jframe.setSize(500, 500); jframe.setLocationRelativeTo(null); jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jframe.setVisible(true); } public void saveFile() { if(currentFile == null) { int res = jfileChooser.showSaveDialog(jframe); if(res == jfileChooser.APPROVE_OPTION) { currentFile = jfileChooser.getSelectedFile(); jframe.setTitle(currentFile + "-记事本"); } } if(currentFile != null) { saveFileAs(currentFile, jtextArea.getText().replaceAll("\n", "\r\n")); } } public void saveFileAs(File file, String text) { PrintWriter pw = null; try { pw = new PrintWriter(file); pw.write(text); pw.flush(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if(pw != null) { pw.close(); } } } public void openFile(File file) { BufferedReader br = null; try { br = new BufferedReader(new FileReader(file)); String str = br.readLine(); while (str != null) { jtextArea.append(str + "\n"); System.out.println(str); str = br.readLine(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if(br != null) { br.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static void main(String[] args) { new TestNotepad(); } } ...展开收缩
(系统自动生成,下载前可以参看下载内容)
下载文件列表
相关说明
- 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
- 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度。
- 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
- 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
- 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
- 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.