CTIGERS 2009-07-13 09:42
浏览 280
已采纳

我有一段java代码,请教怎样写才能实现自动关闭这个窗口

主要目的是通过一个选择程序的对话框来选择一个文件的路径,然后把这个路径传到后台去(只传路径,不用考虑打开文件的问题).现在当选择完路径后,路径被自动传到了后台,可是我想要同时自动关闭掉那个JFrame但不关闭整个程序(因为有后台的处理),请问关闭代码应该怎么写,写在什么地方??代码如下:

package ft;

import java.awt.FileDialog;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class MyTest extends JFrame implements ActionListener{
JButton jbtn = null;
JTextField jtf = null;
public MyTest(){
this.setLayout(new FlowLayout());
jbtn = new JButton("打开..");
jbtn.addActionListener(this);
jtf = new JTextField(30);
this.add(jtf);
this.add(jbtn);

    this.setSize(400,400);
    this.setVisible(true);
    this.pack();
}
public void actionPerformed(ActionEvent e) {
    if(e.getSource().equals(jbtn)){
    FileDialog fd = new FileDialog(this);
    fd.setVisible(true);


    String a = fd.getDirectory()+fd.getFile();
    System.out.println(a);

    //System.exit(0); 
    }
}
public static void main(String[] args){
    new MyTest();

}

}

  • 写回答

2条回答 默认 最新

  • fdsafds 2009-07-13 10:34
    关注
    public void actionPerformed(ActionEvent e) {
        if (e.getSource().equals(jbtn)) {
            FileDialog fd = new FileDialog(this);
            fd.setVisible(true);
    
            String a = fd.getDirectory() + fd.getFile();
            System.out.println(a);
    
            [color=red]this.setVisible(false);[/color]
    
            // System.exit(0);
    
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?