import java.awt.*;
import java.awt.event.*;
import java.io.*;
class MyWindow
{
private Frame f;
private TextField tf;
private Button but,okBut;
private TextArea ta;
private Label lab;
private Dialog dia;
private MyDialog mydia;
MyWindow()
{
init();
}
public void init()
{
f = new Frame("my window");
f.setBounds(300,200,600,500);
f.setLayout(new FlowLayout());
tf = new TextField(60);//可以传入字符个数。
but = new Button("转 到");
ta = new TextArea(25,70);//可传入行和列。
dia = new Dialog(f,"错误信息",false);
dia.setBounds(400,300,280,200);
dia.setLayout(new FlowLayout());
lab = new Label();
okBut = new Button("确 定");
dia.add(lab);
dia.add(okBut);
f.add(tf);
f.add(but);
f.add(ta);
myEvent();
f.setVisible(true);
}
private void myEvent()
{
okBut.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
dia.setVisible(false);
}
});
dia.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dia.setVisible(false);
}
});
tf.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_ENTER)
{
showDir();
}
}
});
but.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
showDir();
}
});
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public void showDir()
{
String path = tf.getText();
File dir = new File(path);
ta.setText("");
if(dir.exists() && dir.isDirectory())
{
String[] names = dir.list();
for(String name : names)
{
ta.append(name+"\r\n");
}
}
else
{
//dia.setVisible(true);
String info = "输入的路径"+path+"是错误的。";
//lab.setText(info);
// mydia = new MyDialog(f,"路径错误",true,info);
MyDialog.getMyDialog(f,"路径错---误",true,info);
}
}
}
class MyDialog
{
private String message;
private Dialog d;
private Button but;
private Label lab;
private MyDialog md = new MyDialog(null,null,false,null);
private MyDialog(Frame f,String title,boolean mode,String message)
{
d = new Dialog(f,title,mode);
d.setBounds(400,300,280,200);
d.setLayout(new FlowLayout());
but = new Button("自定义确定");
lab = new Label(message);
d.add(lab);
d.add(but);
myEvent();
d.setVisible(true);
}
public static MyDialog getMyDialog()
{
return md;
}
public void setTitle(String message)
{
this.mesage =message;
}
private void myEvent()
{
but.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
d.setVisible(false);
}
}
}
}