您的位置:首页 > 产品设计 > UI/UE

黑马程序员—GUI(菜单)小例子

2013-12-14 19:58 495 查看
------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------
package twenty_two;

import java.awt.*;

import java.io.*;

import java.awt.event.*;

public class Ten {

public static void main(String[] args) {

new Mwinn();

}

}

class Mwinn

{

private Frame f;

private TextField tf;

private Button but;

private Button okbut;

private TextArea ta;

private Label lae;

private Dialog dg;

Mwinn()

{

init();

}

public void init()

{

f=new Frame("中");

f.setBounds(300,200,500,999);

f.setLayout(new FlowLayout());

tf=new TextField(30);

but=new Button("转到");

ta=new TextArea(15,40);

dg=new Dialog(f,"提示信息—self",true);

dg.setBounds(500,200,300,160);

dg.setLayout(new FlowLayout());

lae=new Label();

okbut=new Button("确定");

dg.add(lae);

dg.add(okbut);

f.add(tf);

f.add(but);

f.add(ta);

mevent();

f.setVisible(true);

}

private void mevent()

{

okbut.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

dg.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();

}

});

dg.addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{

dg.setVisible(false);

}

});

f.addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{

System.exit(0);

}

});

}

private void showDir()

{

String dirPath=tf.getText();

File dir=new File(dirPath);//D:\\java\\workspace\\heima\\src\\twenty_two

if(dir.exists()&&dir.isDirectory())

{

ta.setText("");//不加它两次目录的东西都会存在。

String[]names=dir.list();

for(String name :names)

{

ta.append(name+"\r\n");//将所有的文件都输出来。

}

}

else

{

String info="您的路径为:"+ dirPath+"这是错误的路径";

lae.setText(info);

dg.setVisible(true);

}

}

}

------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: