java记事本1

java记事本小白

源码:

主函数

package java实践;

import java.awt.Color;

import java.awt.Desktop;

import java.awt.EventQueue;

import java.awt.Font;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.border.EmptyBorder;

import javax.swing.event.CaretEvent;

import javax.swing.event.CaretListener;

import javax.swing.text.BadLocationException;

import javax.swing.undo.UndoManager;

import javax.swing.JToolBar;

import javax.swing.KeyStroke;

import javax.swing.JMenuBar;

import javax.swing.JMenu;

import javax.swing.JButton;

import javax.swing.ImageIcon;

import javax.swing.JMenuItem;

import javax.swing.JOptionPane;

import java.awt.Toolkit;

import javax.swing.JFileChooser;

import java.awt.event.ActionListener;

import java.awt.event.KeyEvent;

import java.io.IOException;

import java.net.URI;

import java.net.URISyntaxException;

import java.awt.event.ActionEvent;

import javax.swing.JTextArea;

import java.awt.Dialog.ModalExclusionType;

import java.awt.BorderLayout;

import javax.swing.JLabel;

import javax.swing.SwingConstants;

import javax.swing.JCheckBoxMenuItem;

public class Notebook extends JFrame{

/**

*

*/

private static final long serialVersionUID = 1L;

private static int linenum =1;

private static int columnnum=1;

private JPanel contentPane;

private JTextArea textArea_1;

private UndoManager undoManager=new UndoManager();

private JToolBar toolBar_1;

/**

* Launch the application.

*/

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {

try {

Notebook frame = new Notebook();

frame.setVisible(true);

} catch (Exception e) {

e.printStackTrace();

}

}

});

}

/**

* Create the frame.

*/

public Notebook() {

setModalExclusionType(ModalExclusionType.TOOLKIT_EXCLUDE);

textArea_1 = new JTextArea();

textArea_1.requestFocusInWindow();

textArea_1.setFont(new Font("黑体",Font.ITALIC,25));

setIconImage(Toolkit.getDefaultToolkit().getImage("images//timg.jpg"));

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setBounds(400, 200, 1293, 729);

JMenuBar menuBar = new JMenuBar();

setJMenuBar(menuBar);

JMenu menu = new JMenu("文件");

menu.setActionCommand("menu");

menuBar.add(menu);

JMenuItem mntmCtrln = new JMenuItem("新建 ");

mntmCtrln.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

int n=JOptionPane.showConfirmDialog(null, "是否保存当前文件","提示",JOptionPane.YES_NO_OPTION);

if (n==JOptionPane.YES_OPTION) {

saveT();

}

}

});

mntmCtrln.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,ActionEvent.CTRL_MASK));

menu.add(mntmCtrln);

JMenuItem mntmCtrlo = new JMenuItem("打开 ");

mntmCtrlo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,ActionEvent.CTRL_MASK));

mntmCtrlo.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent arg0) {

// TODO 自动生成的方法存根

openT();

}

});

menu.add(mntmCtrlo);

JMenuItem mntmCtrls = new JMenuItem("保存 ");

mntmCtrls.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,ActionEvent.CTRL_MASK));

mntmCtrls.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

saveT();

}

});

menu.add(mntmCtrls);

JMenuItem menuItem = new JMenuItem("另村为");

menuItem.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

saveT();

}

});

menu.add(menuItem);

JMenuItem menuItem_1 = new JMenuItem("页面设置");

menuItem_1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

JOptionPane jOptionPane=new JOptionPane();

jOptionPane.showMessageDialog(null, "该功能尚未开发");

}

});

menu.add(menuItem_1);

JMenuItem menuItem_2 = new JMenuItem("打印");

menuItem_2.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

JOptionPane jOptionPane=new JOptionPane();

jOptionPane.showMessageDialog(null, "该功能尚未开发");

}

});

menuItem_2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,ActionEvent.CTRL_MASK));

menu.add(menuItem_2);

JMenuItem menuItem_3 = new JMenuItem("退出");

menuItem_3.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

int n=JOptionPane.showConfirmDialog(null, "是否保存","提示",JOptionPane.YES_NO_OPTION);

if (n==JOptionPane.YES_OPTION) {

saveT();

System.exit(0);

}

}

});

menu.add(menuItem_3);

JMenu menu_1 = new JMenu("编辑");

menuBar.add(menu_1);

//撤销

textArea_1.setWrapStyleWord(true);

textArea_1.getDocument().addUndoableEditListener(undoManager);

JMenuItem mntmCtrlz = new JMenuItem("撤销 ");

mntmCtrlz.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

if (undoManager.canUndo()) {

undoManager.undo();

}

}

});

mntmCtrlz.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,ActionEvent.CTRL_MASK));

menu_1.add(mntmCtrlz);

JMenuItem mntmCtrlx = new JMenuItem("剪切 ");

mntmCtrlx.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

textArea_1.cut();

}

});

mntmCtrlx.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,ActionEvent.CTRL_MASK));

menu_1.add(mntmCtrlx);

JMenuItem mntmCtrlc = new JMenuItem("复制 ");

mntmCtrlc.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

textArea_1.copy();

}

});

mntmCtrlc.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,ActionEvent.CTRL_MASK));

menu_1.add(mntmCtrlc);

JMenuItem mntmCtrlv = new JMenuItem("粘贴 ");

mntmCtrlv.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

textArea_1.paste();

}

});

mntmCtrlv.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,ActionEvent.CTRL_MASK));

menu_1.add(mntmCtrlv);

JMenuItem mntmDel = new JMenuItem("删除");

mntmDel.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

textArea_1.setText("");

}

});

mntmDel.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE,0));

menu_1.add(mntmDel);

JMenuItem mntmCtrlf = new JMenuItem("查找 ");

mntmCtrlf.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

new Replace(textArea_1);

}

});

mntmCtrlf.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F,ActionEvent.CTRL_MASK));

menu_1.add(mntmCtrlf);

JMenuItem mntmF = new JMenuItem("查找下一个");

mntmF.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

new Replace(textArea_1);

}

});

mntmF.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F3,0));

menu_1.add(mntmF);

JMenuItem mntmCtrlh = new JMenuItem("替换 ");

mntmCtrlh.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

new Replace(textArea_1);

}

});

mntmCtrlh.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H,ActionEvent.CTRL_MASK));

menu_1.add(mntmCtrlh);

JMenuItem mntmCtrlg = new JMenuItem("转到 ");

mntmCtrlg.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

new Goto(textArea_1);

}

});

mntmCtrlg.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G,ActionEvent.CTRL_MASK));

menu_1.add(mntmCtrlg);

JMenuItem mntmCtrla = new JMenuItem("全选 ");

mntmCtrla.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

textArea_1.selectAll();

textArea_1.setSelectedTextColor(Color.blue);

}

});

mntmCtrla.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,ActionEvent.CTRL_MASK));

menu_1.add(mntmCtrla);

JMenuItem mntmF_1 = new JMenuItem("日期/时间 ");

mntmF_1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F5,0));

mntmF_1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

new Date_time(textArea_1);

}

});

menu_1.add(mntmF_1);

JMenu menu_2 = new JMenu("格式");

menuBar.add(menu_2);

JMenuItem menuItem_5 = new JMenuItem("字体");

menuItem_5.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

Window_Font ziti=new Window_Font(textArea_1);

ziti.setVisible(true);

}

});

JCheckBoxMenuItem checkBoxMenuItem_1 = new JCheckBoxMenuItem("自动换行");

checkBoxMenuItem_1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if (checkBoxMenuItem_1.isSelected()) {

textArea_1.setLineWrap(true);

}else {

textArea_1.setLineWrap(false);

}

}

});

menu_2.add(checkBoxMenuItem_1);

menu_2.add(menuItem_5);

JMenu menu_3 = new JMenu("查看");

menu_3.setHorizontalAlignment(SwingConstants.TRAILING);

menuBar.add(menu_3);

JLabel jLabel=new JLabel("    第 " + linenum + " 行, 第 " + columnnum+" 列  ");

toolBar_1 = new JToolBar();

toolBar_1.add(jLabel);

toolBar_1.setVisible(false);

textArea_1.addCaretListener(new CaretListener() {

@Override

public void caretUpdate(CaretEvent e) {

// TODO 自动生成的方法存根

JTextArea editArea = (JTextArea)e.getSource();

            try {

int caretpos = textArea_1.getCaretPosition();

linenum = editArea.getLineOfOffset(caretpos);

columnnum = caretpos - textArea_1.getLineStartOffset(linenum);

            linenum += 1;

            jLabel.setText("    第 " + linenum + " 行, 第 " + (columnnum+1)+" 列  ");

} catch (BadLocationException e1) {

// TODO 自动生成的 catch 块

e1.printStackTrace();

}

}

});

JCheckBoxMenuItem checkBoxMenuItem = new JCheckBoxMenuItem("状态栏");

checkBoxMenuItem.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if (checkBoxMenuItem.isSelected()) {

toolBar_1.setVisible(true);

}else {

toolBar_1.setVisible(false);

}

}

});

menu_3.add(checkBoxMenuItem);

JMenu menu_4 = new JMenu("帮助");

menuBar.add(menu_4);

JMenuItem mntmh = new JMenuItem("查看帮助(H)");

mntmh.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

Desktop desktop = Desktop.getDesktop();

try {

desktop.browse(new URI("https://cn.bing.com/search?q"

+ "=%E8%8E%B7%E5%8F%96%E6%9C%89%E5%85%B3+windows+10+%E4%B8%AD"

+ "%E7%9A%84%E8%AE%B0%E4%BA%8B%E6%9C%AC%E7%9A%84%E5%B8%AE%E5%"

+ "8A%A9&filters=guid:%224466414-zh-hans-dia%22%20lang:%22zh-h"

+ "ans%22&form=T00032&ocid=HelpPane-BingIA"));

} catch (IOException | URISyntaxException e1) {

// TODO 自动生成的 catch 块

e1.printStackTrace();

}

}

});

menu_4.add(mntmh);

JMenuItem mntma = new JMenuItem("关于记事本(A)");

mntma.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

// AboutNote aboutNote=new AboutNote();

// aboutNote.setVisible(true);

// new Turtle();

new ShowPicture();

}

});

menu_4.add(mntma);

contentPane = new JPanel();

contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

setContentPane(contentPane);

JToolBar toolBar = new JToolBar();

toolBar.setForeground(Color.WHITE);

JButton button = new JButton("");

button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

int n=JOptionPane.showConfirmDialog(null, "是否保存当前文件","提示",JOptionPane.YES_NO_OPTION);

if (n==JOptionPane.YES_OPTION) {

saveT();

}

}

});

button.setIcon(new ImageIcon("images\\8.png"));

toolBar.add(button);

JButton button_1 = new JButton("");

button_1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

openT();

}

});

button_1.setIcon(new ImageIcon("images\\9.png"));

toolBar.add(button_1);

JButton button_2 = new JButton("");

button_2.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

saveT();

}

});

button_2.setIcon(new ImageIcon("images\\10.png"));

toolBar.add(button_2);

JButton button_3 = new JButton("");

button_3.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

textArea_1.copy();

}

});

button_3.setIcon(new ImageIcon("images\\11.png"));

toolBar.add(button_3);

JButton button_4 = new JButton("");

button_4.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

textArea_1.cut();

}

});

button_4.setIcon(new ImageIcon("images\\12.png"));

toolBar.add(button_4);

JButton button_5 = new JButton("");

button_5.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

textArea_1.paste();

}

});

button_5.setIcon(new ImageIcon("images\\13.png"));

toolBar.add(button_5);

contentPane.setLayout(new BorderLayout(0, 0));

JScrollPane jScrollPane=new JScrollPane(textArea_1);

contentPane.add(jScrollPane);

contentPane.add(toolBar, BorderLayout.NORTH);

contentPane.add(toolBar_1, BorderLayout.SOUTH);

}

//保存

public void saveT() {

new Writefile(this,textArea_1);

}

//打开

public void openT() {

new Openfile(this,textArea_1);

}

}

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 211,376评论 6 491
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,126评论 2 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 156,966评论 0 347
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,432评论 1 283
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,519评论 6 385
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,792评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,933评论 3 406
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,701评论 0 266
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,143评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,488评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,626评论 1 340
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,292评论 4 329
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,896评论 3 313
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,742评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,977评论 1 265
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,324评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,494评论 2 348

推荐阅读更多精彩内容