用java编程实现记事本_javase实现记事本

本文展示了如何使用Java编程实现一个基本的记事本程序,包括新建、打开、保存、退出、剪切、复制、粘贴等功能。通过创建自定义窗口类EditorFrame,结合JMenuItem、JButton等组件,实现文本编辑器的界面和操作。
摘要由CSDN通过智能技术生成

java编程就是分布式、微服务?离开Spring...我还能写点什么

不知从何时起,自己喜欢上也习惯了用java写点界面程序、app。也许这就是程序员仅剩的一点乐趣。但对我而言。我却很享受这个过程。程序猿一枚,热爱着编程。闲暇时光,一杯咖啡,一首轻音乐,打开笔记本用一行行代码实现自己心中的想法,实属快事。

效果

2944d33d9ed4985b32f49557c10774a8.png

代码

package example;

/**

* ┏┓   ┏┓

*┏┛┻━━━ ┻┓

*┃       ┃

*┃   ━   ┃

*┃ ┳┛ ┗┳ ┃

*┃       ┃

*┃   ┻   ┃

*┃       ┃

*┗━┓   ┏━┛

*  ┃   ┃神兽保佑

*  ┃   ┃代码无BUG!

*  ┃   ┗━━━┓

*  ┃       ┣┓

*  ┃       ┏┛

*  ┗┓┓┏━┳┓┏┛

*   ┃┫┫ ┃┫┫

*   ┗┻┛ ┗┻┛

*

*!!!!!!!!!!!!!!!!!!Get busy living or get busy dying!!!!!!!!!!!!!

*/

/**

* 记事本启动类

*

* @author www.javayihao.top

* @Time 2019

*/

public class App {

public static void main(String[] args) {

// 启动自定义窗口对象

EditorFrame editor = new EditorFrame();

}

}

package example;

import java.awt.BorderLayout;

import java.awt.Container;

import java.awt.Toolkit;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.InputStream;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JFileChooser;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

import javax.swing.JOptionPane;

import javax.swing.JScrollPane;

import javax.swing.JTextPane;

import javax.swing.JToolBar;

import javax.swing.text.DefaultStyledDocument;

/**

* java实现记事本程序主程序

*

* @author Administrator

*

*/

/*

* 自定义窗口类EditorFrame继承JFrame实现动作监听接口ActionListener

*/

public class EditorFrame extends JFrame implements ActionListener {

// 定义相关属性

private JMenuBar menuBar;// 頂部菜单栏

private JMenu menuFile, menuEdit, menuAbout;// 菜单文件、编辑、关于

// 菜单项新建、 打开、保存、退出、剪切、复制、粘贴、关于

private JMenuItem itemNewFile, itemOpen, itemSave, itemExit, itemCut, itemCopy, itemPaste, itemAbout;

private JToolBar toolBar;// 工具条

// 按钮新建、 打开、保存、退出、剪切、复制、粘贴、关于

private JButton butNewFile, butOpen, butSave, butExit, butCut, butCopy, butPaste, butAbout;

private JTextPane textPane;// 编辑窗口

private JLabel label;// 底部标签栏

private JFileChooser fileChooser;// 文件选择器

private JScrollPane scrollPane;

/*

* 构造方法

*/

public EditorFrame() {

// 实例化菜单栏

menuBar = new JMenuBar();

// 实例化菜单

menuFile = new JMenu("文件");

menuEdit = new JMenu("编辑");

menuAbout = new JMenu("关于");

// 实例化菜单项并添加事件监听

itemNewFile = new JMenuItem("新建");

itemNewFile.addActionListener(this);// 设置监听

itemNewFile.setActionCommand("newFile");

itemOpen = new JMenuItem("打开");

itemOpen.addActionListener(this);// 设置监听

itemOpen.setActionCommand("open");

itemSave = new JMenuItem("保存");

itemSave.addActionListener(this);// 设置监听

itemSave.setActionCommand("save");

itemExit = new JMenuItem("退出");

itemExit.addActionListener(this);// 设置监听

itemExit.setActionCommand("exit");

itemCut = new JMenuItem("剪切");

itemCut.addActionListener(this);// 设置监听

itemCut.setActionCommand("cut");

itemCopy = new JMenuItem("复制");

itemCopy.addActionListener(this);// 设置监听

itemCopy.setActionCommand("copy");

itemPaste = new JMenuItem("粘贴");

itemPaste.addActionListener(this);// 设置监听

itemPaste.setActionCommand("paste");

itemAbout = new JMenuItem("关于");

itemAbout.addActionListener(this);// 设置监听

itemAbout.setActionCommand("about");

// 文件设置菜单项

menuFile.add(itemNewFile);

menuFile.add(itemOpen);

menuFile.add(itemExit);

menuFile.add(itemSave);

// 编辑设置菜单项

menuEdit.add(itemCut);

menuEdit.add(itemCopy);

menuEdit.add(itemPaste);

// 关于设置菜单

menuAbout.add(itemAbout);

// 菜单栏设置菜单

menuBar.add(menuFile);

menuBar.add(menuEdit);

menuBar.add(menuAbout);

this.setJMenuBar(menuBar);

// 实例化工具条

toolBar = new JToolBar();

// 实例化按钮

butNewFile = new JButton("新建");

butNewFile.addActionListener(this);

butNewFile.setActionCommand("newFile");

butOpen = new JButton("打开");

butOpen.addActionListener(this);// 设置监听

butOpen.setActionCommand("open");

butSave = new JButton("保存");

butSave.addActionListener(this);// 设置监听

butSave.setActionCommand("save");

butExit = new JButton("退出");

butExit.addActionListener(this);// 设置监听

butExit.setActionCommand("exit");

butCut = new JButton("剪切");

butCut.addActionListener(this);// 设置监听

butCut.setActionCommand("cut");

butCopy = new JButton("复制");

butCopy.addActionListener(this);// 设置监听

butCopy.setActionCommand("copy");

butPaste = new JButton("粘贴");

butPaste.addActionListener(this);// 设置监听

butPaste.setActionCommand("paste");

butAbout = new JButton("关于");

butAbout.addActionListener(this);// 设置监听

butAbout.setActionCommand("about");

// 工具条设置按钮

toolBar.add(butNewFile);

toolBar.add(butOpen);

toolBar.add(butSave);

toolBar.add(butExit);

toolBar.add(butCut);

toolBar.add(butCopy);

toolBar.add(butPaste);

toolBar.add(butAbout);

// 实例化编辑窗口

textPane = new JTextPane();

label = new JLabel("www.javayihao.top ----by xiaoyuan");

// 实例化文件选择器

fileChooser = new JFileChooser();

// 实例化滚动条

scrollPane = new JScrollPane(textPane);

// 窗口容器中添加組件(使用边界布局)

Container container = getContentPane(); // 得到容器

container.add(toolBar, BorderLayout.NORTH); // 增加工具栏

container.add(scrollPane, BorderLayout.CENTER);

container.add(label, BorderLayout.SOUTH); // 增加状态栏

// 初始化窗口

this.setTitle("小猿记事本");// 窗口标题

this.setSize(600, 300);// 窗体大小

this.setIconImage((new ImageIcon("images/logo.png")).getImage());// 设置图标

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置可关闭进程

int width = Toolkit.getDefaultToolkit().getScreenSize().width;// 获得屏幕宽度

int height = Toolkit.getDefaultToolkit().getScreenSize().height;// 获得屏幕高度

this.setLocation((width - 500) / 2, (height - 400) / 2);// 剧中显示

this.setVisible(true);// 设置窗体可见

this.setResizable(true);// 可改变窗体大小

}

/*

* 事件方法

*/

@Override

public void actionPerformed(ActionEvent e) {

if (e.getActionCommand().equals("newFile")) {

textPane.setDocument(new DefaultStyledDocument());// 清空文档

} else if (e.getActionCommand().equals("open")) {

int i = fileChooser.showOpenDialog(EditorFrame.this); // 显示打开文件对话框

if (i == JFileChooser.APPROVE_OPTION) { // 点击对话框中打开选项

File f = fileChooser.getSelectedFile(); // 得到选择的文件

try {

InputStream is = new FileInputStream(f); // 得到文件输入流

textPane.read(is, "d"); // 读入文件到文本窗格

} catch (Exception ex) {

ex.printStackTrace(); // 输出出错信息

}

}

} else if (e.getActionCommand().equals("save")) {

int i = fileChooser.showSaveDialog(EditorFrame.this); // 显示保存文件对话框

if (i == JFileChooser.APPROVE_OPTION) { // 点击对话框中保存按钮

File f = fileChooser.getSelectedFile(); // 得到选择的文件

try {

FileOutputStream out = new FileOutputStream(f); // 得到文件输出流

out.write(textPane.getText().getBytes()); // 写出文件

} catch (Exception ex) {

ex.printStackTrace(); // 输出出错信息

}

}

} else if (e.getActionCommand().equals("exit")) {

System.exit(0);

} else if (e.getActionCommand().equals("cut")) {

textPane.cut();// 調用文本剪切方法

} else if (e.getActionCommand().equals("copy")) {

textPane.copy();// 調用复制方法

} else if (e.getActionCommand().equals("paste")) {

textPane.paste();// 调用粘贴方法

} else if (e.getActionCommand().equals("about")) {

JOptionPane.showMessageDialog(EditorFrame.this, "www.javayihao.top---简单的文本编辑器演示");

}

}

}

嗯,差不多了,一个多小时了,写着玩玩挺舒服的,代码不足之处就是重复代码过多,本来打算自定一个事件数组,使用java8中的Stream流特性给组件以此添加事件,为了看着清楚就这样喽....

更多java实战项目 文章 关注微信公众号 java一号

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值