JAVA记事本源代码(雏形)

本文介绍了一个简易记事本应用程序的开发过程,包括文本读取、保存文件等功能的实现。探讨了如何通过Java Swing创建GUI界面,并解决了文本面板刷新及文件操作等问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

//目前实现了可以将文本内容读入到文本区并显示的功能
//2007-10-11实现了保存文件的功能
//问题一:需要解决当改变框架大小时,PANLE及时刷新的问题.
//问题二:没有实现当打开文件时,记事本的标题改变为文件名称的功能.
package Notepad_new;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.io.*;

public class NotePad {
    
public static void main(String args[]) {

        JFrame frame 
= new NoteFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(
true);
    }

}


class NoteFrame extends JFrame {

    
public NoteFrame() {// 在frame中不指定框架大小,因为是用NotePanel对象填充的.
        this.setLocation(200200);
        setTitle(title);
        Container c 
= getContentPane();
        p 
= new TextPanel();
        area 
= p.area;
        JScrollPane scroll 
= new JScrollPane(area);
        p.add(scroll);
// 当文本内容超过文本区显示范围时,为文本区增加滚动条.
        add(p);

        JMenuBar menubar 
= new JMenuBar();
        
this.setJMenuBar(menubar);
        
// 增加"文件"菜单
        JMenu fileMenu = new JMenu("文件");
        JMenuItem openItem 
= new JMenuItem("打开");
        openItem.addActionListener(
new ActionListener() {
            
public void actionPerformed(ActionEvent e) {
                JFileChooser chooser 
= new JFileChooser();
                chooser.setCurrentDirectory(
new File("."));
                chooser.showOpenDialog(NoteFrame.
this);
                filePath 
= chooser.getSelectedFile().getPath();

                
try {
                    File f 
= new File(filePath);
                    BufferedInputStream in 
= new BufferedInputStream(
                            
new FileInputStream(f));
                    
int n = in.available();
                    
byte[] b = new byte[n];
                    in.read(b, 
0, n);
                    
// in.close();
                    fileText = new String(b);
                    area.setText(fileText);

                }
 catch (IOException e1) {
                    System.out.println(e.toString());
                }

            }

        }
);
        fileMenu.add(openItem);
        
// 添加保存功能
        JMenuItem saveItem = new JMenuItem("保存");
        saveItem.addActionListener(
new ActionListener() {// 处理保存事件
                    public void actionPerformed(ActionEvent e) {
                        JFileChooser chooser 
= new JFileChooser();// 创建一个jFileChooser的实例
                        chooser.setCurrentDirectory(new File("."));// 设定默认目录为当前目录
                        chooser.showSaveDialog(NoteFrame.this);// 显示保存文件对话框
                        String filePath = chooser.getSelectedFile().getPath();// 获取输入的文件名
                        try {
                            FileWriter out 
= new FileWriter(new File(filePath));
                            System.out.println(area.getText());
                            out.write(area.getText());
                            out.flush();
                        }
 catch (IOException e1) {
                            System.out.println(e1.toString());
                        }

                    }

                }
);
        fileMenu.add(saveItem);
        
// 增加退出功能
        JMenuItem exitItem = new JMenuItem("退出");
        exitItem.addActionListener(
new ActionListener() {
            
public void actionPerformed(ActionEvent e) {
                System.exit(
0);
            }

        }
);
        fileMenu.add(exitItem);

        
// 增加"帮助"菜单
        JMenu helpMenu = new JMenu("帮助");
        JMenuItem aboutItem 
= new JMenuItem("关于");
        aboutItem.addActionListener(
new ActionListener() {
            
public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(
null"小马菜菜倾情制作 2007-10-11");
            }

        }
);
        helpMenu.add(aboutItem);

        menubar.add(fileMenu);
// 将文件菜单添加到菜单栏
        menubar.add(helpMenu);// 将帮助菜单添加到菜单栏
        pack();// 使用该语句,使panel的大小与frame相适应
        area.setText(fileText);
    }


    
public static String fileText, filePath;

    
public static JTextArea area;

    
public static String title;

    
public static TextPanel p;
}


class TextPanel extends JPanel {
    
public TextPanel() {
        setSize(
600800);
        area 
= new JTextArea(2040);
        BorderLayout layout 
= new BorderLayout();
        
this.setLayout(layout);
        add(area, layout.CENTER);
    }


    
public static JTextArea area;

    
public static String title;
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

低音钢琴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值