⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 note.java

📁 JAVA课程设计 简单文本编辑器。适合JAVA初学者做课程设计!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package note;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
public class note {
public static void main(String[] args)
{ 
myfr fr=new myfr("高攀的java记事本");
fr.setSize(700,400);
}
}
///////////////////////////myfr主窗体类//////////////////////////////////////
class myfr extends JFrame implements ActionListener
{ 
String str,strnext,path,fname; //部分变量的声明
JPanel mainpane; 
JFileChooser choose = new JFileChooser(); //文件对话框
Dialog find,replace; //find为查找对话框,replace对话框
JTextField findtxt,repltxt; //find为查找对话框的输入文本区,replace对话框的输入文本区
Font newfont;
JButton findenter,replb; //find为查找对话框,replace对话框的确定按钮
JLabel state=new JLabel("高攀javaX记事本------未命名文档"); //状态栏
JTextArea txt1; //主输入文本区
File newfiles;
JPopupMenu popm; //弹出菜单声明
JMenu m1,m2,m3,m4,m5,m6; //各菜单项
JMenuItem m61,m62,m26,m271,m34,m51,m52,m53,m54,p_copy,p_cut,p_paste,p_del;
int startp,endp,nexttemp,newstartp,newendp; //查找替换时所用的临时变量
JToolBar toolbar = new JToolBar(); //工具条 

JButton newf=new JButton(new ImageIcon("pic/new.gif")); //图标在PIC下
JButton open=new JButton(new ImageIcon("pic/open.gif")); 
JButton save=new JButton(new ImageIcon("pic/save.gif")); 
JButton copy=new JButton(new ImageIcon("pic/copy.gif"));
JButton cut=new JButton(new ImageIcon("pic/cut.gif")); 
JButton pp=new JButton(new ImageIcon("pic/pp.gif")); 
JButton del=new JButton(new ImageIcon("pic/del.gif")); 
JButton findc=new JButton(new ImageIcon("pic/find.gif")); 
JButton color=new JButton(new ImageIcon("pic/color.gif"));
JButton help=new JButton(new ImageIcon("pic/help.gif")); 
JButton exit=new JButton(new ImageIcon("pic/exit.gif")); 



myfr(String sss) /////构造函数开始
{
super(sss);
JMenuBar mb=new JMenuBar();
fname=null; //初始文件名为空 
findenter=new JButton("确定");
findenter.addActionListener(this); //声明对话框中上确定按钮,并注册事件
replb=new JButton("确定");
replb.addActionListener(this);

mainpane=(JPanel)this.getContentPane(); 
mainpane.setLayout(new BorderLayout()); 
txt1=new JTextArea("",13,61);
txt1.addMouseListener(new handlemouse(this));//注册鼠标右击事件
txt1.setFont(new Font("宋体",Font.PLAIN,18));
mainpane.add(txt1, BorderLayout.CENTER); 
mainpane.add("North",toolbar); 
mainpane.add("South",state);
JScrollPane sll = new JScrollPane(); //创建滚动条
mainpane.add("Center", sll); ;
sll.getViewport().add(txt1); //将滚动条装入文本区

popm=new JPopupMenu(); ////POPMeun 开始
p_copy=new JMenuItem("复制 ");
p_copy.addActionListener(this);
KeyStroke keycopyp=KeyStroke.getKeyStroke(KeyEvent.VK_C,Event.CTRL_MASK); 
p_copy.setAccelerator(keycopyp); 
p_cut=new JMenuItem("剪切 ");
p_cut.addActionListener(this);
KeyStroke keycutp=KeyStroke.getKeyStroke(KeyEvent.VK_X,Event.CTRL_MASK); 
p_cut.setAccelerator(keycutp); 
p_paste=new JMenuItem("粘贴 ");
p_paste.addActionListener(this);
KeyStroke keypp=KeyStroke.getKeyStroke(KeyEvent.VK_V,Event.CTRL_MASK); 
p_paste.setAccelerator(keypp); 
p_del=new JMenuItem("删除 ");
p_del.addActionListener(this);
KeyStroke keydelp=KeyStroke.getKeyStroke(KeyEvent.VK_DELETE,0); 
p_del.setAccelerator(keydelp); 
popm.add(p_copy);
popm.add(p_cut);
popm.add(p_paste);
popm.add(p_del);
txt1.add(popm); ////POPMenu 结束

m1=new JMenu("文件(F)");
m1.setMnemonic('F'); //定义快捷方式
JMenuItem m10=new JMenuItem("新建 ");
m10.addActionListener(this); //注册事件监听器
KeyStroke keynew=KeyStroke.getKeyStroke(KeyEvent.VK_N,Event.CTRL_MASK); //定义快捷键
m10.setAccelerator(keynew); 

JMenuItem m11=new JMenuItem("打开 ");
m11.addActionListener(this);
KeyStroke keyopen=KeyStroke.getKeyStroke(KeyEvent.VK_O,Event.CTRL_MASK); 
m11.setAccelerator(keyopen); 

JMenuItem m12=new JMenuItem("保存 ");
m12.addActionListener(this);
KeyStroke keysave=KeyStroke.getKeyStroke(KeyEvent.VK_S,Event.CTRL_MASK); 
m12.setAccelerator(keysave); 

JMenuItem m13=new JMenuItem("另保存为 ");
m13.addActionListener(this);
JMenuItem m14=new JMenuItem("退出 ");
m14.addActionListener(this);
KeyStroke keyexit=KeyStroke.getKeyStroke(KeyEvent.VK_F4,Event.ALT_MASK); 
m14.setAccelerator(keyexit); 
////////////////////////////////////////////
m2=new JMenu("编辑(E)");
m2.setMnemonic('E');
JMenuItem m21=new JMenuItem("复制 ");
m21.addActionListener(this);
KeyStroke keycopy=KeyStroke.getKeyStroke(KeyEvent.VK_C,Event.CTRL_MASK); 
m21.setAccelerator(keycopy); 

JMenuItem m22=new JMenuItem("剪切 ");
m22.addActionListener(this);
KeyStroke keycut=KeyStroke.getKeyStroke(KeyEvent.VK_X,Event.CTRL_MASK); 
m22.setAccelerator(keycut); 

JMenuItem m23=new JMenuItem("粘贴 ");
m23.addActionListener(this);
KeyStroke keyp=KeyStroke.getKeyStroke(KeyEvent.VK_V,Event.CTRL_MASK); 
m23.setAccelerator(keyp); 

JMenuItem m24=new JMenuItem("删除 ");
m24.addActionListener(this);
KeyStroke keydel=KeyStroke.getKeyStroke(KeyEvent.VK_DELETE,0); 
m24.setAccelerator(keydel); 

JMenuItem m25=new JMenuItem("查找 ");
m25.addActionListener(this);
KeyStroke keyfind=KeyStroke.getKeyStroke(KeyEvent.VK_F,Event.CTRL_MASK); 
m25.setAccelerator(keyfind); 

m26=new JMenuItem("查找下一个 ");
m26.addActionListener(this);
KeyStroke keyfn=KeyStroke.getKeyStroke(KeyEvent.VK_F3,0); 
m26.setAccelerator(keyfn); 
m26.setEnabled(false);

JMenuItem m27=new JMenuItem("替换 ");
m27.addActionListener(this);
KeyStroke keyrepl=KeyStroke.getKeyStroke(KeyEvent.VK_H,Event.CTRL_MASK); 
m27.setAccelerator(keyrepl); 

m271=new JMenuItem("替换下一个");
m271.setEnabled(false);
m271.addActionListener(this);
KeyStroke keyrepn=KeyStroke.getKeyStroke(KeyEvent.VK_F6,0); 
m271.setAccelerator(keyrepn); 

JMenuItem m28=new JMenuItem("全选 ");
m28.addActionListener(this);
KeyStroke keyall=KeyStroke.getKeyStroke(KeyEvent.VK_A,Event.CTRL_MASK); 
m28.setAccelerator(keyall); 

JMenuItem m29=new JMenuItem("日期/时间 ");
m29.addActionListener(this);

m3=new JMenu("格式(O)");
m3.setMnemonic('O');
JMenu m31=new JMenu("字体样式 ");
JMenuItem m311=new JMenuItem("正常 ");
m311.addActionListener(this);
JMenuItem m312=new JMenuItem("粗体 ");
m312.addActionListener(this);
JMenuItem m313=new JMenuItem("斜体 ");
m313.addActionListener(this);

JMenu m32=new JMenu("字体大小 ");
JMenuItem m321=new JMenuItem("最大 ");
m321.addActionListener(this);
JMenuItem m322=new JMenuItem("较大 ");
m322.addActionListener(this);
JMenuItem m323=new JMenuItem("适中 ");
m323.addActionListener(this);
JMenuItem m324=new JMenuItem("较小 ");
m324.addActionListener(this);
JMenuItem m325=new JMenuItem("最小 ");
m325.addActionListener(this);

JMenuItem m33=new JMenuItem("字体颜色 ");
m33.addActionListener(this);

m34=new JMenuItem("自动换行 ");
m34.addActionListener(this);

m5=new JMenu("视图风格(V)");
m5.setMnemonic('V');

m52=new JMenuItem("MOTIF风格 ");
m52.addActionListener(this);
m53=new JMenuItem("默认风格 ");
m53.addActionListener(this);
m54=new JMenuItem("状态栏 ");
m54.addActionListener(this);

m6=new JMenu("帮助(H)");
m6.setMnemonic('H'); 
m61=new JMenuItem("帮助主题 ");
m61.addActionListener(this);
m62=new JMenuItem("关于 ");
m62.addActionListener(this);
//添加各项
m1.add(m10); m1.add(m11); m1.add(m12); m1.add(m13); m1.addSeparator(); m1.add(m14);
m2.add(m21); m2.add(m22); m2.add(m23); m2.add(m24); m2.addSeparator(); m2.add(m25);
m2.add(m26); m2.add(m27); m2.add(m271); m2.addSeparator(); m2.add(m28);
m2.add(m29); m3.add(m31); m31.add(m311); m31.add(m312); m31.add(m313);
m3.add(m32); m32.add(m321); m32.add(m322); m32.add(m323); m32.add(m324); 
m32.add(m325); m3.add(m33); m3.addSeparator(); m3.add(m34); //m5.add(m51);
m5.add(m52); m5.add(m53); m5.addSeparator(); m5.add(m54); m6.add(m61); 
m6.addSeparator(); m6.add(m62); mb.add(m1); mb.add(m2); mb.add(m3); 
mb.add(m5); mb.add(m6); this.setJMenuBar(mb); //设置菜单栏

toolbar.add(newf); //工具栏各按钮
newf.setText("新 建");
newf.setToolTipText("新建");
newf.addActionListener(this);
toolbar.add(open); 
open.setText("打 开");
open.setToolTipText("打开");
open.addActionListener(this);
toolbar.add(save);
save.setText("保 存");
save.setToolTipText("保存");
save.addActionListener(this);
toolbar.addSeparator(); 
toolbar.add(copy);
copy.setText("复 制");
copy.setToolTipText("复制");
copy.addActionListener(this);
toolbar.add(cut);
cut.setText("剪 切");
cut.setToolTipText("剪切");
cut.addActionListener(this);
toolbar.add(pp);
pp.setText("粘 贴");
pp.setToolTipText("粘贴");
pp.addActionListener(this);
toolbar.add(del);
del.setText("删 除");
del.setToolTipText("删除");
del.addActionListener(this);
toolbar.addSeparator(); 
toolbar.add(findc);
findc.setText("查 找");
findc.setToolTipText("查找");
findc.addActionListener(this);
toolbar.add(color);
color.setToolTipText("字体颜色");
color.setText("字体颜色");
color.addActionListener(this);
toolbar.addSeparator(); 
toolbar.add(help);
help.setToolTipText("帮助主题");
help.setText("帮助主题");
help.addActionListener(this);
toolbar.add(exit);
exit.setText("退 出");
exit.setToolTipText("退出");
exit.addActionListener(this);

setVisible(true);
pack();
show();
this.addWindowListener(new xxx(this));
//toolbar.setBackground(new Color(150,150,240));
//txt1.setBackground(new Color(170,180,245));
}///构造函数结束///

////////////////事件处理/////////////////////////////////////
public void actionPerformed(ActionEvent p)
{
if(p.getActionCommand()=="新建 "||p.getSource()==newf) //响应菜单及工具栏事件
{ 
fname=null; //置文件名为空,便于判断文件是否保存过
txt1.setText("");
state.setText(" 我的javaX记事本------未命名文档");
}

if(p.getActionCommand()=="打开 "||p.getSource()==open)
{
try {
if(this.choose.APPROVE_OPTION==this.choose.showOpenDialog(this))
{ 
path=this.choose.getSelectedFile().getPath(); 
fname=this.choose.getSelectedFile().getName();
File file=new File(path);
int flength=(int)file.length(); 
FileReader fReader=new FileReader(file); 
char[] data=new char[flength]; 
fReader.read(data,0,flength); 
txt1.setText(new String(data)); 
state.setText(" 我的javaX记事本------"+path+" 共"+flength+"字节");//状态栏统计文件字节数
txt1.setCaretPosition(0);
}}
catch(IOException e)
{}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -