黑马程序员技术交流社区

标题: GUI开始简析三 [打印本页]

作者: 奋发吧小白    时间: 2014-8-25 08:28
标题: GUI开始简析三
需求:实现如下窗口,功能:能够转到相应的盘符下显示该盘内的内容
[java] view plaincopy
<span style="font-size:18px;">import java.awt.*;  
import java.awt.event.*;  
import java.io.*;  
class MyWindows   
{  
    private Frame f;  
    private TextField tf;  
    private Button b ;  
    private TextArea ta;  
      
    MyWindows()  
    {  
        init();  
    }  
    private void init()  
    {  
        f = new Frame("我的窗口");  
        f.setBounds(300,100,600,500);  
        f.setLayout(new FlowLayout());  
  
        tf = new TextField(50);  
        b = new Button("转到");  
        ta = new TextArea(25,70);  
  
        f.add(tf);  
        f.add(b);  
        f.add(ta);  
  
        myEvent();  
        f.setVisible(true);  
    }  
      
    private void myEvent()  
    {  
               
        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");  
                    }  
                }  
            }  
        });  
        f.addWindowListener(new WindowAdapter()  
        {  
              public void windowClosing(WindowEvent e)  
            {  
                System.exit(0);  
            }  
        });  
    }  
    public static void main(String[] args)   
    {  
        new MyWindows();  
    }  
}  
</span>  
作者: 许庭洲    时间: 2014-8-25 09:14
值得学习ing!




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