Java仿Windows記事本原始碼
阿新 • • 發佈:2019-01-02
先上截圖:
原始碼:
import java.awt.*;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.undo.UndoManager;
import java.awt.event.*;
import java.io.*;
/**
*
* @author [email protected]
* @created 2015年7月6日 上午11:22:24
*/
public class JNotepad extends JFrame implements ActionListener {
JMenuBar menubar = new JMenuBar();
JMenu file = new JMenu("檔案(F)");
JMenu edit = new JMenu("編輯(E)");
JMenu format = new JMenu("格式(O)");
JMenu help = new JMenu("幫助(H)");
JMenuItem create = new JMenuItem("新建");
JMenuItem open = new JMenuItem("開啟..." );
JMenuItem save = new JMenuItem("儲存");
JMenuItem saveAs = new JMenuItem("另存為...");
JMenuItem exit = new JMenuItem("退出");
JMenuItem undo = new JMenuItem("撤銷");
JMenuItem cut = new JMenuItem("剪下");
JMenuItem copy = new JMenuItem("複製");
JMenuItem paste = new JMenuItem("貼上" );
JMenuItem findRep = new JMenuItem("查詢替換");
JMenuItem selectAll = new JMenuItem("全選");
JMenuItem font = new JMenuItem("字型");
JMenuItem about = new JMenuItem("關於");
JMenuItem cut2 = new JMenuItem("剪下(X)");
JMenuItem copy2 = new JMenuItem("複製(C)");
JMenuItem paste2 = new JMenuItem("貼上(V)");
JMenuItem selectAll2 = new JMenuItem("全選(A)");
public static JTextArea textarea = new JTextArea();
UndoManager um = new UndoManager();
JScrollPane scroll = new JScrollPane(textarea, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
JPopupMenu popup = new JPopupMenu();
String pathSelect;
// 獲取螢幕尺寸
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
public JNotepad() {
// 此處定義鍵盤快捷鍵
// MenuBar
file.setMnemonic(KeyEvent.VK_F);
edit.setMnemonic(KeyEvent.VK_E);
format.setMnemonic(KeyEvent.VK_O);
help.setMnemonic(KeyEvent.VK_H);
// MenuItem
create.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
undo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z, ActionEvent.CTRL_MASK));
cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK));
copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK));
paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK));
findRep.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, ActionEvent.CTRL_MASK));
selectAll.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.CTRL_MASK));
// 事件監聽者
save.addActionListener(this);
create.addActionListener(this);
open.addActionListener(this);
saveAs.addActionListener(this);
exit.addActionListener(this);
undo.addActionListener(this);
cut.addActionListener(this);
copy.addActionListener(this);
paste.addActionListener(this);
selectAll.addActionListener(this);
font.addActionListener(this);
about.addActionListener(this);
cut2.addActionListener(this);
copy2.addActionListener(this);
paste2.addActionListener(this);
selectAll2.addActionListener(this);
findRep.addActionListener(this);
// 設定撤銷文字的管理器
textarea.getDocument().addUndoableEditListener(um);
textarea.setFont(Format.font);
// 檔案
file.add(create);
file.add(open);
file.add(save);
file.add(saveAs);
file.addSeparator();
file.add(exit);
// 編輯
edit.add(undo);
edit.addSeparator();
edit.add(cut);
edit.add(copy);
edit.add(paste);
edit.addSeparator();
edit.add(findRep);
edit.addSeparator();
edit.add(selectAll);
// 格式
format.add(font);
// 幫助
help.add(about);
// 選單欄
menubar.add(file);
menubar.add(edit);
menubar.add(format);
menubar.add(help);
// 右鍵選單
popup.add(cut2);
popup.add(copy2);
popup.add(paste2);
popup.addSeparator();
popup.add(selectAll2);
// 新增到文字域容器
textarea.add(popup);
// 匿名內部類監聽器右鍵動作
textarea.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3) {
popup.show(textarea, e.getX(), e.getY());
}
}
});
// 邊界佈局
this.add(menubar, BorderLayout.NORTH);
this.add(scroll, BorderLayout.CENTER);
this.setTitle("記事本");
this.setSize(500, 400);
this.setLocationRelativeTo(null);
this.setIconImage(new ImageIcon(this.getClass().getResource("/icon/notepad.png")).getImage());//圖示放在源目錄的icon資料夾
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setVisible(true);
}
// 重寫actionPerformed
@Override
public void actionPerformed(ActionEvent e) {
// Event物件發生源
if (e.getSource() == open) {
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("文字文件(*.txt)", "txt");
chooser.setFileFilter(filter);
chooser.setDialogTitle("檔案開啟");
chooser.showOpenDialog(null);
chooser.setVisible(true);
try {
pathSelect = chooser.getSelectedFile().getPath();
FileReader wjl = new FileReader(pathSelect);
BufferedReader hcl = new BufferedReader(wjl);
String s = "", zfc = "";
while ((s = hcl.readLine()) != null) {
zfc += (s + "\n");
}
textarea.setText(zfc);
} catch (Exception e1) {
}
}
if (e.getSource() == saveAs) {// 另存為
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("文字文件(*.txt)", "txt");
chooser.setFileFilter(filter);
chooser.setDialogTitle("另存為");
chooser.showSaveDialog(null);
chooser.setVisible(true);
PrintStream ps;
try {
String select = chooser.getSelectedFile().getPath();
ps = new PrintStream(select);
System.setOut(ps);
System.out.println(this.textarea.getText());
} catch (Exception e1) {
}
}
if (e.getSource() == save && (pathSelect == null)) {// 儲存
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle("儲存");
chooser.showSaveDialog(null);
chooser.setVisible(true);
PrintStream ps;
try {
pathSelect = chooser.getSelectedFile().getPath();
ps = new PrintStream(pathSelect);
System.setOut(ps);
System.out.println(this.textarea.getText());
} catch (Exception e1) {
}
} else if (e.getSource() == save && !(pathSelect == null)) {
PrintStream ps;
try {
ps = new PrintStream(pathSelect);
System.setOut(ps);
System.out.println(this.textarea.getText());
} catch (FileNotFoundException e1) {
}
}
if (e.getSource() == create) {
textarea.setText("");
pathSelect = null;
}
if (e.getSource() == exit) {
System.exit(0);
}
if (e.getSource() == undo) {
if (um.canUndo()) {
um.undo();
}
}
if (e.getSource() == cut || e.getSource() == cut2) {
textarea.cut();
} else if (e.getSource() == copy || e.getSource() == copy2) {
textarea.copy();
} else if (e.getSource() == paste || e.getSource() == paste2) {
textarea.paste();
} else if (e.getSource() == findRep) {
new FindAndReplace(textarea);
}
else if (e.getSource() == selectAll || e.getSource() == selectAll2) {
textarea.selectAll();
}
if (e.getSource() == font) {
new Format(textarea);
}
if (e.getSource() == about) {
new About();
}
}
public static void main(String[] args) {
new JNotepad();
}
}
class FindAndReplace extends JDialog implements ActionListener {// 查詢和替換
JLabel findLabel = new JLabel("查詢內容:");
JLabel repLabel = new JLabel(" 替換為:");
JTextField findTf = new JTextField(8);
JTextField repTf = new JTextField(8);
JButton findBtn = new JButton("查詢");
JButton repBtn = new JButton("替換");
JPanel findPn = new JPanel();
JPanel repPn = new JPanel();
JTextArea textarea;
String text;
boolean flg = false;
int len;
int start = 0;
int k = 0;
public FindAndReplace(JTextArea textarea) {
this.textarea = textarea;
findPn.add(findLabel);
findPn.add(findTf);
findPn.add(findBtn);
repPn.add(repLabel);
repPn.add(repTf);
repPn.add(repBtn);
this.add(findPn);
this.add(repPn);
findBtn.addActionListener(this);
repBtn.addActionListener(this);
this.setTitle("查詢和替換");
this.setLayout(new GridLayout(2, 1));
// this.setBounds(400, 200, 300, 140);
this.pack();
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setVisible(true);
this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
}
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e) {
String findText = findTf.getText();
String repText = repTf.getText();
text = textarea.getText();
if (e.getSource() == findBtn) {
findBtn.setLabel("下一個");
if (findText != null) {
len = findText.length();
start = text.indexOf(findText, k);
k = start + len;
textarea.select(start, start + len);
flg = true;
if (start == -1) {
JOptionPane.showMessageDialog(null, "已到檔案尾部!", "提示", JOptionPane.INFORMATION_MESSAGE);
start = 0;
k = 0;
flg = false;
}
}
} else if (e.getSource() == repBtn) {
if (flg) {
textarea.replaceRange(repText, start, start + len);
flg = false;
}
}
}
}
// 字型格式
class Format extends JDialog implements ActionListener {
public static int style = 0; // 全域性變數型別,預設值為0
public static int size = 16; // 全域性變數字型大小,預設值為16
public static Font font = new Font("新宋體", style, size); // 全域性變數字型,預設值為新宋體
JPanel pn = new JPanel();
JPanel okCelPn = new JPanel();
JPanel fontPn = new JPanel();
JPanel ptPn = new JPanel();
JLabel fontLabel = new JLabel("字型: ");
JLabel fontStyleLabel = new JLabel(" 字形: ");
JLabel ptLabel = new JLabel(" 磅值: ");
JButton ok = new JButton("確定");
JButton cancel = new JButton("取消");
GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();// 獲取系統中可用的字型的名字
String[] fontName = e.getAvailableFontFamilyNames();// 獲取系統中可用的字型的名字
String[] fontType = { "常規", "傾斜", "粗體", "粗偏斜體" };
JList fontList = new JList(fontName);
JList fontTypeList = new JList(fontType);
JScrollPane fontScroll = new JScrollPane(fontList);
JScrollPane fontTypeScroll = new JScrollPane(fontTypeList);
JTextArea textarea;
SpinnerModel spinnerModel = new SpinnerNumberModel(size, // initial value
0, // min
100, // max
2 // Step
);
JSpinner spinner = new JSpinner(spinnerModel);
public Format(JTextArea textarea) {
this.textarea = textarea;
ok.addActionListener(this);
cancel.addActionListener(this);
pn.setLayout(new GridLayout(2, 1));
pn.add(fontPn);
pn.add(ptPn);
fontPn.add(fontLabel);
fontPn.add(fontScroll);
fontPn.add(fontStyleLabel);
fontPn.add(fontTypeScroll);
ptPn.add(ptLabel);
ptPn.add(spinner);
fontList.setVisibleRowCount(5);
fontList.setFixedCellWidth(60);
fontList.setSelectedIndex(50);
fontList.setSelectedValue(font.getFontName(), true);
fontTypeList.setVisibleRowCount(5);
fontTypeList.setSelectedIndex(style);
okCelPn.add(ok);
okCelPn.add(cancel);
okCelPn.setLayout(new FlowLayout(FlowLayout.RIGHT));
this.add(pn, BorderLayout.CENTER);
this.add(okCelPn, BorderLayout.SOUTH);
this.setTitle("字型");
this.pack();
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == ok) {
System.out.println(fontList.getSelectedValue());
style = this.type();
size = Integer.parseInt(spinner.getValue().toString());
font = new Font((String) fontList.getSelectedValue(), style, size);
textarea.setFont(font);
this.dispose();
System.out.println(type());
} else if (e.getSource() == cancel) {
this.dispose();
}
}
private int type() {
if (fontTypeList.getSelectedValue().equals("傾斜")) {
return 1;
} else if (fontTypeList.getSelectedValue().equals("粗體")) {
return 2;
} else if (fontTypeList.getSelectedValue().equals("粗偏斜體")) {
return 3;
} else
return 0;
}
}
class About extends JDialog {// 關於視窗
About() {
JOptionPane.showMessageDialog(null, " 作者:cjb 版本:v1.5\n\n 聯絡:[email protected]", "關於",
JOptionPane.PLAIN_MESSAGE);
}
}
轉載請註明出處。