//FileChoose.java
//打开对话框,需要先点open才打开对话框,而不是先打开对话框----
//2009-11-12
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.awt.event.*;
class FileChoose extends JFrame
{
FileChoose(String title){
super(title);
}
JButton openButton=new JButton("Open");
JTextField txt1=new JTextField(20);
JTextField txt2=new JTextField(20);
JFileChooser choose=new JFileChooser(new File("c://java"));
class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e){
int openResult=choose.showOpenDialog(null);
if (openResult==JFileChooser.APPROVE_OPTION)
{
txt1.setText(choose.getSelectedFile().getName());
txt2.setText(choose.getCurrentDirectory().toString());
}
if (openResult==JFileChooser.CANCEL_OPTION)
{
txt1.setText("you selected Cancel button");
txt2.setText("");
}
}
}
ButtonListener openListener=new ButtonListener();
public void init(){
Container cp=getContentPane();
cp.setLayout(new FlowLayout());
openButton.addActionListener(openListener);
cp.add(openButton);
cp.add(txt1);
cp.add(txt2);
}
public static void main(String[] args){
FileChoose openFile=new FileChoose("打开");
openFile.init();
openFile.setSize(240,180);
openFile.setVisible(true);
openFile.addWindowListener(new WindowClose());
}
}
class WindowClose extends WindowAdapter
{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
窗口还是不能完全关闭,为什么?