📄 txt.java
字号:
JMenuItem shanchu=new JMenuItem("删除(L)");
JMenuItem chazhao=new JMenuItem("查找(F)");
JMenuItem tihuan=new JMenuItem("替换(R)");
chexiao.setMnemonic('U');
jianqie.setMnemonic('T');
fuzhi.setMnemonic('C');
niantie.setMnemonic('A');
shanchu.setMnemonic('D');
chazhao.setMnemonic('F');
tihuan.setMnemonic('R');
chexiao.setAccelerator(KeyStroke.getKeyStroke('Z',java.awt.Event.CTRL_MASK,false));
jianqie.setAccelerator(KeyStroke.getKeyStroke('X',java.awt.Event.CTRL_MASK,false));
fuzhi.setAccelerator(KeyStroke.getKeyStroke('C',java.awt.Event.CTRL_MASK,false));
niantie.setAccelerator(KeyStroke.getKeyStroke('V',java.awt.Event.CTRL_MASK,false));
shanchu.setAccelerator(KeyStroke.getKeyStroke('D',java.awt.Event.CTRL_MASK,false));
chazhao.setAccelerator(KeyStroke.getKeyStroke('F',java.awt.Event.CTRL_MASK,false));
tihuan.setAccelerator(KeyStroke.getKeyStroke('R',java.awt.Event.CTRL_MASK,false));
jianqie.addActionListener(new ActionListener() { //添加剪切的事件处理
public void actionPerformed(ActionEvent e){
theArea.cut();
} });
fuzhi.addActionListener(new ActionListener() { //添加复制的事件处理
public void actionPerformed(ActionEvent e){
theArea.copy();
}
});
niantie.addActionListener(new ActionListener() { //添加粘贴的事件处理
public void actionPerformed(ActionEvent e){
theArea.paste();
}
} ) ;
thefile.add(chexiao);
thefile.addSeparator(); //添加分割条
thefile.add(jianqie);
thefile.add(fuzhi);
thefile.add(niantie);
thefile.add(shanchu);
thefile.addSeparator();
thefile.add(chazhao);
thefile.add(tihuan);
return thefile;
}
public JMenu oppFileMenu() {
JMenu thefile=new JMenu("格式(O)");
thefile.setMnemonic('O'); //添加快捷键
JMenuItem huanhang=new JMenuItem("自动换行(W)");
JMenu ziti=new JMenu("字体(F)");
JMenuItem backcolor=new JMenuItem("背景颜色");
JMenuItem size=new JMenuItem("大小"); //设置及连菜单
JMenuItem yanse=new JMenuItem("颜色");
JMenuItem yangshi=new JMenuItem("样式");
huanhang.setMnemonic('W');
ziti.setMnemonic('F');
huanhang.setAccelerator(KeyStroke.getKeyStroke('W',java.awt.Event.CTRL_MASK,false));
// ziti.setAccelerator(KeyStroke.getKeyStroke('F',java.awt.Event.CTRL_MASK,false));
ziti.add(size);
ziti.add(yanse);
ziti.add(yangshi);
thefile.add(huanhang);
thefile.add(ziti);
thefile.add(backcolor);
//
class font extends JDialog implements ItemListener,ActionListener //字体类
{
public int style=Font.PLAIN,size=15;
public String s2="宋体";
JDialog fontdialog = new JDialog(f,"字体",true);
JComboBox li1,li2,li3;
String zt[]={"Arial","ArialBlack","ArialNarrow","宋体","幼圆","隶书"},
zx[]={"常规","斜体","粗体","粗斜体"},
dx[]={"10","11","12","14","16","48"};
JButton jb1,jb2;
font()
{
//this.tm=tm;
JPanel p = new JPanel();
Label l1 = new Label("字体:");
Label l2 = new Label("字形:");
Label l3 = new Label("大小:");
li1 = new JComboBox();
li2 = new JComboBox();
li3 = new JComboBox();
li1.addItemListener(this);
li2.addItemListener(this);
li3.addItemListener(this);
jb1 = new JButton("确定");
jb2 = new JButton("取消");
jb1.addActionListener(this);
jb2.addActionListener(this);
for(int i=0;i<zt.length;i++)
{
li1.addItem(zt[i]);
}
for(int i=0;i<zx.length;i++)
{
li2.addItem(zx[i]);
}
for(int i=0;i<dx.length;i++)
{
li3.addItem(dx[i]);
}
p.add(l1).setBounds(11, 10, 65, 23);
p.add(li1).setBounds(11, 40, 138, 20);
p.add(l2).setBounds(new Rectangle(158, 10, 65, 23));
p.add(li2).setBounds(new Rectangle(152, 40, 109, 20));
p.add(l3).setBounds(new Rectangle(273, 10, 65, 23));
p.add(li3).setBounds(new Rectangle(267, 40, 66, 20));
p.add(jb1);
p.add(jb2);
fontdialog.add(p);
fontdialog.setSize(380,150);
fontdialog.setVisible(true);
fontdialog.setBounds(250,120,380,150);
}
public void itemStateChanged(ItemEvent ei)
{
String strr=(String)ei.getItem();
if(strr==zx[0])
style=Font.PLAIN;
if(strr==zx[1])
style=Font.ITALIC;
if(strr==zx[2])
style=Font.BOLD;
if(strr==zx[3])
style=Font.BOLD+Font.ITALIC;
for(int i=0;i<6;i++)
if(strr==zt[i])
s2=zt[i];
for(int i=0;i<6;i++)
if(strr==dx[i])
size=Integer.parseInt(dx[i]);
}
public void actionPerformed(ActionEvent e)
{
JButton bb = (JButton)e.getSource();
if(bb.getLabel().equals("确定"))
{
Font font=new Font(s2,style,size);
theArea.setFont(font);
// tm.text.setFont(font);
fontdialog.dispose();
}
if(bb.getLabel().equals("取消"))
{
fontdialog.dispose();
}
}
} //字体类结束
//
size.addActionListener(new ActionListener(){ //添加字体大小
public void actionPerformed(ActionEvent e){
new font();
}});
yangshi.addActionListener(new ActionListener(){ //添加字体样式大小
public void actionPerformed(ActionEvent e){
new font();
}});
huanhang.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
theArea.setLineWrap(true); //换行
theArea.setWrapStyleWord(true); //换行不断字
}} );
yanse.addActionListener(new ActionListener() { //添加字体的及连菜单颜色的事件处理
public void actionPerformed(ActionEvent e){
Color color=JColorChooser.showDialog(f,"请选择字体颜色",Color.black);
// theArea.setSelectedTextColor(color);
//SimpleAttributeSet sas=new SimpleAttributeSet();
theArea.setSelectionColor(color);
theArea.setForeground(color);
}
});
backcolor.addActionListener(new ActionListener() { //添加背景颜色处理事件
public void actionPerformed(ActionEvent e){
Color color=JColorChooser.showDialog(f,"请选择背景颜色",Color.white);
theArea.setBackground(color);
}
} );
return thefile;
}
public JMenu viewFileMenu() {
JMenu thefile=new JMenu("查看(V)");
thefile.setMnemonic('V'); //添加快捷键
JMenuItem zhuangtai=new JMenuItem("状态拦(V)");
zhuangtai.setMnemonic('V');
zhuangtai.setAccelerator(KeyStroke.getKeyStroke('W',java.awt.Event.CTRL_MASK,false));
thefile.add(zhuangtai);
return thefile;
}
public JMenu helpFileMenu() {
JMenu thefile=new JMenu("帮助(H)");
thefile.setMnemonic('H'); //添加快捷键
JMenuItem helpz=new JMenuItem("帮助主题(H)");
JMenuItem helpj=new JMenuItem("关于记事本(A)");
helpz.setMnemonic('W');
helpj.setMnemonic('F');
helpz.setAccelerator(KeyStroke.getKeyStroke('W',java.awt.Event.CTRL_MASK,false));
helpj.setAccelerator(KeyStroke.getKeyStroke('F',java.awt.Event.CTRL_MASK,false));
helpz.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e){
String message="这个是我的第一个小作品!\n 为了代码清晰撤消功能,保存询问对话框没有写出来\n欢迎一起来研究学习!\n特此感谢*呆版时代*的支持\n 作者:鱼泪成海(QQ:76037562)";
int optionType=JOptionPane.DEFAULT_OPTION;
int messageType=JOptionPane.INFORMATION_MESSAGE;
JOptionPane.showConfirmDialog(f,message,"记事本",optionType,messageType);
}
});
helpj.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e){
String message="这个是我的第一个小作品!\n 为了代码清晰撤消功能,保存询问对话框没有写出来\n欢迎一起来研究学习!\n特此感谢*呆版时代*的支持\n 作者:鱼泪成海(QQ:76037562)";
int optionType=JOptionPane.DEFAULT_OPTION;
int messageType=JOptionPane.INFORMATION_MESSAGE;
JOptionPane.showConfirmDialog(f,message,"记事本",optionType,messageType);
}
});
thefile.add(helpz);
thefile.add(helpj);
return thefile;
}
public static void main(String args[]) {
new Txt();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -