📄 notepad.java
字号:
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class MainWindow extends JFrame implements ActionListener
{
//声明所要用到的组件
JMenuBar menubar;
JMenu file,edit,help;
JMenuItem newfile,open,save,exit,cut,copy,paste,about;
JFileChooser fc;
JScrollPane sp;
JTextArea ta;
JDialog aboutDlg;
JLabel lb1,lb2;
JButton btn1,tb1,tb2,tb3;
JToolBar tb;
MainWindow(String title)
{
super(title);
setBounds(350,70,600,650);
setVisible(true);
//创建所要用到的组件
menubar=new JMenuBar();
file=new JMenu("文件");
newfile=new JMenuItem("新建");
open=new JMenuItem("打开");
save=new JMenuItem("保存");
exit=new JMenuItem("退出");
edit=new JMenu("编辑");
cut=new JMenuItem("剪切");
copy=new JMenuItem("复制");
paste=new JMenuItem("粘贴");
help=new JMenu("帮助");
about=new JMenuItem("关于Notepad V1.0");
aboutDlg=new JDialog(this,"关于Notepad V1.0");
lb1=new JLabel("Notepad V1.0 版权所有(c)2007 锐意科技");
lb2=new JLabel("本程序为原创程序,仅用于学习参考之用");
btn1=new JButton("确定");
fc=new JFileChooser();
ta=new JTextArea();
tb=new JToolBar();
//需要3个图片文件
tb1=new JButton(new ImageIcon("New.gif"));
tb2=new JButton(new ImageIcon("Open.gif"));
tb3=new JButton(new ImageIcon("Save.gif"));
sp=new JScrollPane(ta);
//设置对话框属性
aboutDlg.setLayout(new FlowLayout());
aboutDlg.setBounds(450,300,250,150);
aboutDlg.add(lb1);
aboutDlg.add(lb2);
aboutDlg.add(btn1);
//添加组件
setJMenuBar(menubar);
menubar.add(file);
file.add(newfile);
file.add(open);
file.add(save);
file.addSeparator();
file.add(exit);
menubar.add(edit);
edit.add(cut);
edit.add(copy);
edit.add(paste);
menubar.add(help);
help.add(about);
add(sp);
this.getContentPane().add(tb,BorderLayout.NORTH);
tb.add(tb1);
tb.add(tb2);
tb.add(tb3);
//注册快捷 键
newfile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK));
open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));
save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));
cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK));
copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));
paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK));
//为组件注册事件监视器
newfile.addActionListener(this);
open.addActionListener(this);
save.addActionListener(this);
exit.addActionListener(this);
cut.addActionListener(this);
copy.addActionListener(this);
paste.addActionListener(this);
about.addActionListener(this);
btn1.addActionListener(this);
tb1.addActionListener(this);
tb2.addActionListener(this);
tb3.addActionListener(this);
validate();
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
//新建
if(e.getActionCommand()=="新建"||e.getSource()==tb1)
{
new MainWindow("无标题-Notepad V1.0");
}
//打开
else if(e.getActionCommand()=="打开"||e.getSource()==tb2)
{
int returnVal=fc.showOpenDialog(this);
if(returnVal==JFileChooser.APPROVE_OPTION)
{
File file=new File(fc.getSelectedFile().getPath());
try
{
FileReader fr=new FileReader(file);
int len=(int)file.length();
char[] c=new char[len];
fr.read(c);
ta.setText(new String(c));
fr.close();
}
catch(IOException e1)
{
e1.printStackTrace();
}
this.setTitle(fc.getSelectedFile().getName()+"-Notepad V1.0");
}
}
//保存
else if(e.getActionCommand()=="保存"||e.getSource()==tb3)
{
int returnVal=fc.showSaveDialog(this);
if(returnVal==JFileChooser.APPROVE_OPTION)
{
File file=new File(fc.getSelectedFile().getPath());
try
{
FileWriter fw=new FileWriter(file);
String text=ta.getText();
fw.write(text);
fw.close();
}
catch(IOException e1)
{
e1.printStackTrace();
}
this.setTitle(fc.getSelectedFile().getName()+"-Notepad V1.0");
}
}
//退出
else if(e.getActionCommand()=="退出")
{
System.exit(0);
}
//剪切
else if(e.getActionCommand()=="剪切")
{
ta.cut();
}
//复制
else if(e.getActionCommand()=="复制")
{
ta.copy();
}
//粘贴
else if(e.getActionCommand()=="粘贴")
{
ta.paste();
}
//关于Notepad V1.0
else if(e.getActionCommand()=="关于Notepad V1.0")
{
aboutDlg.setVisible(true);
}
//弹出版本信息对话框
else if(e.getSource()==btn1)
{
aboutDlg.dispose();
}
}
}
public class Notepad
{
//程序入口
public static void main(String[] args)
{
MainWindow mainwindow=new MainWindow("Notepad V1.0");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -