swing的简单应用,写的不好,还有好多药完善,作为初学者可以参考一下
import java.awt.FileDialog;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextArea;
public class TextEdit {
static String filepath;
static JFrame frame = new JFrame("文本编辑器");
static TextArea tf = new TextArea("",30,40,TextArea.SCROLLBARS_BOTH);
//TextArea(String text, int rows,int columns, int scrollbars)scrollbars
//参数值提供的常量: SCROLLBARS_BOTH, SCROLLBARS_VERTICAL_ONLY, SCROLLBARS_HORIZONTAL_ONLY, SCROLLBARS_NONE.
static JMenuBar menubar=new JMenuBar();
static JMenu menuFile=new JMenu("文件");
JMenu menuEdit=new JMenu("编辑");
JMenu menuLook=new JMenu("查看");
JMenu menuTool=new JMenu("工具");
JMenu menuDoc=new JMenu("文档");
static JMenuItem newFile=new JMenuItem("新建 Ctrl+N");
static JMenuItem Open=new JMenuItem("打开 Ctrl+O");
JMenuItem Save=new JMenuItem("保存 Ctrl+S");
JMenuItem Lsave=new JMenuItem("另存为 Shift+Ctrl+S");
JMenuItem Cut=new JMenuItem("剪切 Ctrl+X");
JMenuItem Copy=new JMenuItem("复制 Ctrl+C");
JMenuItem Paste=new JMenuItem("粘贴 Ctrl+V");
JMenuItem delete=new JMenuItem("删除 Ctrl+D");
JMenuItem ToolItem=new JMenuItem("工具栏 ");
JMenuItem zt=new JMenuItem("状态栏 ");
JMenuItem cb=new JMenuItem("测边栏 ");
JMenuItem tc=new JMenuItem("突出显示模式 ");
JMenuItem Doctongji=new JMenuItem("文档统计 ");
JMenuItem Docsave=new JMenuItem("文档保存 ");
public TextEdit(){
frame.setSize(600,500);
frame.setVisible(true);
frame.add(tf);
frame.setJMenuBar(menubar);
menubar.add(menuFile);
menubar.add(menuEdit);
menubar.add(menuLook);
menubar.add(menuTool);
menubar.add(menuDoc);
menuFile.add(newFile);
menuFile.add(Open);
menuFile.add(Save);
menuFile.add(Lsave);
menuEdit.add(Cut);
menuEdit.add(Copy);
menuEdit.add(Paste);
menuEdit.add(delete);
menuLook.add(ToolItem);
menuLook.add(zt);
menuLook.add(cb);
menuLook.add(tc);
menuTool.add(Doctongji);
menuDoc.add(Docsave);
init();
}
public void init(){
Open.addActionListener(new openFileAction(filepath));
Lsave.addActionListener(new saveFileAction(filepath));
}
public static void main(String[] args) {
new EditText();
}
}
//打开文件菜单项的监听事件类
class openFileAction implements ActionListener{
private String filepath=null;;
private JTextArea tf=null;
public openFileAction(String filepath){
this.filepath=filepath;
this.tf=tf;
}
public void actionPerformed(ActionEvent e) {
BufferedReader br=null;
FileDialog dialog=new FileDialog(new JFrame(),"打开 指定文件 ",FileDialog.LOAD);
dialog.setVisible(true);
filepath=dialog.getDirectory()+dialog.getFile();
File file=new File(filepath);
try {
br=new BufferedReader(new FileReader((file)));
String content;
StringBuffer sb=new StringBuffer();
while((content=br.readLine())!=null){
sb.append(content+"\n");
}
tf.setText(sb.toString());//添加的内容会覆盖原有的
tf.append(sb.toString());//在原有的基础上追加
} catch (Exception e1) {
e1.printStackTrace();
}
finally{
try {
br.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
//保存文件菜单项的监听事件类
class saveFileAction implements ActionListener{
private String filepath=null;;
private JTextArea tf=null;
public saveFileAction(String filepath){
this.filepath=filepath;
this.tf=tf;
}
public void actionPerformed(ActionEvent e) {
FileDialog dialog=new FileDialog(new JFrame(),"保存文件 ",FileDialog.LOAD);
dialog.setVisible(true);
filepath=dialog.getDirectory()+dialog.getFile();
File file=new File(filepath);
FileOutputStream fos =null;
try {
fos = new FileOutputStream(file);
String content=tf.getText();
byte [] b=content.getBytes();
fos.write(b);
fos.close();//关闭输出管道
} catch (Exception e1) {
e1.printStackTrace();
}
}
}