最简单的文本编辑器 Swing组件

/*
* 文本编辑器
*/
package exersize;

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class JTextEditor extends WindowAdapter implements ActionListener {

private JFrame frame ;
private JTextArea textArea ;
private String filename ;

public void createEditor(){

JMenuBar menuBar = new JMenuBar() ;
JMenu menu = new JMenu("文件") ;

JMenuItem menunew = new JMenuItem("新建") ;
menunew.addActionListener(this) ;
menu.add(menunew) ;

JMenuItem menuopen = new JMenuItem("打开") ;
menuopen.addActionListener(this) ;
menu.add(menuopen) ;

JMenuItem menusave = new JMenuItem("保存") ;
menusave.addActionListener(this) ;
menu.add(menusave) ;

JMenuItem menusaveas = new JMenuItem("另存为...") ;
menusaveas.addActionListener(this) ;
menu.add(menusaveas) ;

menu.addSeparator() ;
JMenuItem menuexit = new JMenuItem("退出") ;
menuexit.addActionListener(this) ;
menu.add(menuexit) ;

menuBar.add(menu) ;

JMenu menuhelp = new JMenu("帮助") ;

JMenuItem menuabout = new JMenuItem("关于") ;
menuabout.addActionListener(this) ;
menuhelp.add(menuabout) ;

menuBar.add(menuhelp) ;


frame = new JFrame("JAVA 文本编辑器") ;
frame.setJMenuBar(menuBar) ;
textArea = new JTextArea() ;
frame.add("Center",textArea) ;
frame.addWindowListener(this) ;
frame.setSize(600,400) ;
    frame.setVisible(true) ;

}

public void actionPerformed(ActionEvent e){

try{

if(e.getActionCommand() == "新建"){
textArea.setText("") ;
}
else if(e.getActionCommand() == "打开"){
//选择文件
JFileChooser dlg = new JFileChooser() ;
int result = dlg.showOpenDialog(frame) ;
if(result == JFileChooser.APPROVE_OPTION){

File file = dlg.getSelectedFile() ;
filename = file.getAbsolutePath() ;

//读取文件
FileReader fr = new FileReader(filename) ;
BufferedReader br = new BufferedReader(fr) ;
String str = "" ;
while(br.ready()){
int c = br.read() ;
str += (char)c ;
}
textArea.setText(str) ;
br.close() ;
fr.close() ;
frame.setTitle("编辑器 -" + filename) ;

}
}
else if(e.getActionCommand() == "保存"){

//写入文件
File file = new File(filename) ;
FileWriter fw = new FileWriter(file,true) ;
BufferedWriter bw = new BufferedWriter(fw) ;
PrintWriter pw = new PrintWriter(bw) ;
//写入数据对象
pw.print(textArea.getText()) ;

bw.close() ;
pw.close() ;
fw.close() ;

}
else if(e.getActionCommand() == "另存为..."){
//选择文件
JFileChooser dlg = new JFileChooser() ;
int result = dlg.showOpenDialog(frame) ;
if(result == JFileChooser.APPROVE_OPTION){
File file = dlg.getSelectedFile() ;
//写入
FileWriter fw = new FileWriter(file,true) ;
BufferedWriter bw = new BufferedWriter(fw) ;
PrintWriter pw = new PrintWriter(bw) ;
pw.print(textArea.getText()) ;

bw.close() ;
pw.close() ;
fw.close() ;

}
}
else if(e.getActionCommand() == "退出"){
System.exit(0) ;
}
else if(e.getActionCommand() == "关于"){
//显示对话框
final JDialog dialog = new JDialog(frame,"关于",true) ;
dialog.setSize(267,117) ;
dialog.setLayout(new GridLayout(2,1)) ;

//窗口关闭事件
dialog.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dialog.dispose() ;
}
}) ;
//显示信息
JPanel panel = new JPanel() ;
JLabel label = new JLabel("作者:" + "laotan") ;

panel.add(label,BorderLayout.NORTH) ;
dialog.add(panel) ;
dialog.setVisible(true) ;

}

}catch(Exception ex){
ex.printStackTrace() ;
}
}

public void windowClosing(WindowEvent e){
System.exit(0) ;
}

public static void main(String[] args) {
// TODO Auto-generated method stub
new JTextEditor().createEditor() ;
}

}

你可能感兴趣的:(C++,c,swing,C#)