A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

代码如下:
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.io.*;

  4. class  DialogDemo
  5. {
  6.         private Frame f;
  7.         private Button but;
  8.         private TextArea ta;
  9.         private TextField tf;
  10.         private Dialog dia;
  11.         private Label lab;
  12.         private Button diaBut;
  13.         private Label warning;

  14.         DialogDemo(){
  15.                 init();
  16.         }

  17.         private void init(){
  18.                 f=new Frame("My first GUI Program");

  19.                 f.setBounds(300,200,600,500);
  20.                 f.setLayout(new FlowLayout());
  21.                 but= new Button("Open Button");
  22.                 ta=new TextArea(20,70);
  23.                 ta.setEditable(false);//设置文本区域不可以输入
  24.                 tf=new TextField(60);
  25.                 warning=new Label("作者:XXX  Version:v1.0  Date:2014\\05\\07");
  26.                

  27.                 dia =new Dialog(f,true);
  28.                 diaBut=new Button("确定");
  29.                 lab=new Label();

  30.                 f.add(tf);
  31.                 f.add(but);
  32.                 f.add(ta);
  33.                 f.add(warning);
  34.                 f.setVisible(true);
  35.                 myEvent();
  36.         }
  37.        
  38.         /*
  39.         按钮一般增加addActionListener 事件监听
  40.         */
  41.         private void myEvent(){//监听器
  42.                 but.addActionListener(new ActionListener(){//为button增加事件监听
  43.                         public void actionPerformed(ActionEvent e){
  44.                                 //
  45.                                 show();
  46.                         }
  47.                 });
  48.                 tf.addKeyListener(new KeyAdapter(){
  49.                         public void keyPressed(KeyEvent e){//为TextField增加键盘监听
  50.                                 if(e.getKeyCode()==KeyEvent.VK_ENTER)
  51.                                         show();
  52.                         }
  53.                 });
  54.                
  55.                 diaBut.addActionListener(new ActionListener(){//为Dialog的按钮增加事件监听
  56.                         public void actionPerformed(ActionEvent e){
  57.                                 dia.setVisible(false);
  58.                         }
  59.                 });
  60.                 /*
  61.                 dia.addWindowListener(new WindowAdapter(){//dialog窗口增加关闭监听
  62.                         public void windowClosing(WindowEvent e){
  63.                                 dia.setVisible(false);
  64.                         }
  65.                 });
  66.                 f.addWindowListener(new WindowAdapter(){//frame窗口增加关闭监听
  67.                         public void windowClosing(WindowEvent e){
  68.                                 System.exit(0);
  69.                         }
  70.                 });
  71.                 */
  72.                 closeListener(dia);//对dialog和frame进行关闭监听
  73.                 closeListener(f);
  74.         }
  75.         public void show(){//TextArea打印  文件列表
  76.                 String dirPath=tf.getText();

  77.                 File dir =new File(dirPath);
  78.                
  79.                 if(dir.exists()&&dir.isDirectory()){
  80.                         ta.setText("");
  81.                         String[]names =dir.list();
  82.                         for(String name:names){
  83.                                 ta.append(name+"\r\n");
  84.                         }
  85.                 }
  86.                 else
  87.                 {
  88.                         //在需要dialog的时候才用
  89.                         Rectangle r=f.getBounds();
  90.                         dia.setBounds(r.x+50,r.y+50,r.width-100,r.height-400);
  91.                         dia.setLayout(new FlowLayout());
  92.                         dia.add(lab);
  93.                         dia.add(diaBut);
  94.                         String info="同志您好!您输入的"+dirPath+"是错误的!请重输!";
  95.                         lab.setText(info);
  96.                         dia.setVisible(true);
  97.                 }
  98.         }
  99.         public void closeListener(Component c){//组件关闭方法的封装
  100.                 if(c instanceof Frame){
  101.                         ((Frame)c).addWindowListener(new WindowAdapter(){
  102.                                 public void windowClosing(WindowEvent e)
  103.                                 {
  104.                                         System.exit(0);
  105.                                 }
  106.                         });
  107.                 }
  108.                 else if(c instanceof Dialog){
  109.                         ((Dialog)c).addWindowListener(new WindowAdapter(){
  110.                                 public void windowClosing(WindowEvent e)
  111.                                 {
  112.                                         c.setVisible(false);
  113.                                 }
  114.                         });
  115.                 }
  116.         }
  117.         public static void main(String[] args)
  118.         {
  119.                 new DialogDemo();
  120.                 //System.out.println("Hello World!");
  121.         }
  122. }
复制代码

运行结果如图:




优化了关闭的事件监听!
closeListener(dia)
closeListener(f);
还有这里的TextArea要设置成不可编辑ta.setEditable(false);//设置文本区域不可以输入。
还有Dialog的位置  设置 取决于Frame的位置!
Rectangle r=f.getBounds();
dia.setBounds(r.x+50,r.y+50,r.width-100,r.height-400);


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马