题目要求
编写简单文本编辑器,该程序可以新建、打开、编辑和保存文本文件。当用户点击New时,新建一个文件,用户可以编辑文件内容,然后点击Save保存文件。用户点击Open时,选择一个已有文件,然后可对文件进行编辑。示例输出如图所示。
代码示例
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class FileChooserTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame = new FileChooserFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class FileChooserFrame extends JFrame{
private static final int DEFAULE_WIDTH=600;
private static final int DEFAULT_HEIGHT=400;
private JFileChooser jfc;
private JTextArea jta;
private JScrollPane scrollPane;
public FileChooserFrame() {
setTitle("FileChooserTest");
setSize(DEFAULE_WIDTH, DEFAULT_HEIGHT);
jfc = new JFileChooser();
jfc.setCurrentDirectory(new File("."));
jta = new JTextArea(8,40);
add(jta);
jta.setVisible(false);
scrollPane=new JScrollPane(jta);
add(scrollPane);
JMenuBar jmb = new JMenuBar();
setJMenuBar(jmb);
JMenu menu = new JMenu("File");
jmb.add(menu);
JMenuItem newItem = new JMenuItem("New");
menu.add(newItem);
newItem.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
jta.setVisible(true);
jta.setText("");
setTitle("new");
}
}
);
JMenuItem openItem = new JMenuItem("Open");
menu.add(openItem);
openItem.addActionListener(
new ActionListener() {
@SuppressWarnings("static-access")
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
jta.setVisible(true);
int result = jfc.showOpenDialog(null);
if(result == jfc.APPROVE_OPTION) {
String name = jfc.getSelectedFile().getPath();
jta.setText("");
setTitle(name);
try {
FileInputStream fin = new FileInputStream(name);
InputStreamReader reader = new InputStreamReader(fin,"utf-8");
BufferedReader br = new BufferedReader(reader);
String line;
while ((line = br.readLine()) != null) {
jta.append(line+"\n");
}
br.close();
reader.close();
fin.close();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
);
JMenuItem saveItem = new JMenuItem("Save");
menu.add(saveItem);
saveItem.addActionListener(
new ActionListener() {
@SuppressWarnings("static-access")
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
jta.setVisible(true);
int result = jfc.showSaveDialog(null);
if(result == jfc.APPROVE_OPTION) {
File file = jfc.getSelectedFile();
if (!file.exists()){
try {
file.createNewFile();
} catch (IOException exception) {
exception.printStackTrace();
}
}
try {
FileOutputStream fout = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fout, "utf-8");
osw.append(jta.getText().trim());
osw.close();
fout.close();
} catch (Exception exception) {
exception.printStackTrace();
}
}
}
}
);
JMenuItem exitItem = new JMenuItem("Exit");
menu.add(exitItem);
exitItem.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
}
);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/103068.html