📄 mytexteditor.java
字号:
{
jb.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
doSaveAs();
}
});
}
else if(actionCommand.equals(quick[4]))
{
jb.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jta.copy();
}
});
}
else if(actionCommand.equals(quick[5]))
{
jb.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jta.paste();
}
});
}
else
{
jb.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jta.cut();
}
});
}
}
public ImageIcon createImage(String str)
{
java.net.URL url=MyTextEditor.class.getResource(str);
if(url==null) return null;
else
return new ImageIcon(url);
}
public static void main(String args[])
{
new MyTextEditor().show(); //技巧。
}
public void newactionlistener()
{
if(changed==true)
{
int n;
Object option[]={"YES","NO","CANCEL"};
n=JOptionPane.showOptionDialog(null,
"你的文件还没有保存呢,你想现在保存吗?",
"保存",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
option,
option[0]);
switch(n)
{
case JOptionPane.YES_OPTION:
doSave(fileName); //保存文件
fileName=""; //清空文件名
jta.setText(""); //清空编辑区
changed=false; //新建文件后,把更新标志改为false;
break;
case JOptionPane.NO_OPTION:
fileName="";
jta.setText("");
changed=false;
break;
case JOptionPane.CANCEL_OPTION:
break;
default:
break;
}
}
else
{
fileName="";
jta.setText("");
}
}
public void openactionlistener()
{
FileDialog fd=new FileDialog(MyTextEditor.this,"Open...",FileDialog.LOAD); //这里注意对象的名字
fd.show();
if(fd.getFile()==null) //如果没有选择文件,则返回
return;
fileName=fd.getDirectory()+File.separator+fd.getFile();//取得文件名[包括路径]
FileInputStream fis=null; //好习惯,在定义的时候先初始化。
String str1=null;
try
{
fis=new FileInputStream(fileName);
int size=fis.available();
byte bytes[]=new byte[size];
fis.read(bytes); // 读取文件
str1=new String(bytes); //建立字符串数组
}catch(IOException e1)
{}
finally
{
try
{
fis.close();
}catch(IOException e2){}
}
if(str1!=null){jta.setText(str1);}
changed=false;
}
class A implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Object o=e.getSource();
JMenuItem jmi=(JMenuItem)o;
if(jmi==newjmi)
{
newactionlistener();
}
else if(jmi==openjmi)
{
openactionlistener();
}
else if(jmi==savejmi)
{
doSave(fileName);
}
else if(jmi==saveasjmi)
{
doSaveAs();
}
else if(jmi==exitjmi)
{
if(changed==true)
{
int n;
Object[] option={"YES","NO"};
n=JOptionPane.showOptionDialog(null,
"你的文件还没有保存呢,你想现在保存吗?",
"保存",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
option,
option[0]);
switch(n)
{
case JOptionPane.YES_OPTION:
doSave(fileName);
break;
case JOptionPane.NO_OPTION:
break;
default: break;
}
}
System.exit(0);
}
else if(jmi==copyjmi)
{
jta.copy();
}
else if(jmi==pastejmi)
{
jta.paste();
}
else if(jmi==cutjmi)
{
jta.cut();
}
else if(jmi==findjmi)
{
new Finder(MyTextEditor.this,jta).show();
}
else if(jmi==fjmi)
{
Color newColor=JColorChooser.showDialog(MyTextEditor.this,"选择字体颜色",jta.getForeground());
if(newColor!=null) jta.setForeground(newColor);
}
else if(jmi==bjmi)
{
Color newColor=JColorChooser.showDialog(MyTextEditor.this,"选择字体颜色",jta.getBackground());
if(newColor!=null) jta.setBackground(newColor);
}
else if(jmi==aboutjmi)
{
new About(MyTextEditor.this,true);
}
else;
}
}
public void doSave(String fileName)
{
if(fileName=="")
{
doSaveAs();
return;
}
FileOutputStream fos=null;
String str3=jta.getText();
try
{
fos=new FileOutputStream(fileName);
fos.write(str3.getBytes());
}catch(IOException e1){}
finally
{
try
{fos.close();}catch(IOException e2){}
}
changed=false;
}
public void doSaveAs()
{
FileDialog fd=new FileDialog(MyTextEditor.this,"Save...",FileDialog.SAVE);
fd.show();
if(fd.getFile()==null) return;
fileName=fd.getDirectory()+File.separator+fd.getFile();
String str4=jta.getText();
FileOutputStream fos=null;
try
{
fos=new FileOutputStream(fileName);
fos.write(str4.getBytes());
}catch(IOException e1){}
finally
{
try{fos.close();}catch(IOException e2){}
}
changed=false;
}
}
class Finder extends JDialog implements ActionListener
{
JFrame jf;
JTextArea jta;
JButton findButton,closeButton;
JPanel findPane,buttonPane;
JLabel findLabel;
JTextField jtf;
public Finder(JFrame jf,JTextArea jta)
{
super(jf,true); //JDialog的构造方法。必须是第一句。
super.setTitle("查找");
super.getContentPane().setLayout(new GridBagLayout());
this.jf=jf;
this.jta=jta;
initComponents();
this.setLocationRelativeTo(jf);
this.pack();
this.requestFocus();
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
Finder.this.dispose();
}
});
}
public void initComponents()
{
GridBagConstraints gbc=new GridBagConstraints();//GridBagLayout要和GridBagConstraints一起用才有效果。
gbc.fill=GridBagConstraints.HORIZONTAL;
gbc.weightx=1.0;
gbc.insets=new Insets(0,5,0,0);
findButton=new JButton();
closeButton=new JButton();
findPane=new JPanel();
buttonPane=new JPanel();
findLabel=new JLabel();
jtf=new JTextField();
findPane.setLayout(new GridBagLayout());
findLabel.setText("查找内容");
findLabel.setLabelFor(jtf);
findPane.add(findLabel,new GridBagConstraints());
jtf.addActionListener(this); //在文本框里面按回车键。
findPane.add(jtf,gbc);
gbc=new GridBagConstraints();
gbc.fill=GridBagConstraints.BOTH;
gbc.insets=new Insets(11,12,0,11);
this.getContentPane().add(findPane,gbc);
buttonPane.setLayout(new GridBagLayout());
findButton.setText("查找");
findButton.setMnemonic(KeyEvent.VK_F);
findButton.setToolTipText("查找");
findButton.addActionListener(this);
buttonPane.add(findButton,new GridBagConstraints());
closeButton.setText("关闭");
closeButton.setMnemonic(KeyEvent.VK_C);
closeButton.setToolTipText("关闭");
closeButton.addActionListener(this);
gbc=new GridBagConstraints();
gbc.insets=new Insets(0,5,0,0);
buttonPane.add(closeButton,gbc);
gbc=new GridBagConstraints();
gbc.gridx=0;gbc.gridy=1;
gbc.anchor=GridBagConstraints.SOUTHEAST;
gbc.insets=new Insets(17,12,11,11);
this.getContentPane().add(buttonPane,gbc);
}
public void actionPerformed(ActionEvent e)
{
String textStr=jta.getText();
String findStr=jtf.getText();
if(findStr.equals(""));
else
{
int index=textStr.indexOf(findStr);
if(index!=-1)//找到查找的内容
{
jta.setCaretPosition(index); //将光标移到第index个字符上。
}
else
Toolkit.getDefaultToolkit().beep();
}
this.dispose(); //不管是按回车, 还是查找后,还是关闭,最后都要关闭。
}
}
class About extends JDialog
{
JFrame jf;
JTextField jtf;
public About(JFrame jf,boolean b)
{
super(jf,b);
this.jf=jf;
initComponents();
this.getContentPane().add(jtf,BorderLayout.CENTER);
super.setLocationRelativeTo(jf);
super.pack();
this.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){About.this.dispose();}});
super.show();
}
public void initComponents()
{
jtf=new JTextField();
jtf.setBackground(new Color(0x44ffee));
jtf.setEditable(false);
jtf.setText("一个简单的记事本");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -