java記事本1

java記事本小白

源碼:

主函數(shù)

package java實(shí)踐;

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, "是否保存當(dāng)前文件","提示",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 自動(dòng)生成的方法存根

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("頁(yè)面設(shè)置");

menuItem_1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

JOptionPane jOptionPane=new JOptionPane();

jOptionPane.showMessageDialog(null, "該功能尚未開發(fā)");

}

});

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, "該功能尚未開發(fā)");

}

});

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("復(fù)制 ");

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("查找下一個(gè)");

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("轉(zhuǎn)到 ");

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("日期/時(shí)間 ");

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("自動(dòng)換行");

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 自動(dòng)生成的方法存根

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 自動(dòng)生成的 catch 塊

e1.printStackTrace();

}

}

});

JCheckBoxMenuItem checkBoxMenuItem = new JCheckBoxMenuItem("狀態(tài)欄");

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 自動(dòng)生成的 catch 塊

e1.printStackTrace();

}

}

});

menu_4.add(mntmh);

JMenuItem mntma = new JMenuItem("關(guān)于記事本(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, "是否保存當(dāng)前文件","提示",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);

}

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市借卧,隨后出現(xiàn)的幾起案子酷誓,更是在濱河造成了極大的恐慌,老刑警劉巖撼唾,帶你破解...
    沈念sama閱讀 218,036評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異哥蔚,居然都是意外死亡倒谷,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,046評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門糙箍,熙熙樓的掌柜王于貴愁眉苦臉地迎上來渤愁,“玉大人,你說我怎么就攤上這事深夯《陡瘢” “怎么了?”我有些...
    開封第一講書人閱讀 164,411評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵咕晋,是天一觀的道長(zhǎng)雹拄。 經(jīng)常有香客問我,道長(zhǎng)掌呜,這世上最難降的妖魔是什么滓玖? 我笑而不...
    開封第一講書人閱讀 58,622評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮质蕉,結(jié)果婚禮上势篡,老公的妹妹穿的比我還像新娘。我一直安慰自己模暗,他們只是感情好禁悠,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,661評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著兑宇,像睡著了一般碍侦。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上顾孽,一...
    開封第一講書人閱讀 51,521評(píng)論 1 304
  • 那天祝钢,我揣著相機(jī)與錄音,去河邊找鬼若厚。 笑死拦英,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的测秸。 我是一名探鬼主播疤估,決...
    沈念sama閱讀 40,288評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼灾常,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了铃拇?” 一聲冷哼從身側(cè)響起钞瀑,我...
    開封第一講書人閱讀 39,200評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎慷荔,沒想到半個(gè)月后雕什,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,644評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡显晶,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,837評(píng)論 3 336
  • 正文 我和宋清朗相戀三年贷岸,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片磷雇。...
    茶點(diǎn)故事閱讀 39,953評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡偿警,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出唯笙,到底是詐尸還是另有隱情螟蒸,我是刑警寧澤,帶...
    沈念sama閱讀 35,673評(píng)論 5 346
  • 正文 年R本政府宣布崩掘,位于F島的核電站七嫌,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏苞慢。R本人自食惡果不足惜抄瑟,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,281評(píng)論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望枉疼。 院中可真熱鬧,春花似錦鞋拟、人聲如沸骂维。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,889評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)航闺。三九已至,卻和暖如春猴誊,著一層夾襖步出監(jiān)牢的瞬間潦刃,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,011評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工懈叹, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留乖杠,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,119評(píng)論 3 370
  • 正文 我出身青樓澄成,卻偏偏與公主長(zhǎng)得像胧洒,于是被迫代替她去往敵國(guó)和親畏吓。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,901評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容