📄 notepad.java
字号:
//郑世杰 200631500155 计科5班
import java.io.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.filechooser.*;
import javax.swing.event.*;
import java.math.*;
import java.awt.image.BufferedImage;
import java.awt.Toolkit;
public class NotePad extends JFrame implements ActionListener,ItemListener{
JButton b_save,b_close;
JTextArea textArea;
boolean isCurrentFileExists=false;
File tempFile;
JPanel jp;
JMenu file,edit,style,help;
JCheckBoxMenuItem s_autoLine;
JMenuItem f_new,f_open,f_save,f_close,f_saveas,e_copy,e_paste,e_cut,e_clear,e_selectAll,s_font,s_color,h_editor;
JMenuBar jmb;
JScrollPane jsp;
JPopupMenu popUpMenu=new JPopupMenu();
JLabel stateBar;
JLabel j1,jstate;
JFileChooser jfc=new JFileChooser();
JMenuItem je_copy,je_paste,je_cut,je_clear,je_selectAll;
public NotePad() //构造函数,初始化
{
jp = new JPanel();
// j1 = new JLabel("JAVA记事本");
// jstate = new JLabel("状态栏:");
jmb = new JMenuBar(); //初始化菜单栏
textArea = new JTextArea(); //初始化编辑窗口
jsp = new JScrollPane(textArea);
file = new JMenu("File");
edit = new JMenu("Edit");
style = new JMenu("Style");
help = new JMenu("SOS");
je_copy=new JMenuItem("Copy");
je_paste=new JMenuItem("Paste");
je_cut=new JMenuItem("Cut");
je_clear=new JMenuItem("Clear");
je_selectAll=new JMenuItem("SelectAll");
je_copy.addActionListener(this);
je_paste.addActionListener(this);
je_cut.addActionListener(this);
je_clear.addActionListener(this);
je_selectAll.addActionListener(this);
f_new = new JMenuItem("New");
f_new.addActionListener(this);
f_open = new JMenuItem("Open");
f_open.addActionListener(this);
f_save = new JMenuItem("Save");
f_save.addActionListener(this);
f_saveas = new JMenuItem("Save as");
f_saveas.addActionListener(this);
f_close = new JMenuItem("Close");
f_close.addActionListener(this);
e_copy = new JMenuItem("Copy");
e_copy.addActionListener(this);
e_paste = new JMenuItem("Paste");
e_paste.addActionListener(this);
e_cut = new JMenuItem("Cut");
e_cut.addActionListener(this);
e_clear=new JMenuItem("Clear");
e_clear.addActionListener(this);
e_selectAll=new JMenuItem("SelectAll");
e_selectAll.addActionListener(this);
s_font=new JMenuItem("Font");
s_font.addActionListener(this);
s_color=new JMenuItem("Color");
s_color.addActionListener(this);
s_autoLine=new JCheckBoxMenuItem("AutoLine",true);
s_autoLine.addItemListener(this);
h_editor = new JMenuItem("About");
h_editor.addActionListener(this);
stateBar = new JLabel("NewFile");
stateBar.setHorizontalAlignment(SwingConstants.LEFT);
stateBar.setBorder(BorderFactory.createEtchedBorder());
//添加右键弹出式菜单
//popUpMenu = edit.getPopupMenu();
popUpMenu.add(je_copy);
popUpMenu.add(je_paste);
popUpMenu.add(je_cut);
popUpMenu.add(je_clear);
// 编辑区键盘事件
textArea.addKeyListener(
new KeyAdapter()
{
public void keyTyped(KeyEvent e)
{
stateBar.setText("已修改");
}
});
// 编辑区鼠标事件,点击右键弹出"编辑"菜单
textArea.addMouseListener(
new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
if(e.getButton() == MouseEvent.BUTTON3)
popUpMenu.show(e.getComponent(), e.getX(), e.getY());
}
public void mouseClicked(MouseEvent e) {
if(e.getButton() == MouseEvent.BUTTON1)
popUpMenu.setVisible(false);
}
}
);
this.setJMenuBar(jmb);
this.setTitle("记事本");
Image image=this.getToolkit().getImage("zheng.gif");
this.setIconImage(image);
file.add(f_new);
file.add(f_open);
file.add(f_save);
file.add(f_saveas);
file.add(f_close);
edit.add(e_copy);
edit.add(e_paste);
edit.add(e_cut);
edit.add(e_clear);
edit.add(e_selectAll);
style.add(s_autoLine);
style.add(s_font);
style.add(s_color);
help.add(h_editor);
jmb.add(file);
jmb.add(edit);
jmb.add(style);
jmb.add(help);
b_save = new JButton("保存");
b_save.addActionListener(this);
b_close = new JButton("关闭");
b_close.addActionListener(this);
this.add(jsp);
this.setSize(800,600);
this.setVisible(true);
//设置窗口居中显示
int W = (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth();
int H = (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight();
this.setLocation((W-this.getWidth())/2,(H-this.getHeight())/2);
//为窗口添加"关闭"事件的响应
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
whenExit();
}
});
} /////////////创建工作完成//////////////////
//响应菜单项"自动换行"的事件
public void itemStateChanged(ItemEvent event)
{
if(event.getSource()==s_autoLine)
{
if(s_autoLine.getState())
{
textArea.setLineWrap(true);
}
else
textArea.setLineWrap(false);
}
}
//判断文件是否被修改
public boolean isCurrentFileSaved()
{
if(stateBar.getText().equals("已保存")||stateBar.getText().equals("新文档"))
{
return true;
}
else {
return false;
}
}
//打开文件对话框
public void openFileDialog()
{
int m=jfc.showOpenDialog(this);
if(m== JFileChooser.APPROVE_OPTION)
{
File f=jfc.getSelectedFile();
for(int i=0;i<=f.length();i++)
{
char [] ch = new char[i];
try{
FileReader fr = new FileReader(f);
fr.read(ch);
String str = new String(ch);
textArea.setText(str);
isCurrentFileExists=true;
tempFile=f;
}
catch(FileNotFoundException fe )
{ JOptionPane.showMessageDialog(null,"未找到需要打开的文件!"); }
catch(IOException ie)
{ System.err.println(ie); }
}
}
else
return;
}
//保存文件对话框
public void saveFileDialog()
{
int options=jfc.showSaveDialog(this);
String fname = null;
if(options == JFileChooser.APPROVE_OPTION)
{
File f = jfc.getSelectedFile();//如果没有选取文件,下面的jfc.getName(f)将会返回输入的文件名
fname = jfc.getName(f);
if(fname != null && fname.trim().length()>0)
{
if(fname.endsWith(".txt"))
;
else
{
fname = fname.concat(".txt");
}
}
if(f.isFile())
fname = f.getName();
f = jfc.getCurrentDirectory();
f = new File(f.getPath().concat(File.separator).concat(fname));
if(f.exists())
{
int i=JOptionPane.showConfirmDialog(null,"该文件已经存在,确定要覆盖吗?");
if(i ==JOptionPane.YES_OPTION)
;
else
return;
}
try{
f.createNewFile();
String str=textArea.getText();
FileWriter fw = new FileWriter(f);
fw.write(str);
fw.close();
isCurrentFileExists=true;
tempFile=f;
stateBar.setText("已保存");
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null, "出错:"+ ex.getMessage());
return ;
}
}
}
//剪切
public void cut() {
textArea.cut();
stateBar.setText("已修改");
popUpMenu.setVisible(false);
}
//复制
public void copy() {
textArea.copy();
popUpMenu.setVisible(false);
}
//粘贴
public void paste() {
textArea.paste();
stateBar.setText("已修改");
popUpMenu.setVisible(false);
}
//清除
public void clear(){
textArea.replaceRange("",textArea.getSelectionStart(),textArea.getSelectionEnd());
stateBar.setText("已修改");
popUpMenu.setVisible(false);
}
//全选
public void selectAll(){
textArea.selectAll();
}
//当退出程序时判断文档状态
public void whenExit()
{
if(isCurrentFileSaved()) //关闭窗口时判断是否保存了文件
{
dispose();
System.exit(0);
}
else
{
int i=JOptionPane.showConfirmDialog(null,"文件未保存,是否要保存?","提示",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE);
if(i ==JOptionPane.YES_OPTION)
saveFileDialog();
else if(i==JOptionPane.NO_OPTION)
{
dispose();
System.exit(0);
}
else
return;
}
}
public void actionPerformed(ActionEvent e)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -