java界面 文件选择器_java写个记事本

前言

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


效果

442a7de082646585fe1b41fa6a3ef752.png

a8a63f8bcb2d1ff3ebae8d1f16f2e71f.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---简单的文本编辑器演示");
		}
	}

}

58c75e6d87222e1ae8417ef32bf705b0.png

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值