import java.awt.*;
import java.awt.event.*;
import java.io.*;
class FileListDemo
{
public static void main(String[] args)
{
new FileList();
}
}
class FileList extends Frame
{
private TextField tf=null;
private Button but=null;
private TextArea ta=null;
private Dialog d=null;
private Label lab=null;
private Button okBut=null;
public FileList()
{
init();
}
public void init()
{
this.setBounds(300,200,600,500);
this.setLayout(new FlowLayout());
tf=new TextField(45);
but=new Button("登录");
ta=new TextArea(25,60);
this.add(tf);
this.add(but);
this.add(ta);
addEvent();
this.setVisible(true);
}
public void initDialog()
{
d=new Dialog(this,"提示信息",true);
d.setBounds(350,250,200,150);
d.setLayout(new FlowLayout());
lab=new Label();
okBut=new Button("确定");
d.add(lab);
d.add(okBut);
dialogEvent();
d.setVisible(true);
}
public void dialogEvent()
{
d.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)//public void windowClosing(WindowEvent e)
{
d.setVisible(false);
}
});
okBut.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
d.setVisible(false);
}
});
}
public void addEvent()
{
tf.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_ENTER)
listDictory();
}
});
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)//public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
but.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
listDictory();
}
});
}
public void listDictory()
{
ta.setText("");
String directoryName=tf.getText();
File f = new File(directoryName);
if(f.exists() && f.isDirectory())
{
String[] strs=f.list();
for(String str:strs)
ta.append(str+'\r'+'\n');
}
else
{
initDialog();
}
}
}
这是JAVA关于GUI的程序,我找不出错误,总是提示空指针异常,nullPointException
|
|