package MyContainer;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.io.*;
public class editor extends JFrame{
JMenuBar menubar;//菜单栏
JMenu filemenu;
JMenu editmenu;
JMenuItem jload;//文件
JMenuItem jsave;
JMenuItem jexit;
JMenuItem jcut;//编辑
JMenuItem jcopy;
JMenuItem jpast;
JMenuItem jnull;
JPanel toppanel;//面版
JPanel editorpanel;
JScrollPane scorller;
JTextArea jedit;
editor(){ //构造函数
toppanel=new JPanel();
toppanel.setLayout(new BorderLayout());
editorpanel=new JPanel();
editorpanel.setLayout(new BorderLayout());
editorpanel.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
scorller=new JScrollPane();
jedit=new JTextArea();
jedit.setColumns(40);
this.setTitle("鹏鹏文本编辑器");
this.getContentPane().add(toppanel,BorderLayout.CENTER);
this.addWindowListener(new java.awt.event.WindowAdapter(){
public void windowClosing(WindowEvent e){System.exit(0);} });
toppanel.add(editorpanel,BorderLayout.CENTER);
editorpanel.add(scorller,BorderLayout.CENTER);
scorller.getViewport().add(jedit);
filemenu=new JMenu("文件");//添加菜单栏
editmenu=new JMenu("编辑");//添加编辑菜单
menubar=new JMenuBar();
setJMenuBar(menubar);
menubar.add(filemenu);
menubar.add(editmenu);
jload=new JMenuItem("打开");//文件
jsave=new JMenuItem("保存");
jexit=new JMenuItem("退出");
jcut=new JMenuItem("剪切");//编辑
jcopy=new JMenuItem("复制");
jpast=new JMenuItem("粘贴");
jnull=new JMenuItem("清空");
filemenu.add(jload);//文件项目增加
filemenu.add(jsave);
filemenu.add(jexit);
editmenu.add(jcut);//编辑项目增加
editmenu.add(jcopy);
editmenu.add(jpast);
editmenu.add(jnull);
pack();
setSize(400,300);
setVisible(true);
action();
}
void action(){
jload.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
loadFile();
}
});
jsave.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
saveFile();
}
});
jexit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
jcut.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
cut();
}
});
jcopy.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
copy();
}
});
jpast.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
past();
}
});
jnull.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
jedit.setText("");
}
});
}
private void loadFile(){//打开文件的方法
JFileChooser fc=new JFileChooser();
int returnval=fc.showOpenDialog(this);
if(returnval==JFileChooser.APPROVE_OPTION){
File file=fc.getSelectedFile();
try{
jedit.read(new FileReader(file),null);
}catch(IOException e){}
}
}
private void saveFile(){//保存文件的方法
JFileChooser fc=new JFileChooser();
int returnval=fc.showSaveDialog(this);
if(returnval==JFileChooser.APPROVE_OPTION){
File file=fc.getSelectedFile();
try{
jedit.write(new FileWriter(file));
}catch(IOException ep){}
}
}
private void copy(){
jedit.copy();
jedit.requestFocus();
}
private void past(){
jedit.paste();
jedit.requestFocus();
}
private void cut(){
jedit.cut();//剪切
jedit.requestFocus();
}
public static void main(String[] args){
new editor();
}
}