java Swing 实现记事本
来源:互联网 发布:淘宝天猫客服注意事项 编辑:程序博客网 时间:2024/05/29 11:40
/* Copyright (C) 2017 mzy <1344874077@qq.com> This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.*/package notepad;import java.awt.Color;/** * * @author 半步疯子 * @version 1.0 */import java.awt.Container;import java.awt.Dimension;import java.awt.Font;import java.awt.GraphicsEnvironment;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.io.BufferedReader;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.io.PrintStream;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JDialog;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JList;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JPopupMenu;import javax.swing.JScrollPane;import javax.swing.JSpinner;import javax.swing.JTextArea;import javax.swing.SpinnerModel;import javax.swing.SpinnerNumberModel;import javax.swing.filechooser.FileNameExtensionFilter;public class Notepad extends JFrame implements ActionListener{public static void main(String[] args) {new Notepad();}private static final long serialVersionUID = -2843137103983861960L;JTextArea text; // 必须使用JTextArea 不然第二次显示会出问题public Notepad(){notepadUI();setVisible(true); // setVisible 为true的时候是显示前面添加在JFrame上在组建,所以需要放在最后面}private void notepadUI() {// 设置基本属性 setTitle("MZY记事本");setSize(600, 400);setDefaultCloseOperation(EXIT_ON_CLOSE);setLocationRelativeTo(null);setIconImage(new ImageIcon("icon/notepad.png").getImage());// setIconImage(new ImageIcon(getClass().getResource("icon/notepad.png")).getImage());//图标放在源目录的icon文件夹Container cp = getContentPane();// 怎么将JMenuItem显示在container层上text = new JTextArea();text.setFont(new Font("新宋体",Font.PLAIN, 24));cp.add(new JScrollPane(text));String[] captions = {"文件(F)","编辑(E)","格式(O)","帮助(H)"};String[][] titles = {{"新建","打开...","保存","另存为...", "-", "退出"},{"复制","剪切","粘贴","全选"},{"字体(F)..."},{"帮助","-","关于记事本"}};// JMenuBar -> JMenuBar menuBar = new JMenuBar();JMenu menu;JMenuItem item;for(int i =0; i<captions.length; i++){menu = new JMenu(captions[i]);menu.setFont(new Font("黑体",Font.PLAIN, 20));menuBar.add(menu);for(int j=0; j<titles[i].length; j++ ){String caption = titles[i][j];if("-".equals(caption)){menu.addSeparator(); // 给menuItem加一个分隔符continue;}item = new JMenuItem(titles[i][j]);item.setPreferredSize(new Dimension(120, 35));item.setFont(new Font("黑体",Font.PLAIN, 18));// item.setBorder(BorderFactory.createLineBorder(Color.black,3));menu.add(item);item.addActionListener(this);}} this.setJMenuBar(menuBar); final JPopupMenu rightClick = new JPopupMenu(); // rightClick.setBackground(Color.GRAY); rightClick.setPreferredSize(new Dimension(100, 150)); JMenuItem cut = new JMenuItem("剪切"); JMenuItem copy = new JMenuItem("复制"); JMenuItem paste = new JMenuItem("粘贴"); JMenuItem selectAll = new JMenuItem("全选"); cut.setFont(new Font("黑体",Font.PLAIN, 18)); copy.setFont(new Font("黑体",Font.PLAIN, 18)); paste.setFont(new Font("黑体",Font.PLAIN, 18)); selectAll.setFont(new Font("黑体",Font.PLAIN, 18)); rightClick.add(cut).addActionListener(this); rightClick.add(copy).addActionListener(this); rightClick.add(paste).addActionListener(this); rightClick.addSeparator(); rightClick.add(selectAll).addActionListener(this); text.add(rightClick); text.addMouseListener(new MouseAdapter() { public void mouseReleased(MouseEvent e) { if (e.getButton() == MouseEvent.BUTTON3) { rightClick.show(text, e.getX(), e.getY()); } } }); }/** * about my mylistener */String pathOld;@SuppressWarnings("resource")public void actionPerformed(ActionEvent e) {String s = e.getActionCommand();if("新建".equals(s)) {text.setText("");pathOld = null;}if("退出".equals(s)) {this.dispose();}if("字体(F)...".equals(s)) {new MyFormat(text);}if("另存为...".equals(s)) {aboutSave();}if("保存".equals(s)) {PrintStream printStream = null;if(null != pathOld) {try { printStream = new PrintStream(pathOld); // printStream System.setOut(printStream); // 改变输出流的位置 System.out.println(text.getText()); // 将内容打印输出到目标文件上 } catch (FileNotFoundException e1) { e1.printStackTrace(); } finally { if(null != printStream) { printStream.close(); } }} else {aboutSave();}}if("打开...".equals(s)) { JFileChooser chooser = new JFileChooser(); // FileNameExtensionFilter filter = new FileNameExtensionFilter("文本文档(*.txt)", "txt"); // chooser.setFileFilter(filter); chooser.setDialogTitle("文件打开"); int choose = chooser.showOpenDialog(null); // chooser.setVisible(true); if(JFileChooser.APPROVE_OPTION == choose){ pathOld= chooser.getSelectedFile().getPath(); FileReader reader = null; try { reader = new FileReader(pathOld); BufferedReader bufferReader = new BufferedReader(reader); String temp = "", target = ""; while ((temp = bufferReader.readLine()) != null) { target += (temp + "\n"); } text.setText(target); } catch (FileNotFoundException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } finally { if(null != reader) { try { reader.close(); } catch (IOException e1) { e1.printStackTrace(); } } } } }// {"复制","剪切","粘贴","全选","反向选择"},if ("剪切".equals(s)) { text.cut(); }if ("复制".equals(s)) { text.copy(); }if ("粘贴".equals(s)) { text.paste(); } if ("全选".equals(s)) { text.selectAll(); }if("关于记事本".equals(s)) {new AboutMyNotepad();}if("帮助".equals(s)) {new AboutHelp();}}private void aboutSave() {JFileChooser chooser = new JFileChooser();FileNameExtensionFilter filter = new FileNameExtensionFilter("文本文档(*.txt)", "txt");chooser.setFileFilter(filter); chooser.setDialogTitle("另存为"); int choose = chooser.showSaveDialog(null); // chooser.setVisible(true); PrintStream printStream = null; // getSelectedFile返回选中的文件(得到一个File对象)再调用File的getPath方法 // getPath返回值为String类型的 抽象路径名 if(JFileChooser.APPROVE_OPTION == choose){ String select = chooser.getSelectedFile().getPath(); try {printStream = new PrintStream(select); // printStream System.setOut(printStream); // 改变输出流的位置 System.out.println(text.getText()); // 将内容打印输出到目标文件上} catch (FileNotFoundException e1) {e1.printStackTrace();} finally {if(null != printStream) {printStream.close();}} }}}class MyFormat extends JDialog implements ActionListener{private static final long serialVersionUID = 1L;public static int style = 0;public static int bounds = 16;public static Font font = new Font("黑体", style, bounds);JTextArea text;Container cp = getContentPane();// notrh// centerGraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();// 获取系统中可用的字体的名字String[] fontName = e.getAvailableFontFamilyNames();String[] fontStyle = {"常规", "倾斜", "粗体", "粗斜"};JList<String> fontList = new JList<String>(fontName);JList<String> StyleList = new JList<String>(fontStyle);// SpinnerNumberModel(Number value, Comparable minimum, Comparable maximum, Number stepSize) SpinnerModel spinnerModel = new SpinnerNumberModel(bounds, 0, 80, 2);JSpinner spinner = new JSpinner(spinnerModel); // 上下的小箭头// southJButton ok = new JButton("确定");JButton cancel = new JButton("取消");MyFormat(JTextArea text){this.text = text;setIconImage(new ImageIcon("icon/notepad.png").getImage());cp.setPreferredSize(new Dimension(500, 300)); cp.setLayout(null);JLabel l2 = new JLabel("字形");JLabel l1 = new JLabel("字体");JLabel l3 = new JLabel("大小");l1.setFont(new Font("华文行楷",Font.BOLD, 28));l2.setFont(new Font("华文行楷",Font.BOLD, 28));l3.setFont(new Font("华文行楷",Font.BOLD, 28));l1.setBounds(40, 20, 140, 35);l2.setBounds(215, 20, 120, 35);l3.setBounds(360, 20, 75, 35);cp.add(l1);cp.add(l2);cp.add(l3);StyleList.setFont(new Font("新宋体",0 , 18));JScrollPane j2 = new JScrollPane(StyleList);fontList.setFont(new Font("新宋体",0 , 12));JScrollPane j1 = new JScrollPane(fontList);spinner.setFont(new Font("新宋体",1 , 18));j1.setBounds(40, 75, 150, 175);j2.setBounds(215, 75, 80, 120);spinner.setBounds(360, 75, 45, 50);cp.add(j1);cp.add(j2);cp.add(spinner);ok.addActionListener(this);cancel.addActionListener(this);cp.add(ok);cp.add(cancel);ok.setBounds(245, 240, 80, 30);cancel.setBounds(380, 240, 80, 30);setTitle("字体");setVisible(true); setLocationRelativeTo(null); setResizable(false); // 设置为大小不可变的pack(); // 调整计算全部都可显示的大小 这一句必须放在最后不然没法显示}public void actionPerformed(ActionEvent e) {String s = e.getActionCommand();if("确定".equals(s)) {style = myGetType();bounds = Integer.parseInt(spinner.getValue().toString());font = new Font((String)fontList.getSelectedValue(), style, bounds);text.setFont(font);this.dispose();}if("取消".equals(s)) {dispose();}}private int myGetType() {if("常规".equals(StyleList.getSelectedValue())) {return 0;} else if("倾斜".equals(StyleList.getSelectedValue())) {return 1;} else if("粗体".equals(StyleList.getSelectedValue())) {return 2;} else if("粗斜".equals(StyleList.getSelectedValue())) {return 3;}return 0;}}class AboutHelp extends JDialog {// 关于窗口private static final long serialVersionUID = 1L;AboutHelp() {JTextArea t = new JTextArea();t.setFont(new Font("微软雅黑",1 , 16));String s = " 作者:半步疯子 "+ "实现功能均可点击 :"+ "右键点击也可操作"; t.setText(s); t.setEditable(false); add(t); setIconImage(new ImageIcon("icon/notepad.png").getImage()); setSize(480, 100); setVisible(true); setLocationRelativeTo(null); setResizable(false); // 设置为大小不可变的 }}class AboutMyNotepad extends JDialog {// 关于窗口private static final long serialVersionUID = 1L;AboutMyNotepad() {JTextArea t = new JTextArea();t.setFont(new Font("微软雅黑",1 , 16));String s = " 作者:半步疯子 "+ "版本:mzy1.0 "+ " 联系:1344874077@qq.com"; t.setText(s); t.setEditable(false); add(t); setIconImage(new ImageIcon("icon/notepad.png").getImage()); setSize(550, 100); setVisible(true); setLocationRelativeTo(null); setResizable(false); // 设置为大小不可变的 }}
阅读全文
0 0
- java Swing 实现记事本
- Java的Swing实现一个简单记事本
- Java Swing实现的简单记事本
- 使用JAVA swing实现简单的记事本
- Swing实现记事本
- Java swing 记事本代码
- java Swing记事本
- java swing 记事本
- 简单记事本的实现(swing技术)
- 用Java swing制作简单记事本
- 基于Java的Swing编写的记事本
- Java 实现记事本
- Java简单记事本实现
- Java实现记事本
- java实现记事本
- java实现记事本
- java实现记事本
- Java记事本实现
- 机房之登录窗体
- 超实用压力测试工具-ab工具
- 1003. Emergency (25)
- 学生成绩转换
- 生成RSA公钥和私钥
- java Swing 实现记事本
- 文章标题
- ubuntu IPython notebook
- 修改Java_Home无效,java版本没有更换的问题解决方法
- Python 练习实例7
- 【Java】文本合并程序
- 学生成绩录入
- Quartz-中断正在执行的任务
- 【猪脑子系列】关于字符串