package 对话框;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JFileChooser;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
public class JFileChooserDemo extends JFrame{
private JPanel p;
private JScrollPane sp;
private JButton btnOpen,btnSave,btnClear;
private JTextArea txtContent;
public JFileChooserDemo(){
super("JFileChooser文件对话框");
p=new JPanel();
btnOpen=new JButton("打开");
btnSave=new JButton("保存");
btnClear=new JButton("清除");
txtContent=new JTextArea(20,10);
sp=new JScrollPane(txtContent);
btnOpen.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
openFile();
}
});
btnSave.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
saveFile();
}
});
btnClear.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
txtContent.setText("");
}
});
p.add(btnOpen);
p.add(btnSave);
p.add(btnClear);
this.add(sp);
this.add(p,BorderLayout.SOUTH);
this.setSize(400,300);
this.setLocation(400,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
private void openFile(){
JFileChooser fc=new JFileChooser();
int rVal=fc.showOpenDialog(this);
if(rVal==JFileChooser.APPROVE_OPTION){
String fileName=fc.getSelectedFile().getName();
String path=fc.getCurrentDirectory().toString();
try{
FileReader fread=new FileReader(path+"/"+fileName);
BufferedReader bread=new BufferedReader(fread);
String line=bread.readLine();
while(line!=null){
txtContent.append(line+"\n");
line=bread.readLine();
}
bread.close();
fread.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
private void saveFile(){
JFileChooser fc=new JFileChooser();
int rVal=fc.showSaveDialog(this);
if(rVal==JFileChooser.APPROVE_OPTION){
String fileName=fc.getSelectedFile().getName();
String path=fc.getCurrentDirectory().toString();
try{
FileWriter fwriter=new FileWriter(path+"/"+fileName);
fwriter.write(txtContent.getText());
fwriter.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
public static void main(String[] args){
new JFileChooserDemo();
}
}
错误: 在类 对话框.JFileChooserDemo 中找不到 main 方法, 请将 main 方法定义为:
public static void main(String[] args)
否则 JavaFX 应用程序类必须扩展javafx.application.Application
java 新手求大神