建立一个框架并含有文本框和文本区域,文本框录入的内容转到文本区域,并且能列出指定目录下的文件,
输入错误出现对话框提示并且有信息和按钮,而且回车也能操作对话框。
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class Test
{
public static void main(String[] args)
{
new TextFieldTextArea();
}
}
class TextFieldTextArea
{
private Frame f;
private Button b;
private TextField tf;
private TextArea ta;
private Dialog d;
private Label l;
private Button okbu;
TextFieldTextArea()
{
Init();
}
public void Init()
{f=new Frame("My Window");
f.setBounds(300,100,600,500);
f.setLayout(new FlowLayout());
b=new Button("转到");
f.add(b);
tf=new TextField(60);
f.add(tf);
ta=new TextArea(25,70);
d=new Dialog(f,"提示信息——self",true);
d.setBounds(300,150,500,260);
d.setLayout(new FlowLayout());
l=new Label();
okbu=new Button("确定");
f.add(ta);
d.add(l);
d.add(okbu);
MyEvent();
f.setVisible(true);
}
public void MyEvent()
{f.addWindowListener(new WindowAdapter(){
public void WindowClosing(WindowEvent e)
{
System.exit(0);
}
});
d.addWindowListener(new WindowAdapter(){
public void WindowClosing(WindowEvent e)
{
d.setVisible(false);
}
});
okbu.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
okbu.setVisible(false);
}
});
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{ String DirPath=tf.getText();
File Dir=new File(DirPath);
if(Dir.exists() && Dir.isDirectory())
{ ta.setText("");
String[] names=Dir.list();
for(String name:names)
{
ta.append(name+"\r\n");
}
}
else
{String info="您输入的信息:"+DirPath+"是错误的:请重输";
l.setText(info);
d.setVisible(true);
}
}
});
}
} |
|