對上一個打開對話框作了調整

 //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);
 }
}

 

 

 

窗口還是不能完全關閉,爲什麼?

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章