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();
}
}
java 新手求大神
你的代码使用的全部是Swing,但是根据你的错误来看,你确实把它当做JavaFx应用程序来编译运行的。如果是想写JavaFx,则类必须扩展javafx.application.Application
有时候 Eclipse 会发神经,好端端的 project 就这么编译不了了,连 Hello World 都会报“找不到或无法加载主类”的错误,我已经遇到好几次了,以前是懒得深究就直接重建project了,但遇到次数多了必须深究下了,现总结几种解决方案,大家根据自己的情况逐一尝试即可。
1、是因为.java文件不在项目的src路径内,也就是说源代码未被eclipse编译,字节码不存在无法运行了在项目名上右键 -> Builder Path -> Configure Build Path -> 选择Source面板 再点Add Folder, 把源代码所在的包路径的上层目录加进来,而且如果你是把两个类写在一个文件里的话,你在右键选择Run As Java Appication 的时候,要把光标至于包括main方法的类上
2、最快捷的解决办法是,打开带有main函数的类,ctrl +A (全选)---> ctrl +X(剪切)-----> ctrl+S(保存)--->关闭---->再打开--->ctrl +C (粘贴) ---->OK;一句话就是粘出去 关闭 再粘进来
3、重建Project也行。
4、项目的Java Build Path中的Libraries中也许某个jar包是不可用的,显示红色叉叉。这说明系统找不到这个这个jar文件,把这个jar删除或者重新加载进来即可。
5、如果上述不行,大家直接在 cmd 下 javac 然后 java 运行试试,如果这都报错,那是你的 jdk 或者 os 环境有问题了。
转自IT女汉子的新浪博客
** 刚用她的方法解决了,发出来如果以后有相同遭遇的人可以参考下**
package 对话框; 说实话,这种定义方式我还是头一次见
这种人确实是到处都有,要是关注一些不重要的事情