📄 notepad_main.java
字号:
/**
*
*/
package com.edu.sccp.snail.notepad.view;
/**
* @author SSnail
* @time 2008-1-3
* @dscr 小豆包's 文本编辑器
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.undo.CannotUndoException;
public class Notepad_Main implements ActionListener ,ItemListener{
/**
* @param args
* @dscri 构造函数
* @author 小豆包
*/
public Notepad_Main() {
super();
try {
initComponent();
init();
notepad.setDefaultCloseOperation(exit);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 初始化窗体
*@param no args
*@return a Notepad Windows
*/
public void init() {
notepad.setSize(new Dimension(600, 450));
notepad.setTitle("文本编辑器");
c.setLayout(new BorderLayout());
notepad.setJMenuBar(jmb);
c.add(jtb, BorderLayout.NORTH);
// jtb.addMouseListener(new JPopumMenu_MouseListener()); //右键监听事件
c.add(jsp, BorderLayout.CENTER);
c.add(jl,BorderLayout.SOUTH);
popumMenuListener = new JPopumMenu_MouseListener();
notepad.addMouseListener(popumMenuListener); // 右键监听事件
notepad.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent w){
if(jb3.isEnabled()){
int optionType = JOptionPane.showConfirmDialog(notepad,notepad.getTitle()+"文件是否保存","关闭文件",JOptionPane.YES_NO_CANCEL_OPTION);
if(optionType == JOptionPane.YES_OPTION)
{
/**
* 保存代码块
*/
}
else if(optionType == JOptionPane.CANCEL_OPTION ) {
notepad.dispose();
System.exit(0);
}else if(optionType == JOptionPane.NO_OPTION){
//关闭自己的代码怎么写?
}
}else{
System.exit(0);
}
}
});
notepad.setVisible(true);
}
/**
* 得到状态栏
*
*/
public JLabel getJLabel(){
JLabel label = new JLabel("文本编辑器 1.0 ");
return label;
}
/**
* @param no args
* @dscri 初始化组件
* @author 小豆包
*/
public void initComponent() {
notepad = new JFrame();
c = notepad.getContentPane();
jmb = getJMenuBar();
jtb = getJToolBar();
jsp = getJSplitPane();
jpm = getJPopuMenu(); // 右键菜单
jl = getJLabel();
}
/**
* 得到右键菜单
* @return PopupMenu
* @author 小豆包
* @time 08/1/4
*/
public JPopupMenu getJPopuMenu() {
JPopupMenu pm1 = new JPopupMenu();
pm1.add(find = new JMenuItem("查找"));
pm1.addSeparator();
pm1.add(change = new JMenuItem("替换"));
pm1.addSeparator();
pm1.add(per = new JMenuItem("属性"));
find.setAccelerator(KeyStroke.getKeyStroke('F', InputEvent.SHIFT_MASK));
change.setAccelerator(KeyStroke
.getKeyStroke('C', InputEvent.SHIFT_MASK));
per.setAccelerator(KeyStroke.getKeyStroke('P', InputEvent.ALT_MASK));
return pm1;
}
/**
* 获得一个有两个面板的面板
* @return JPanel
* @author 小豆包
* @param jp2 : 树形的父窗体
* @param jp1 : 编辑区
*/
public JSplitPane getJSplitPane() {
jp1 = new JPanel();
jp1.setLayout(null);
JScrollPane jp2 = getJtree_JScrollPane();
JSplitPane jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, jp2, jp1);
jsp.setDividerSize(4);
jsp.addMouseListener(popumMenuListener); // 右键监听事件
return jsp;
}
/**
* 返回一个带有树形结构的JPanel
* @author 小豆包
* @time 08/1/6
*/
public JScrollPane getJtree_JScrollPane(){
JScrollPane jp = new JScrollPane();
jt = new Notepad_Tree(this);
jp.setViewportView(jt);
/**
* 绑定组件事件,当组件大小改变时
* 滚动条随之大小改变
* 确保滚动条的大小与其父组件的大小一致
*/
/* jp.addComponentListener(new ComponentAdapter(){
public void componentResized(ComponentEvent e) {
Component parnet = e.getComponent();
Dimension parnetSize = parnet.getSize();
jsp1.setSize(parnetSize);
}
});*/
return jp;
}
/**
* 获得工具栏
* @return JToolBar
* @author 小豆包
*/
public JToolBar getJToolBar() {
JToolBar btj = new JToolBar();
jb1 = new JButton(icon1);
jb2 = new JButton(icon3);
jb3 = new JButton(icon2);
jb4 = new JButton(icon4);
jb5 = new JButton(icon5);
jb6 = new JButton(icon6);
jb1.setToolTipText("新建");
jb2.setToolTipText("打开");
jb3.setToolTipText("保存");
jb4.setToolTipText("复制");
jb5.setToolTipText("剪切");
jb6.setToolTipText("粘贴");
btj.add(jb1);
btj.add(jb2);
btj.add(jb3);
btj.add(jb4);
btj.add(jb5);
btj.add(jb6);
////////////////////////////////////////
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
jb4.addActionListener(this);
jb5.addActionListener(this);
jb6.addActionListener(this);
////////////////////////////////////////
btj.setFloatable(false);
return btj;
}
/**
* 构造一个JMenuBar
*
* @return JMenuBar
* @author 小豆包
*/
public JMenuBar getJMenuBar() {
JMenuBar jmb = new JMenuBar();
JMenu fileF = new JMenu("文件(F)");
JMenu fileE = new JMenu("编辑(E)");
JMenu fileO = new JMenu("格式(O)");
JMenu fileV = new JMenu("查看(V)");
JMenu fileS = new JMenu("外观(S)");
JMenu fileH = new JMenu("帮助(H)");
fileF1 = new JMenuItem("新建 (N)");
fileF2 = new JMenuItem("打开 (O)");
fileF3 = new JMenuItem("保存 (S)");
fileF4 = new JMenuItem("另存为(A)");
fileF5 = new JMenuItem("退出(E)");
fileE1 = new JMenuItem("撤销(U)");
fileE2 = new JMenuItem("剪贴(T)");
fileE3 = new JMenuItem("复制(C)");
fileE4 = new JMenuItem("粘贴(P)");
fileE5 = new JMenuItem("查找(F)");
fileE6 = new JMenuItem("查找下一个(N)");
fileE7 = new JMenuItem("替换(R)");
fileO1 = new JCheckBoxMenuItem("自动换行(W)",true);
fileO2 = new JMenuItem("字体(F)");
fileO3 = new JMenuItem("颜色(C)");
fileV1 = new JCheckBoxMenuItem("状态栏(Z)",true);
fileH1 = new JMenuItem("主题");
fileF1.setAccelerator(KeyStroke.getKeyStroke('N',KeyEvent.ALT_MASK,false));
fileF2.setAccelerator(KeyStroke.getKeyStroke('O',KeyEvent.ALT_MASK,false));
fileF3.setAccelerator(KeyStroke.getKeyStroke('S',KeyEvent.ALT_MASK,false));
fileF4.setAccelerator(KeyStroke.getKeyStroke('A',KeyEvent.ALT_MASK,false));
fileF5.setAccelerator(KeyStroke.getKeyStroke('E',KeyEvent.ALT_MASK,false));
fileE1.setAccelerator(KeyStroke.getKeyStroke('U',KeyEvent.ALT_MASK,false));
fileE2.setAccelerator(KeyStroke.getKeyStroke('T',KeyEvent.ALT_MASK,false));
fileE3.setAccelerator(KeyStroke.getKeyStroke('C',KeyEvent.ALT_MASK,false));
fileE4.setAccelerator(KeyStroke.getKeyStroke('P',KeyEvent.ALT_MASK,false));
fileE5.setAccelerator(KeyStroke.getKeyStroke('F',KeyEvent.ALT_MASK,false));
fileE6.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F3,KeyEvent.ALT_MASK,false));
fileE7.setAccelerator(KeyStroke.getKeyStroke('R',KeyEvent.ALT_MASK,false));
fileO1.setAccelerator(KeyStroke.getKeyStroke('W',KeyEvent.ALT_MASK,false));
fileO2.setAccelerator(KeyStroke.getKeyStroke('F',KeyEvent.CTRL_MASK,false));
fileO3.setAccelerator(KeyStroke.getKeyStroke('C',KeyEvent.CTRL_MASK,false));
fileV1.setAccelerator(KeyStroke.getKeyStroke('Z',KeyEvent.ALT_MASK,false));
fileF1.addActionListener(this);
fileF2.addActionListener(this);
fileF3.addActionListener(this);
fileF4.addActionListener(this);
fileF5.addActionListener(this);
fileE1.addActionListener(this);
fileE2.addActionListener(this);
fileE3.addActionListener(this);
fileE4.addActionListener(this);
fileE5.addActionListener(this);
fileE6.addActionListener(this);
fileE7.addActionListener(this);
fileO1.addItemListener(this);
fileO2.addActionListener(this);
fileO3.addActionListener(this);
fileV1.addItemListener(this);
fileH1.addActionListener(this);
fileF.add(fileF1);
fileF.add(fileF2);
fileF.addSeparator();
fileF.add(fileF3);
fileF.add(fileF4);
fileF.addSeparator();
fileF.add(fileF5);
fileE.add(fileE1);
fileE.addSeparator();
fileE.add(fileE2);
fileE.add(fileE3);
fileE.addSeparator();
fileE.add(fileE4);
fileE.add(fileE5);
fileE.add(fileE6);
fileE.addSeparator();
fileE.add(fileE7);
fileO.add(fileO1);
fileE.addSeparator();
fileO.add(fileO2);
fileO.add(fileO3);
fileV.add(fileV1);
fileH.add(fileH1);
jmb.add(fileF);
jmb.add(fileE);
jmb.add(fileO);
jmb.add(fileV);
jmb.add(fileS);
jmb.add(fileH);
return jmb;
}
/**
* 事件处理
*/
class JPopumMenu_MouseListener extends MouseAdapter {
public void mousePressed(MouseEvent e) {
Component parner = e.getComponent();
if (e.getButton() == MouseEvent.BUTTON3) {
int x = e.getX();
int y = e.getY(); // 获得鼠标在父组件上的坐标
Dimension parentSize = parner.getSize(); // 返回父组件的大小
Dimension d = jpm.getSize();
y = ((parentSize.height - y) - d.height) > 0 ? y
: (parentSize.height - d.height); // 如果坐标显示不开 则自动定位
x = ((parentSize.width - x) - d.width) > 0 ? x
: (parentSize.width - d.width);
jpm.show(parner, x, y);
}
}
}
public void itemStateChanged(ItemEvent e) {
//显示状态栏
if(((JCheckBoxMenuItem) fileV1).getState()){
jl.setVisible(true);
}else{
jl.setVisible(false);
}
if(((JCheckBoxMenuItem)fileO1).getState()){
/**
* 自动换行
*/
nf.jta.setLineWrap(true);
nf.jta.setWrapStyleWord(true);
}else{
nf.jta.setLineWrap(false);
nf.jta.setWrapStyleWord(false);
}
}
/**
* 事件处理
*/
public void actionPerformed(ActionEvent e) {
Object ob = e.getSource();
if(ob == fileF1 || ob == jb1) { //// 新建 (N)
nf = new Notepad_newFile("无标题文档");
jp1.add(nf);
}if(ob == fileF2 || ob == jb2) { // 打开 (O)
new Notepad_OpenFile(this);
}if(ob == fileF3 || ob == jb3) { //
/**
* 保存代码块
*/
new Notepad_SaveFile(this);
//fileF3.setEnabled(false);
}if(ob == fileF4 || e.getSource() == jb4) { //
{
/**
* 另存代码块
*/
}
}if(ob == fileF5) { //
{
/**
* 退出代码块
*/
}
System.exit(0);
}if(ob == fileE1){
/**
* 撤销代码块
*/
try{
nf.getUndo().undo();
}catch(CannotUndoException e2){
}
}if(ob == fileE2){
/**
* 剪贴代码块
*/
}if(ob == fileE3 || e.getSource() == jb4){
/**
* 复制代码块
* 还没贴过来
* 有时间再说
*/
}if(ob == fileE4){
/**
* 黏贴代码块
* 还没贴过来
* 有时间再说
*/
}if(ob == fileE5){
/**
* 查找代码块
* 还没贴过来
* 有时间再说
*/
new Notepad_LookFor("查找",nf);
}if(ob == fileE6){
/**
* 查找下一个代码块
* 还没贴过来
* 有时间再说
*/
}if(ob == fileE7){
/**
* 替换代码块
*/
new Notepad_Change(nf);
}if(ob == fileO2){
/**
* 字体代码块
*/
jp1.add(new Notepad_Font_Frame(nf));
}if(ob == fileO3){
/**
* 颜色代码块
*/
new Notepad_Color_Frame(nf);
}if(ob == fileH1){
/**
* 帮助代码块
*/
Notepad_Help nh = new Notepad_Help("帮助");
jp1.add(nh);
}
}
private final static int exit = JFrame.EXIT_ON_CLOSE;
JFrame notepad = null;//
private Container c = null;//
private JMenuBar jmb = null;//
private JToolBar jtb = null;//
private JSplitPane jsp = null; //
private JPopupMenu jpm = null; // 右键菜单
private JPopumMenu_MouseListener popumMenuListener;
private JMenuItem copy = null, paste = null, cut = null, per = null,
refresh = null, find = null, change = null; // 右键功能菜单
private JMenuItem fileF1,fileF2,fileF3,fileF4,fileF5,
fileE1,fileE2,fileE3,fileE4,fileE5
,fileE6,fileE7,fileO1,fileO2,fileO3
,fileV1,fileH1;
private JTree jt = null;
private JLabel jl = null;
JPanel jp1 = null; //右边编辑区
Notepad_Font_Frame notepad_font = null;
Notepad_newFile nf = null;
private Icon icon1 = new ImageIcon("icon\\newfile.jpg");
private Icon icon2 = new ImageIcon("icon\\save.jpg");
private Icon icon3 = new ImageIcon("icon\\open.jpg");
private Icon icon4 = new ImageIcon("icon\\copy.jpg");
private Icon icon5 = new ImageIcon("icon\\cut.jpg");
private Icon icon6 = new ImageIcon("icon\\paste.jpg");
JButton jb1 = null;
JButton jb2 = null;
JButton jb3 = null;
JButton jb4 = null;
JButton jb5 = null;
JButton jb6 = null;
public static void main(String[] args) {
new Notepad_Main();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -