day22/MyWindowDemo.java

来源:互联网 发布:写轮眼隐形眼镜淘宝 编辑:程序博客网 时间:2024/06/04 19:04
/*列出指定目录内容弹出错误提示对话窗口。Dialog d = new Dialog(f,"提示信息-self",true);为true时,错误提示窗口不关闭,所属的Frame就不能动。*/import java.awt.*;import java.awt.event.*;import java.io.*;class MyWindowDemo {private Frame f;private Button but;private TextField tf;private TextArea ta;private Dialog d;private Button okBut;private Label lab;MyWindowDemo(){init();}public void init(){f=new Frame("我的电脑");f.setBounds(300,100,600,500);f.setLayout(new FlowLayout());tf=new TextField(50);f.add(tf);but=new Button("转到");f.add(but);ta=new TextArea(25,60);f.add(ta);//-------------弹出的对话框d = new Dialog(f,"提示信息-self",true);d.setBounds(450,200,250,100);d.setLayout(new FlowLayout());okBut =  new Button("确认");lab = new Label();d.add(lab);d.add(okBut);//---------------------myEvent();f.setVisible(true);}private void myEvent(){//对话框窗体d.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){d.setVisible(false);}});//对话框上的按钮okBut.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){d.setVisible(false);}});//-----------------------//主窗体按钮活动监听//文本框里输完后,点击按钮,列出指定目录but.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){showDir();}});//文本框里面输完后回车键,列出指定目录tf.addKeyListener(new KeyAdapter(){public void keyPressed(KeyEvent e){if(e.getKeyCode()==KeyEvent.VK_ENTER){showDir();}}});f.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});}public void showDir(){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 = "输入的路径"+tf.getText()+"是错误,请重新输入";lab.setText(info);d.setVisible(true);}}public static void main(String[] args) {new MyWindowDemo();}}

0 0
原创粉丝点击