本人初学者,我知道格式,模式什么的都写的不好,只是想简单实现一下功能。但,怎么我的dialog对话框关闭不起作用呢,请各位帮忙看看。
import java.io.*;
import java.awt.*;
import java.awt.event.*;
class Test
{
Frame f = new Frame("title");
TextField tf = new TextField(37);
Button b = new Button("转到");
Button b1 = new Button("确定");
TextArea ta = new TextArea(13,50);
Dialog d = new Dialog(f,true);
Label lb = new Label(); public void frame()
{
f.setBounds(300,200,500,300);
f.setLayout(new BorderLayout());
Panel p1 = new Panel();
Panel p2 = new Panel();
p1.setLayout(new FlowLayout());
p1.add(tf);
p1.add(b);
p2.setLayout(new FlowLayout());
p2.add(ta,BorderLayout.SOUTH);
f.add(p1,BorderLayout.NORTH);
f.add(p2,BorderLayout.SOUTH);
f.setVisible(true); f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}); b.addActionListener(new MyActionListener());
} public void dialog()
{
d.setBounds(370,260,270,100);
d.setLayout(new FlowLayout());
d.add(b1);
d.add(lb);
d.setVisible(true);

d.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
d.setVisible(false);
}
});

b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
d.setVisible(false);
}
});
} public static void main(String[] args) throws Exception
{
Test t = new Test();
t.frame();
} public void list(File f)
{
File[] files = f.listFiles();
for(File file: files)
{
ta.append(file.toString());
ta.append("\r\n");
}
} class MyActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String file = tf.getText();
File f = new File(file);
ta.setText("");
if(!f.isDirectory())
dialog();
else
list(f);
}
}
}

解决方案 »

  1.   

    dialog() 里边,把d.setVisible(true);写在最后边就行了你先显示窗口,后边的语句还木有执行哩。
     public void dialog(){    
            d.setBounds(370,260,270,100);
            d.setLayout(new FlowLayout());
            d.add(b1);
            d.add(lb);         
            d.addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent e)
                {
                    d.setVisible(false);
                }
            });
             
            b1.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e)
                {
                    d.setVisible(false);
                }
            });
            d.setVisible(true);
        }
      

  2.   

    嗯, 可以关了,但 frame()里setVisible()写在前面怎么没事啊。
      

  3.   


    dialog顾名思义,是一个对话框,是需要交互的,他要等你一个反馈,他跟据你的反馈来进行下一步。你可以还按你原先的写,但是在显示前加一句“d.setModal(false);”
      

  4.   

    Dialog 也是 Window ,当然可以使用 WindowListener
      

  5.   

    dispose()
      

  6.   

    使用dispose() serVisible只是隐藏了并不会释放掉
      

  7.   

    这个保证好使: $.imDialog.close();
      

  8.   


            f.addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent e)
                {
                   // System.exit(0);
                   d.dispose();
                }
            });