zoukankan      html  css  js  c++  java
  • 事件监听机制——列出指定目录内容、添加Dialog对话框

    事件监听机制理解与Dialog练习

            利用Java语言,仿照我的电脑目录进行打开目录,输入文件路径,查看该路径下所有的文件,设置两个文本框,一个转到按钮,当点击转到按钮时,查看路径是否正确,若正确在第二个文本框中打开文件路径,若不正确弹出对话框(Dialog),显示提示信息,完成该设计。

    要求:

    (1)输入正确的路径,要求主窗体采用流式布局管理,实现右上角退出按钮,对于转到Button,鼠标点击可以实现,通过ENTER键也能实现。

    (2)Dialog模式为true,即当路径不正确时,Dialog对话框弹出,必须点击确定获右上角关闭按钮才能操作主窗口。

    提示:

    (1)当点击Dialog右上角关闭时,操作应为Dialog窗体隐身,通过setVisible(false)实现,而不能真的关闭System.exit(0),否则程序则会退出。

    (2)如果多次操作,应将上次的操作目录清空。


    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    class MyWindowDemo  
    {
    	private Frame f;
    	private TextField tf;
    	private Button btn;
    	private TextArea ta;
    	
    	// 对话框,操作出现错误时弹窗
    	private Dialog d;
    	private Label lab;
    	private Button okBtn;
    
    	MyWindowDemo()
    	{
    		init();
    	}
    	
    	public void init()
    	{
    		f=new Frame("my window");
    		f.setBounds(300,100,600,500);
    		f.setLayout(new FlowLayout());
    
    		tf=new TextField(60);
    		btn=new Button("转到");
    		ta=new TextArea(25,70);
    		d=new Dialog(f,"提示信息",true);//不操作,父窗体操作不了
    		d.setBounds(400,200,240,150);
    		d.setLayout(new FlowLayout());
    		
    		lab=new Label();
    		okBtn=new Button("确定");
    		
    		d.add(lab);
    		d.add(okBtn);
    
    		f.add(tf);
    		f.add(btn);
    		f.add(ta);
    		
    		myEvent();
    		f.setVisible(true);
    	}
    
    	private void myEvent()
    	{
    		okBtn.addActionListener(new ActionListener()
    		{
    			public void actionPerformed(ActionEvent e)
    			{
    				d.setVisible(false);//点击确定按钮将Dialog窗体隐身
    			}
    		});
    
    		d.addWindowListener(new WindowAdapter()
    		{
    			public void windowClosing(WindowEvent e)
    			{
    				d.setVisible(false);//点击关闭将将Dialog窗体隐身
    			}
    		});
    
    		//做一个键盘监听,ENTER键
    		tf.addKeyListener(new KeyAdapter()
    		{
    			public void keyPressed(KeyEvent e)
    			{
    				if(e.getKeyCode()==KeyEvent.VK_ENTER)
    					showDir();
    			}
    			
    		});
    
    		btn.addActionListener(new ActionListener()
    		{
    
    			public void actionPerformed(ActionEvent e)
    			{
    				//String text=tf.getText();
    				//System.out.println(text);
    				//ta.setText(text);
    				showDir();
    				
    				//tf.setText("");
    			}
    		});
    
    		f.addWindowListener(new WindowAdapter()
    		{
    			public void windowClosing(WindowEvent e)
    			{
    				System.exit(0);
    			}
    		});
    	}
    
    	private 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.setText(name+"
    ");这样的话会被替代
    				ta.append(name+"
    ");
    			}
    		}
    		else
    		{
    			String info="您输入的信息"+dirPath+"是错误的,请重输";
    			lab.setText(info);
    			d.setVisible(true);
    		}
    	}
    
    	public static void main(String[] args) 
    	{
    		new MyWindowDemo();
    	}
    }
    


    效果图:

                                   

                           


  • 相关阅读:
    【2019.7.10】树上差分 杂[LCA 倍增][树上差分 点差分 边差分]
    【luogu4145】 上帝造题的七分钟2 / 花神游历各国 [线段树]
    【luogu1198】 [JSOI2008]最大数 [线段树]
    【luogu2783】 有机化学之神偶尔会做作弊 [tarjan 缩点][LCA]
    【luogu3388】 【模板】割点(割顶)[tarjan 割点]
    【luogu2272】 [ZJOI2007]最大半连通子图 [tarjan 缩点][拓扑排序]
    【luogu2194】HXY烧情侣 [tarjan 缩点]
    【luogu3627】 [APIO2009]抢掠计划 [tarjan 缩点][最短路]
    【luogu3398】 仓鼠找sugar [LCA 倍增]
    【luogu2746】 [USACO5.3]校园网Network of Schools [tarjan 缩点]
  • 原文地址:https://www.cnblogs.com/xiangyangzhu/p/4239778.html
Copyright © 2011-2022 走看看