java 记事本 新建_Java 生成一个记事本

这是一个Java实现的记事本程序,包括文本框、菜单栏、打开和保存功能。通过JFileChooser进行文件选择,使用FileReader和BufferedReader读取文件,FileWriter和BufferedWriter实现文件保存。
摘要由CSDN通过智能技术生成

6348cf74b4436e20d012fe699e1b0d64.png

实现打开与保存的记事本

1 packagecom.tanzhou.notepad.util;2

3 importjava.awt.Color;4 importjava.awt.event.ActionEvent;5 importjava.awt.event.ActionListener;6 importjava.io.BufferedReader;7 importjava.io.BufferedWriter;8 importjava.io.FileReader;9 importjava.io.FileWriter;10 importjava.io.IOException;11

12 importjavax.swing.JFileChooser;13 importjavax.swing.JFrame;14 importjavax.swing.JMenu;15 importjavax.swing.JMenuBar;16 importjavax.swing.JMenuItem;17 importjavax.swing.JTextArea;18

19 /**

20 * @className NotepadTest.java21 *22 * @Description:TODO(生成一个记事本)23 *24 * @date 2018.7.2325 *26 *@author银桑的洞爷湖27 *28 */

29 public class NotepadTest extends JFrame implements ActionListener{// 监听事件

30 // 1.定义一个文本框 全局变量,在哪都可以使用 new的话一开始就会占用空间;null的话,创造了但不会占用空间31 JTextArea jTextArea=null;32 // 2.定义一个菜单栏33 JMenuBar jMenuBar = null;34 // 3.定义一个菜单"文件"35 JMenu jMenu1 = null;36 // 4.定义 打开和37 JMenuItem jMenuItem1 = null;38 // 5.保存39 JMenuItem jMenuItem2 = null;40 // 6.定义一个文件选择41 JFileChooser jFileChooser = null;42 // 7.定义一个filereader 文件输入流:从中读取数据,打开时要用输入流读取43 FileReader fileReader = null;44 // 8.定义一个filewrite 文件输出流:保存 保存到某个文件中45 FileWriter fileWriter = null;46 // 9.定义一个缓冲字符输入流 缓冲区 可当成一个水池,每次给里面放一瓢水,等这个水池蓄满以后一次性取出来47 BufferedReader bufferedReader = null;48 // 10.定义一个缓冲字符输出流49 BufferedWriter bufferedWrider = null;50

51 /**

52 * @Description:TODO(构造函数) 方法名必须和类名相同53 *@param

54 *@return思路:55 生成一个记事本56 1.定义记事本的样式57 2.生成一个记事本58 3.实现记事本的功能 打开和保存59 */

60

61 publicNotepadTest(){62       // 1.实例化文本框 new63       jTextArea=newJTextArea();64       // 2.实例化菜单栏65       jMenuBar=newJMenuBar();66       // 3.实例化一个菜单"文件"67       jMenu1=new JMenu("文件");68       // 4.实例化 打开 按钮69       jMenuItem1=new JMenuItem("打开");70       // 绑定监听事件 监视者的意思71       jMenuItem1.addActionListener(this);// this表当前对象 jMenuItem172       jMenuItem1.setActionCommand("打开");// setActionCommand:给哪个设置监听事件

73       // 5.实例化 保存 按钮74       jMenuItem2 = new JMenuItem("保存");75       // 绑定监听事件 监视者的意思76       jMenuItem2.addActionListener(this);//this表当前对象

77       jMenuItem2.setActionCommand("保存");//setActionCommand:给哪个设置监听事件78       //6.设置文本框的背景颜色

79       jTextArea.setBackground(Color.WHITE);80   // 7.归为 定位

81       // 将菜单栏添加到窗体中82       this.setJMenuBar( jMenuBar );83       // 将菜单添加到菜单栏中84 jMenuBar.add(jMenu1);85 // 将“打开”和“保存”添加到“文件”菜单中86       jMenu1.add(jMenuItem1);87 jMenu1.add(jMenuItem2);88       // 将文本框添加到窗体中89       this.add(jTextArea);90       // 8.设置记事本标题91       this.setTitle("啊灵呀记事本");92       // 9.设置记事本大小93       this.setSize(800,600);94       // 10.当我们关闭窗口时 关闭进程95       this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);96       // 11.显示窗口97       this.setVisible(true);98 }99

100     //用流一般要抛异常

101

102 /**

103 * @Description:TODO(main函数 门)104 *@paramargs105 *@returnvoid106 */

107

108     public static voidmain(String[] args){109       NotepadTest notepadTest=newNotepadTest();110     }111

112     @Override113     public voidactionPerformed(ActionEvent e){114 // 实现 打开 和 保存 功能

//点 打开 按钮115 if(e.getActionCommand().equals("打开")){116       // 1.实例化jFileChooser 文件选择117       jFileChooser=newJFileChooser();118       // 2.定义一个标题119       jFileChooser.setDialogTitle("选择要打开的文件");120       // 3.设置一个默认的路径121       jFileChooser.showOpenDialog(null);122       // 4.当点击打开时,让文件选择框自动弹出123       jFileChooser.setVisible(true);124       //5.用address 保存用户编辑文件的绝对路径

125       String address=jFileChooser.getSelectedFile().getAbsolutePath();126

127       try{128         //6.实例化文件输入流

129         fileReader=newFileReader(address);130         //7.实例化缓冲字符输入流 缓冲区 暂时存放进去

131         bufferedReader =newBufferedReader(fileReader);132         //8.定义一个str判断输入的字符是否已经为空

133         String str="";134         //9.定义一个strAll接收文件的全部信息

135         String strAll="";136         //去缓冲区取数据并保存到strAll中 read()返回-1,读取完 readLine():一行行读

137         while((str=bufferedReader.readLine())!=null){138           strAll=str+"\r\n";139         }140         jTextArea.setText(strAll);141       }catch(IOException e1){142         e1.printStackTrace();143       }finally{// 可保证里面的代码一定会执行144         try{145           bufferedReader.close();146           fileReader.close();147         }catch(IOException e1){148           e1.printStackTrace();149         }150       }151     }

//点 保存 按钮

if(e.getActionCommand().equals("保存")){

JFileChooser jFileChooser1=new JFileChooser();        jFileChooser1.setDialogTitle("保存");

jFileChooser1.showSaveDialog(null);

好的,以下是一个简易的记事本GUI的Java代码示例: ```java import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; public class SimpleNotepad extends JFrame implements ActionListener { private JTextArea textArea = new JTextArea(); private JMenuBar menuBar = new JMenuBar(); private JMenu fileMenu = new JMenu("File"); private JMenuItem newMenuItem = new JMenuItem("New"); private JMenuItem openMenuItem = new JMenuItem("Open"); private JMenuItem saveMenuItem = new JMenuItem("Save"); private JMenuItem exitMenuItem = new JMenuItem("Exit"); public SimpleNotepad() { setTitle("Simple Notepad"); // Set the size of the window setSize(640, 480); // Set the location of the window setLocationRelativeTo(null); // Add the text area to the content pane getContentPane().add(textArea); // Add the menu bar to the window setJMenuBar(menuBar); // Add the file menu to the menu bar menuBar.add(fileMenu); // Add the menu items to the file menu fileMenu.add(newMenuItem); fileMenu.add(openMenuItem); fileMenu.add(saveMenuItem); fileMenu.addSeparator(); fileMenu.add(exitMenuItem); // Add the action listeners to the menu items newMenuItem.addActionListener(this); openMenuItem.addActionListener(this); saveMenuItem.addActionListener(this); exitMenuItem.addActionListener(this); // Show the window setVisible(true); } public void actionPerformed(ActionEvent e) { // If the user clicks the "New" menu item if (e.getSource() == newMenuItem) { // Clear the text area textArea.setText(""); } // If the user clicks the "Open" menu item else if (e.getSource() == openMenuItem) { // Create a file chooser dialog JFileChooser fileChooser = new JFileChooser(); // Show the dialog int result = fileChooser.showOpenDialog(this); // If the user clicks the "Open" button if (result == JFileChooser.APPROVE_OPTION) { // Get the selected file File file = fileChooser.getSelectedFile(); try { // Open the file and read its contents into the text area BufferedReader reader = new BufferedReader(new FileReader(file)); String line = null; while ((line = reader.readLine()) != null) { textArea.append(line + "\n"); } reader.close(); } catch (IOException ex) { ex.printStackTrace(); } } } // If the user clicks the "Save" menu item else if (e.getSource() == saveMenuItem) { // Create a file chooser dialog JFileChooser fileChooser = new JFileChooser(); // Show the dialog int result = fileChooser.showSaveDialog(this); // If the user clicks the "Save" button if (result == JFileChooser.APPROVE_OPTION) { // Get the selected file File file = fileChooser.getSelectedFile(); try { // Save the contents of the text area to the file BufferedWriter writer = new BufferedWriter(new FileWriter(file)); writer.write(textArea.getText()); writer.close(); } catch (IOException ex) { ex.printStackTrace(); } } } // If the user clicks the "Exit" menu item else if (e.getSource() == exitMenuItem) { // Exit the application System.exit(0); } } public static void main(String[] args) { new SimpleNotepad(); } } ``` 这个记事本GUI包括了菜单栏和一个文本编辑区域,并且可以实现新建、打开、保存和退出功能。你可以根据自己的需求进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值