快来看(java编写记事本程序源码是多少)java编写记事本代码,java写个记事本,java记事本,
前言
java编程就是分布式、微服务?离开Spring...我还能写点什么
不知从何时起,自己喜欢上也习惯了用java写点界面程序、app。也许这就是程序员仅剩的一点乐趣。但对我而言。我却很享受这个过程。程序猿一枚,热爱着编程。闲暇时光,一杯咖啡,一首轻音乐,打开笔记本用一行行代码实现自己心中的想法,实属快事。
java写个记事本
先上效果吧
1
拖动工具条
代码如下:
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);// 可改变窗体大小}/** 事件方法*/@Overridepublic 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---简单的文本编辑器演示");}}}版权申明
本文系作者 @河马 原创发布在河马博客站点。未经许可,禁止转载。
暂无评论数据