麻烦问一下大家,我在一个窗口程序中,在菜单File的菜单项“打开”上添加了一个事件监听器,想实现这样的效果:鼠标单击“打开”后,会打开一个选择文件的对话框,这一步我能做到,但怎样实现在这个对话框中选择所想要打开的文件后(如选择了state.txt文件)单击确定能够打开此文件啊?如何才能实现呢?请教高手尽快指教!部分代码如下:
jmi.addActionListener(new ActionAdapter());//在菜单项上添加事件监听class ActionMonitor implements ActionListener {//
public void actionPerformed(ActionEvent e) {
JFileChooser jfc = new JFileChooser("D:/myjava/state.txt");
int state = jfc.showOpenDialog(null);
if(state == JFileChooser.APPROVE_OPTION) {
File file = jfc.getSelectedFile();
String filename = file.toString().toLowerCase();
String path = file.getAbsolutePath();
try {
if(filename.endsWith(".txt")) {
runtime.exec("rundll32 url.dll NOTEPAD"+path);//打开文件
//runtime.exec("NOTEPAD"+ path);
System.out.println(path);
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
jmi.addActionListener(new ActionAdapter());//在菜单项上添加事件监听class ActionMonitor implements ActionListener {//
public void actionPerformed(ActionEvent e) {
JFileChooser jfc = new JFileChooser("D:/myjava/state.txt");
int state = jfc.showOpenDialog(null);
if(state == JFileChooser.APPROVE_OPTION) {
File file = jfc.getSelectedFile();
String filename = file.toString().toLowerCase();
String path = file.getAbsolutePath();
try {
if(filename.endsWith(".txt")) {
runtime.exec("rundll32 url.dll NOTEPAD"+path);//打开文件
//runtime.exec("NOTEPAD"+ path);
System.out.println(path);
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
import java.io.*;import javax.swing.*;public class Pop extends JFrame implements ActionListener { private static final long serialVersionUID = 1L;
JMenuBar jmb;
JMenu m1,m2,m3;
JMenuItem m11,m12,m13;
File f,f1;
FileInputStream in1=null,in2=null;
SequenceInputStream s=null;
FileOutputStream out=null;
JFileChooser jfc=new JFileChooser();
JTextArea jta;
String str,str1;
FileWriter fw;
public Pop(){
jmb=new JMenuBar();
m1=new JMenu("文件");
jmb.add(m1);
this.setJMenuBar(jmb);
m11=new JMenuItem("读文件");
m11.addActionListener(this);
m12=new JMenuItem("写文件");
m12.addActionListener(this);
m13=new JMenuItem("退出");
m13.addActionListener(this);
m1.add(m11);
m1.add(m12);
m1.add(m13);
this.setTitle("文件读写系统");
jta=new JTextArea("",400,600);
this.add(new JScrollPane(jta,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER));
this.setSize(500, 600);
//this.setResizable(false);
this.setLocationRelativeTo(null);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==m11){
int i=jfc.showOpenDialog(this);
if(i==JFileChooser.APPROVE_OPTION){
jta.setText("");
f=jfc.getSelectedFile();
try {
BufferedReader br=new BufferedReader(new FileReader(f));
try {
while((str=br.readLine())!=null){
jta.append(str);
jta.append("\n");
}
br.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
if(e.getSource()==m12){
int i= jfc.showSaveDialog(this);
jfc.getDialogTitle();
if(i==JFileChooser.APPROVE_OPTION){
try {
BufferedWriter br=new BufferedWriter(new FileWriter(f.toString()));
br.write(jta.getText());
br.close();
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
System.out.print(f);
}
}
if(e.getSource()==m13){
System.exit(0);
}
} public static void main(String[] args) {
new Pop();
}
}