菜单条
import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.awt.event.*;
class WindowReader extends JFrame implements ActionListener{
JFileChooser fileDialog;
JMenuBar menuBar;
JMenu menu;
JMenuItem itemSave,itemOpen;
JTextArea text;
BufferedReader in;
FileReader fileReader;FileWriter fileWriter;
WindowReader(){
init();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setSize(300,400);
}
void init(){
text=new JTextArea(10,10);
text.setFont(new Font("宋体_gb2312w",Font.PLAIN,28));
add(new JScrollPane(text),BorderLayout.CENTER);
menuBar=new JMenuBar();
menu=new JMenu("文件");
itemOpen=new JMenuItem("打开文件");
itemSave=new JMenuItem("保存文件");
itemSave.addActionListener(this);
itemOpen.addActionListener(this);
menu.add(itemOpen);menu.add(itemSave);
menuBar.add(menu);
setJMenuBar(menuBar);
fileDialog=new JFileChooser();
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==itemSave){
int state=fileDialog.showSaveDialog(this);
if(state==JFileChooser.APPROVE_OPTION){
try{
File dir=fileDialog.getCurrentDirectory();
String name=fileDialog.getSelectedFile().getName();
File file=new File(dir,name);
fileWriter=new FileWriter(file);
BufferedWriter out;
out=new BufferedWriter(fileWriter) ;
out.write(text.getText());
out.close();
fileWriter.close();
}catch(Exception ee){
ee.printStackTrace();
}
}
}
else if(e.getSource()==itemOpen){
int state=fileDialog.showOpenDialog(this);
if(state==JFileChooser.APPROVE_OPTION){
text.setText(null);
try{
File dir=fileDialog.getCurrentDirectory();
String name=fileDialog.getSelectedFile().getName();
File file=new File(dir,name);
fileReader=new FileReader(file);
in=new BufferedReader(fileReader);
String s;
while((s=in.readLine())!=null){
text.append(s+" \n");
}
in.close();
fileReader.close();
}catch(Exception ee){
ee.printStackTrace();
}
}
}
}
}
public class visualOpen{
public static void main(String args[]){
WindowReader win=new WindowReader();
win.setTitle("使用文件对话框读写文件");
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/b499839388af8aed30998f4575923a43.png)
JFileChooser更多
按钮
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class FileFrame extends JFrame implements ActionListener {
TextArea ta;
Button open, quit;
FileDialog fd;
FileFrame() {
super("获取显示文本文件");
ta = new TextArea(10, 45);
open = new Button("open");
quit = new Button("close");
Dimension preferredSize = new Dimension(100,100);
open.setPreferredSize(preferredSize );
quit.setPreferredSize(preferredSize );
open.addActionListener(this);
quit.addActionListener(this);
setLayout(new FlowLayout());
add(ta);
add(open);
add(quit);
show();
setSize(300,300);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == "open") {
fd = new FileDialog(this, "打开文件", FileDialog.LOAD);
fd.setDirectory("f:");
fd.show();
try {
File myfile = new File(fd.getDirectory(), fd.getFile());
System.out.print(myfile);
FileReader inFile = new FileReader(myfile);
BufferedReader inB = new BufferedReader(inFile);
String fileCountent = "", str = "";
while ((fileCountent = inB.readLine()) != null) {
str = str + fileCountent + "\n";
ta.append(fileCountent + "\n");
}
System.out.print(str);
} catch (IOException ie) {
System.out.println(ie.toString());
}
}
if (e.getActionCommand() == "close") {
dispose();
System.exit(0);
}
}
}
public class visualOpen{
public static void main(String args[]){
new FileFrame().setTitle("使用按钮读写文件");
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/e6ced1fbd715af1a3e6bb947ad103329.png)