JAVA--简单记事本
来源:互联网 发布:大陆单片机 编辑:程序博客网 时间:2024/05/19 04:03
代码分为三部分:界面、打开、保存
界面代码:
public void init() { //外框 frame = new Frame("记事本"); frame.setVisible(true); frame.setSize(1300, 900); frame.setLocation(300, 400); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); // MenuBar mb = new MenuBar(); Menu file = new Menu("文件"); help = new Menu("帮助"); MenuItem shelp = new MenuItem("帮助"); open = new MenuItem("打开"); save = new MenuItem("保存"); file.add(open); file.add(save); help.add(shelp); ta = new TextArea(); ta.setFont(new Font("黑体",0,30)); frame.add(ta); mb.add(file); mb.add(help); frame.setMenuBar(mb); }
打开文件:
public void loadFile() throws Exception { fd_load.setVisible(true); String setFile = fd_load.getDirectory() + fd_load.getFile(); FileInputStream fis = new FileInputStream(setFile); byte b[] = new byte[1024]; int n; while((n = fis.read(b)) != -1) { String s = new String(b, 0, n); ta.append(s); } }
保存文件:
public void saveFile() throws Exception { fd_save.setVisible(true); String path = fd_save.getDirectory()+fd_save.getFile() +".txt"; FileOutputStream fos = new FileOutputStream(path); String sa = ta.getText(); byte b[] = sa.getBytes(); fos.write(b); fos.close(); }
完整源代码
package test;import java.awt.*;import java.awt.event.*;import java.io.FileInputStream;import java.io.FileOutputStream;import javax.swing.JOptionPane;public class txt implements ActionListener{ private Frame frame; private Menu help; private MenuItem open; private MenuItem save; private TextArea ta; private FileDialog fd_load; private FileDialog fd_save; private Frame fhelp; public static void main(String[] args) { txt hj = new txt(); hj.init(); } public void init() { //外框 frame = new Frame("记事本"); frame.setVisible(true); frame.setSize(1300, 900); frame.setLocation(300, 400); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); // MenuBar mb = new MenuBar(); Menu file = new Menu("文件"); help = new Menu("帮助"); MenuItem shelp = new MenuItem("帮助"); open = new MenuItem("打开"); save = new MenuItem("保存"); help.addActionListener(this); open.addActionListener(this); save.addActionListener(this); file.add(open); file.add(save); help.add(shelp); ta = new TextArea(); ta.setFont(new Font("黑体",0,30)); frame.add(ta); mb.add(file); mb.add(help); frame.setMenuBar(mb); fd_load = new FileDialog(frame, "打开文件", FileDialog.LOAD); fd_save = new FileDialog(frame, "保存文件", FileDialog.SAVE); } public void actionPerformed(ActionEvent e) { String s = e.getActionCommand(); try{ if(s.equals("打开")) { this.loadFile(); } else if(s.equals("保存")) { this.saveFile(); } else if(s.equals("帮助")) { this.help(); } }catch (Exception e1) { e1.printStackTrace(); } } public void saveFile() throws Exception { fd_save.setVisible(true); String path = fd_save.getDirectory()+fd_save.getFile() +".txt"; FileOutputStream fos = new FileOutputStream(path); String sa = ta.getText(); byte b[] = sa.getBytes(); fos.write(b); fos.close(); } public void loadFile() throws Exception { fd_load.setVisible(true); String setFile = fd_load.getDirectory() + fd_load.getFile(); FileInputStream fis = new FileInputStream(setFile); byte b[] = new byte[1024]; int n; while((n = fis.read(b)) != -1) { String s = new String(b, 0, n); ta.append(s); } } public void help() { JOptionPane.showMessageDialog(null, "@huangju", "JAVA--记事本", 2); }}
0 0
- 简单的Java记事本
- java开发简单记事本
- Java简单记事本实现
- java简单记事本
- java简单的记事本
- JAVA--简单记事本
- Java简单记事本
- [Java] BufferedReader/BufferedWriter 简单记事本
- java编写一个简单记事本
- JAVA之记事本开发简单Java程序
- 大学JAVA实验四 简单计算器、记事本
- java实现的简单记事本程序
- 一个简单的java记事本程序
- 基于JAVA的简单界面设计:记事本
- 基于JAVA的简单界面设计:记事本
- 简单记事本程序java源码项目
- Java的Swing实现一个简单记事本
- 用java实现简单记事本的编写
- 往fragment传值参考代码
- iOS开发 给Label加下划线、中划线
- FatMouse'trade
- SnmpSharpNet OctetString DateAndTime自定义解析
- 天池_阿里音乐流行趋势预测大赛(1) —— 赛题分析
- JAVA--简单记事本
- js点击图片放大效果ImageZoom代码下载
- 定制并发类(五)在一个Executor对象中使用我们的ThreadFactory
- 百度地图简单定位
- 关于android Sdk23不兼容HttpEntity解决
- [leetcode-341]Flatten Nested List Iterator(java)
- C#面试问答总结1
- 笔记一:关于在Windows环境下使用cmd向mysql数据库中导入.sql数据表文件
- (二) P - P Save Luke 【数学】