📄 notepad.java
字号:
import java.awt.*;
import java.awt.event.WindowListener;
import java.awt.event.*;
import java.util.EventListener;
import java.io.*;
import javax.imageio.*;
import javax.swing.JOptionPane;
import java.lang.Object.*;
import javax.swing.*;
import java.awt.datatransfer.*;
public class Notepad
{
public static void main(String[] args)
{
final JFrame f = new JFrame("记事本");
f.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
f.setSize(600,400);
f.setLocation(200,200);
//final TextArea ta = new TextArea();
final JTextArea ta = new JTextArea();
//System.out.println(ta.getLineWrap());
//ta.setWrapStyleWord(true);
//ta.setLineWrap(true);
//ta.setWrapStyleWord(true);
JScrollPane jp = new JScrollPane(ta);
//设置滚动条可见
jp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS );
jp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS );
//System.out.println(ta.getWrapStyleWord());
ta.setBackground(Color.white);
f.add(jp);
JMenuBar mb = new JMenuBar();
JMenu m1 = new JMenu("文件");
JMenu m2 = new JMenu("编辑");
JMenu m3= new JMenu("帮助");
JMenuItem mi1 = new JMenuItem("新建");
JMenuItem mi2 = new JMenuItem("打开");
mi2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
FileDialog fd = new FileDialog(f,"打开",FileDialog.LOAD);
fd.show();
String strFile = fd.getDirectory()+fd.getFile();
if(strFile!=null)
{
try
{
FileInputStream fis = new FileInputStream(strFile);
int r = 0;
StringBuffer sb = new StringBuffer();
while (r != -1)
{
byte[] buf = new byte[1024];
r = fis.read(buf);
if (r != -1)
{
sb.append(new String(buf, 0, r));
}
}
ta.append(new String(sb));
//System.out.println(ta.getLineCount());
fis.close();
}
catch(Exception ex)
{
// ex.printStackTrace();
}
}
//fd.show();
}
});
JMenuItem mi3 = new JMenuItem("保存");
mi3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
FileDialog fd = new FileDialog(f,"保存",FileDialog.SAVE);
fd.show();
String strFile = fd.getDirectory()+fd.getFile();
try
{
BufferedWriter bw = new BufferedWriter(new FileWriter(strFile));
bw.write(ta.getText());
bw.flush();
bw.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
});
JMenuItem mi4 = new JMenuItem("另存为");
mi4.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
FileDialog fd = new FileDialog(f,"保存",FileDialog.SAVE);
fd.show();
String strFile = fd.getDirectory()+fd.getFile();
try
{
BufferedWriter bw = new BufferedWriter(new FileWriter(strFile));
bw.write(ta.getText());
bw.flush();
bw.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
});
JMenuItem mi5 = new JMenuItem("退出");
mi5.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(!ta.getText().equals(""))
{
int d = JOptionPane.showConfirmDialog(f,
"记事本的文字已经改变想保存文件吗?",
"记事本",JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE);
if(d == JOptionPane.YES_OPTION)
{
FileDialog fd = new FileDialog(f,"保存",FileDialog.SAVE);
fd.show();
String strFile = fd.getDirectory()+fd.getFile();
try
{
BufferedWriter bw = new BufferedWriter(new FileWriter(strFile));
bw.write(ta.getText());
bw.flush();
bw.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
if(d == JOptionPane.NO_OPTION)
{
System.exit(0);
}
else if(d == JOptionPane.CANCEL_OPTION )
{
}
}
else
{
System.exit(0);
}
}
});
JMenuItem mi61 = new JMenuItem("自动换行");
class ItActionListener1 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
ta.setLineWrap(true);
ta.setWrapStyleWord(true);
}
}
mi61.addActionListener(new ItActionListener1());
JMenuItem mi71 = new JMenuItem("剪切");
mi71.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
cb.setContents(new StringSelection(ta.getSelectedText()),null);
ta.replaceRange("",ta.getSelectionStart(),ta.getSelectionEnd());
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
});
JMenuItem mi81 = new JMenuItem("复制");
mi81.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
cb.setContents(new StringSelection(ta.getSelectedText()),null);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
});
JMenuItem mi91 = new JMenuItem("粘贴");
mi91.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable content = cb.getContents(null);
ta.replaceRange((String)content.getTransferData(DataFlavor.stringFlavor),
ta.getSelectionStart(),ta.getSelectionEnd());
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
});
JMenuItem mi101 = new JMenuItem("删除");
mi101.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
ta.replaceRange("",ta.getSelectionStart(),ta.getSelectionEnd());
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
});
JMenuItem mi111 = new JMenuItem("查找");
mi111.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
final JDialog jd = new JDialog(f,"查找");
jd.setSize(350,120);
jd.setLocation(350,300);
JLabel jl = new JLabel("查找内容");
final JTextField tf = new JTextField(20);
JButton jb1 = new JButton("查找");
JButton jb2 = new JButton("取消");
GridBagLayout gb = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
jd.setLayout(gb);
c.weightx = 60;
c.weighty = 30;
c.gridwidth = GridBagConstraints.RELATIVE;
gb.setConstraints(jl,c);
c.gridwidth = GridBagConstraints.REMAINDER;
gb.setConstraints(tf,c);
c.weightx = 5;
c.gridx = 1;
c.gridwidth = GridBagConstraints.RELATIVE;
gb.setConstraints(jb1,c);
c.gridx = 3;
c.gridx = GridBagConstraints.RELATIVE;
gb.setConstraints(jb2,c);
// c.gridwidth = GridBagConstraints.REMAINDER;
jd.add(jl);
jd.add(tf);
jd.add(jb1);
jd.add(jb2);
jd.setResizable(false);
jd.show();
jb1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
try
{
String str1 = tf.getText();
int len = str1.length();
String text = ta.getText();
int sum = text.length();
int start = ta.getSelectionEnd();
if(start ==sum)
start = 0;
for(;len<=sum&&start<=sum-len;start++)
{
if(start ==sum)
start = 0;
if(text.substring(start,start+len).equals(str1))
{
ta.setSelectionStart(start);
ta.setSelectionEnd(start+len);
return;
}
}
JOptionPane.showMessageDialog(null,"所指定的文本没有找到",
"记事本",JOptionPane.INFORMATION_MESSAGE);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
});
jb2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
jd.dispose();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
});
}
});
JMenuItem mi121 = new JMenuItem("替换");
mi121.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
final JDialog jd = new JDialog(f,"替换");
jd.setSize(350,120);
jd.setLocation(350,300);
JLabel jl1 = new JLabel("查找内容");
final JTextField tf1 = new JTextField(20);
JLabel jl2 = new JLabel("替换为");
final JTextField tf2 = new JTextField(20);
JButton jb1 = new JButton("替换");
JButton jb2 = new JButton("取消");
GridBagLayout gb = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
jd.setLayout(gb);
c.weightx = 60;
c.weighty = 30;
c.gridwidth = GridBagConstraints.RELATIVE;
gb.setConstraints(jl1,c);
c.gridwidth = GridBagConstraints.REMAINDER;
gb.setConstraints(tf1,c);
c.gridwidth = GridBagConstraints.RELATIVE;
gb.setConstraints(jl2,c);
c.gridwidth = GridBagConstraints.REMAINDER;
gb.setConstraints(tf2,c);
c.weightx = 5;
c.gridx = 1;
c.gridwidth = GridBagConstraints.RELATIVE;
gb.setConstraints(jb1,c);
c.gridx = 3;
c.gridx = GridBagConstraints.RELATIVE;
gb.setConstraints(jb2,c);
jd.add(jl1);
jd.add(tf1);
jd.add(jl2);
jd.add(tf2);
jd.add(jb1);
jd.add(jb2);
jd.setResizable(false);
jd.show();
jb1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
try
{
String str1 = tf1.getText();
int len = str1.length();
String str2 = tf2.getText();
String text = ta.getText();
int sum = text.length();
int start = ta.getSelectionEnd();
if(start ==sum)
start = 0;
for(;len<=sum&&start<=sum-len;start++)
{
if(text.substring(start,start+len).equals(str1))
{
ta.replaceRange(str2,start,(start+len));
return;
}
}
JOptionPane.showMessageDialog(null,"所指定的文本没有找到,无法替换",
"记事本",JOptionPane.INFORMATION_MESSAGE);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
});
jb2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
jd.dispose();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
});
}
});
JMenuItem mi131 = new JMenuItem("全选");
mi131.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
String text = ta.getText();
int len = text.length();
ta.selectAll();
}
catch(Exception ex)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -