黑马程序员技术交流社区

标题: CUI小代码 [打印本页]

作者: 途中ms前进    时间: 2015-6-1 17:50
标题: CUI小代码
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class FrameDemo
{        private Frame f;
private Button b;
private TextField tf;
private TextArea ta;
private Dialog d;
private Label l;
private Button ok;
FrameDemo(){
init();
}
public void init(){
f=new Frame();
f.setBounds(500,200,300,200);
f.setLayout(new FlowLayout());
b=new Button("转到");
tf=new TextField(50);
ta=new TextArea(20,60);
d=new Dialog(f,"提示信息_self",true);
d.setBounds(400,200,260,170);
d.setLayout(new FlowLayout());
l=new Label();
ok=new Button("确定");
d.add(ok);
d.add(l);
f.add(b);
f.add(tf);
f.add(ta);
MyEvent();
f.setVisible(true);

}
private 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);
}
});
ok.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){

ok.setVisible(true);
}
});


b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String dirPath=tf.getText();
File f=new File(dirPath);
if(f.exists()&&f.isDirectory()){
ta.setText("");
String[] names=f.list();
for(String name:names){
ta.append(name+"\r\n");

}
}
else{
String s="您输入信息错误,请重新输入或者换电脑";
l.setText(s);
d.setVisible(true);
}



}
});

}


        public static void main(String[] args)
        {
                new FrameDemo();
        }
}





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2