黑马程序员--- 学习笔记(第二十二天)

 —————————— ASP.Net+Android+IOS开发.Net培训、期待与您交流!——————————
 GUI(Graphical User Interface)图形化用户接口
 CLI(Command Line User Interface)命令行用户接口


java为GUI提供的对象都在java.awt和java.swing两个包中


java.awt:(abstract window toolkit(抽象窗口工具包))
需要调用本地系统方实现功能,是重量级控件
java.swing:在awt的基础上,建立的一套图形界面系统,其中
提供了更多的组件,而且完全由java实现,增加移植性,属于轻量级
控件


Component组件
container:容器,是一个特殊的组件,该组件可以通过add方法添加
其他组件进来


容器中组件的排版方式就是布局:
常见的布局管理器:
FlowLayout:流式布局 默认居中
.从左到右的顺序排列
.Panel默认的布局管理器
BorderLayout:边界式布局 默认居中
.东南西北中
.Frame默认的布局管理器
GridLayout:网格布局
.规则的矩阵
CradleLayout:卡片布局
.选项卡
GridBagLayout:网格包布局
.非规则的矩阵


最厉害的是坐标式布局, Eclipse可以安装插件轻松布局


cmd命令:
md 目录名  //创建目录
rd 目录名  //删除目录


创建图形化界面步骤:
1.创建Frame窗体
2.对窗体进行基本设置(比如大小,位置,布局等)
3.定义组件
4.将组件通过窗体的add方法添加到窗体中
5.让窗体显示


事件监听机制
事件源(组件)
事件(Event)
监听器(Listener)
事件处理(引发事件后的处理方式)




抽象类没有抽象方法,该类只是为了不让创建对象,
因为创建对象没有意义.


窗体事件


事件和图形化界面分离出来




按钮事件
/*
按钮就是事件源
那么选择哪个监听器呢?
通过关闭窗体示例了解到,想要知道哪个组件具备什么样的监听器
需要查看该组件对象的功能


通过查询Button的描述,发现按钮支持一个特有的监听addActionListener


*/


鼠标事件


键盘事件

/*
创建图形化界面步骤:
1.创建Frame窗体
2.对窗体进行基本设置(例如大小,位置,布局方式等)
3.定义组件
4.将组件通过窗体的add方法添加到窗体中
5.让窗体显示

 让事件和图形化界面分离出来

 窗体事件

按钮事件

鼠标事件

键盘事件

*/
import java.awt.*;
import java.awt.event.*;
//窗体类
class MyFrame  extends Frame
{
	//定义该窗体中所需的控件
	private Button b;
	//构造函数 初始化窗体
	public MyFrame(){
		init();
	}
	//初始化
	private void init(){
	   //setBounds(x,y,w,h);==setSize(w,h)+setLacation(x,y);

	   //设置组件基本属性
		 //Frame
		 setTitle("my first program");   //设置标题
		 setBounds(300,200,500,400);//设置位置以及大小
		 setLayout(new FlowLayout());//设置布局
		 //Button
		 b=new Button("first button");
	   //添加控件
		 add(b);//添加组件

	   //调用事件方法
		windowEvent();
		buttonEvent();
		mouseEvent();
		keyEvent();
	   //显示窗体
	   setVisible(true);
	}
	//窗体事件
	private void windowEvent(){
		addWindowListener(new WindowAdapter(){
//			void windowActivated(WindowEvent e) 
//          激活窗口时调用。 
		public void windowActivated(WindowEvent e){
		   sop("我活动了");
		}
//		 void windowClosing(WindowEvent e) 
//          窗口正处在关闭过程中时调用。 
		public void windowClosing(WindowEvent e){
			sop("正在关闭");
			System.exit(0);
			
		}
	//	 void windowOpened(WindowEvent e) 
       //   已打开窗口时调用。 
	   public void windowOpened(WindowEvent e){
			sop("窗口打开");
	   }
		
		});
	}
	//按钮事件
	private void buttonEvent(){
	   b.addActionListener(new ActionListener(){
		   //按钮活动事件
	   	public void actionPerformed(ActionEvent e){
		
		//   	   sop("按钮关闭");
		 //  System.exit(0);
	
		}
	   });
		
	}
	//鼠标事件
	private  void mouseEvent(){
	
		b.addMouseListener(new MouseAdapter(){
	   			//鼠标移进
			public void mouseEntered(MouseEvent e){
				sop("鼠标移动进按钮了");
			}
			//鼠标单机
			public void mouseClicked(MouseEvent e){
				//getClickCount判断 实现双击
				if(e.getClickCount()==2)
					sop("鼠标被双击");
				else
				sop("鼠标被单机");

			}

	   
	   });
	}
	//键盘事件
	private void keyEvent(){
		b.addKeyListener(new KeyAdapter(){
			//键盘按下
			public void keyPressed(KeyEvent e){
				//输出文本形式的键  和键码
				sop(KeyEvent.getKeyText(e.getKeyCode())+" "+e.getKeyCode());
			//	 boolean isControlDown() 
			//返回 Control 修饰符在此事件上是为 down。 
				//组合键
				if(e.isControlDown() && e.getKeyCode()==KeyEvent.VK_B)
					sop("按下了ctrl+b  组合键");
			}
		});
	}
	private void sop(Object o){
		System.out.println(o);
	}
}
//程序运行入口
class Demo1  
{
	public static void main(String[] args) 
	{
		new MyFrame();
	}
}




/*
练习 文本框只能输入数字,要使用consume()方法
*/

/*
  练习 文本框只能输入数字
*/
import java.awt.*;
import java.awt.event.*;
class MyFrame extends Frame
{
	private TextField  tf;
	public MyFrame(){
		init();
	}
	private void init(){
	   setBounds(300,200,500,400);
	   setLayout(new FlowLayout());
	   setTitle("文本框输入练习");
	   tf=new TextField(20);

	   add(tf);
	   
	   keyEvent();
	   windowEvent();

	   setVisible(true);
	}
	//窗体事件
	private void windowEvent(){
		this.addWindowListener(new WindowAdapter(){
		   public void windowClosing(WindowEvent e){
				System.exit(0);
		   }
		});
	}
	//键盘事件
	private void keyEvent(){
		tf.addKeyListener(new KeyAdapter(){
			//键盘按下
			public void keyPressed(KeyEvent e){
				int code=e.getKeyCode();
				//判断是否在0-9
				if(!(code>=0x30 && code<=0x39))
					e.consume();//不是的话就不处理
			}
		});
	}
}
class Demo2 
{
	public static void main(String[] args) 
	{
		new MyFrame();
	}
}




对话框(Dialog)


/*
练习 输入目录,列出目录的列表
*/


/*
文本框输入目录 转到

文本域输出列表
*/
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class MyFrame extends Frame
{
	private TextField tf; //文本框
	private Button b;		 //转到按钮
	private TextArea ta;		   //文本域
	private Dialog d;	//对话框
	private Label l;		 //标签
	private Button sure;		 //确定按钮
	public MyFrame(){
		super("输出文件列表");
		init();
	}
	//初始化
	private void init(){
		setBounds(300,200,500,500);
		setLayout(new FlowLayout());

		tf=new TextField(50);
		b=new Button("转到");
		ta=new TextArea(28,60);
		l=new Label();
		sure=new Button("确定");

		d=new Dialog(this,"温馨提示:",true);
		d.setBounds(400,400,300,100);
		d.setLayout(new FlowLayout());
		d.add(l);
		d.add(sure);

		add(tf);
		add(b);
		add(ta);
		 
		 windowEvent();
		 keyEvent();
		 buttonEvent();

		setVisible(true);
		
	}
	//窗体事件
	private void windowEvent(){
		this.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				System.exit(0);
			}
		});
		d.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				d.setVisible(false);
			}
		});
	}
	//键盘事件
	private void keyEvent(){
		tf.addKeyListener(new KeyAdapter(){
			public void keyPressed(KeyEvent e){
			   if(e.getKeyCode()==10)
				   showDir();
			}
		});
	}
	//按钮事件
	private void buttonEvent(){
	   b.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				showDir();
			}
	   });
	   sure.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				d.setVisible(false);
			}
	   });
	}
	//展示目录
	private void showDir(){
		String filepath=tf.getText();
		File file=new File(filepath);
		if(file.exists() && file.isDirectory()){
			ta.setText("");
			tf.setText("");
			String []strs=file.list();
			for (String s : strs )
			{
				ta.append(s+"\r\n");
			}
		}else{
			l.setText("该目录"+filepath+"不存在,请重新输入");
			tf.setText("");
			d.setVisible(true);
		}
	}
}
class Demo3 
{
	public static void main(String[] args) 
	{
		new MyFrame();
	}
}





菜单:
MenuBar 菜单栏
Menu  菜单
MunuItem 菜单项


菜单是菜单项的子类


文件对话框(FileDialog)


/*
打开文件

保存文件

简单记事本程序 
*/
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class MyFrame extends Frame
{
	private MenuBar filemenu;
	private Menu file;
	private MenuItem open;
	private MenuItem save;
	private Menu zicaidan;
	private MenuItem zicaidanxiang;
	private FileDialog of;
	private FileDialog sf;
	private TextArea ta;
	private Dialog tishi;
	private Label l;
	private String path;
	private String filename;
	public MyFrame(){
		super("打开与保存文件");
		init();
	}
	//初始化
	private void init(){
		 setBounds(300,200,500,500);

		 filemenu=new MenuBar();
		 file=new Menu("File");
		 open=new MenuItem("open");
		 save=new MenuItem("save");
		 zicaidan=new Menu("子菜单");
		 zicaidanxiang=new MenuItem("子菜单项");
		 ta=new TextArea(30,60);
		 tishi=new Dialog(this,"温馨提示",true);
		 l=new Label();
		 tishi.setBounds(300,200,300,100);
		 tishi.add(l);
		 file.add(open);
		 file.add(save);
		 file.add(zicaidan);
		 zicaidan.add(zicaidanxiang);
		 this.setMenuBar(filemenu);
		 filemenu.add(file);
		 
		  windowEvent();
		  menuItemEvent();
		 add(ta);
		 setVisible(true);
	}
	private void windowEvent(){
		addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				System.exit(0);
			}
		});
		tishi.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				tishi.setVisible(false);
			}
			});
	}
	private void menuItemEvent(){
		//打开
		open.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				of=new FileDialog(MyFrame.this,"打开文件",FileDialog.LOAD);
				of.setBounds(300,200,400,400);
				of.setVisible(true);
				 path=of.getDirectory();
				 filename=of.getFile();
				if(path!=null || filename!=null)
						read();
			}
		});
		//保存
		save.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				
				if(of!=null){
					write();
				}else{
					if(sf==null){
						sf=new FileDialog(MyFrame.this,"保存文件",FileDialog.SAVE);
						sf.setBounds(300,200,400,400);
						sf.setVisible(true);
						path=sf.getDirectory();
						filename=sf.getFile();
						if(path!=null && filename!=null)
								write();
					}else{
						path=sf.getDirectory();
						filename=sf.getFile();
						if(path!=null && filename!=null)
								write();
					}
				}
			}
		});
	}
	//读取
	private void read(){
	   File f=new File(path,filename);
	   BufferedReader br=null;
	   ta.setText("");
	   try
	   {
			br=new BufferedReader(new FileReader(f));
			String line=null;
			while((line=br.readLine())!=null){
				ta.append(line+"\r\n");
			}
	   }
	   catch (IOException o)
	   {
		  	l.setText("读取失败");
			tishi.setVisible(true);
	   }
	   finally
	   {
			 try
		   {
				if(br!=null)
					br.close();
		   }
		   catch (IOException e)
		   {
				l.setText("关闭流异常");
				tishi.setVisible(true);
		   }
	   }
	}
	//写入
	private void write(){
	   File f=new File(path,filename);
	   BufferedWriter br=null;
	   try
	   {
			br=new BufferedWriter(new FileWriter(f));
			String text=ta.getText();
			br.write(text);
			br.flush();
	   }
	   catch (IOException o)
	   {
		  	l.setText("读取失败");
			tishi.setVisible(true);
	   }
	   finally
	   {
			 try
		   {
				if(br!=null)
					br.close();
		   }
		   catch (IOException e)
		   {
				l.setText("关闭流异常");
				tishi.setVisible(true);
		   }
	   }
	}
}
class Demo4 
{
	public static void main(String[] args) 
	{
		new MyFrame();
	}
}





双击执行jar程序


创建个清单文件list.txt
写入 Main-Class:空格包名.类名回车
注意空格,回车必须要
然后jar -cvfm jar名称 list.txt 类名


电脑系统盘目录-工具-文件夹选项-文件类型-jar-高级
-新建open -值为安装目录的javaw.exe目录 -jar(注意后面的-jar 
是在exe的后面空格隔开)

 —————————— ASP.Net+Android+IOS开发.Net培训、期待与您交流!——————————
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值