黑马程序员--Java基础总结--GUI

---------------------- ASP.Net+Unity开发.Net培训、期待与您交流! ----------------------

GUI:Graphicla User Interface 图形用户接口
CLI:Commad Line User Interface 命令行用户接口。
java.Awt :Abstrct Window ToolKit 抽象窗口工具包,需要调用本地系统方法实现功能。跨平台性稍差。属于重量级控件。系统依赖性比较强。
javax.Swing:在AWT的基础上,建立的一套图形界面系统。跨平台性强。
图形化界面是单独线程执行。
事件监听机制:特点:1、事件源(就是AWT包或者swing包中那些图形界面组件)2、事件(每一个事件源都有自己特有的对应事件和共性事件) 3、监听器(将可以触发某一个事件的动作都已经封装到监听器中)4、事件处理()。1-3在java中已经定义好,直接获取对象就可以用了,4是我们要做的。

类似小的记事本程序代码:

public class MyMenuDemo {
	private Frame f;
	private MenuBar mb;
	private Menu m,subMenu;
	private MenuItem closeItem,subItem,openItem,saveItem;
	private TextArea ta;
	private File file;
	
	private FileDialog openDia,saveDia;
	
	MyMenuDemo()
	{
			init();
	}
	public void init(){
		f=new Frame("my window");
		f.setBounds(300,100,600,500);
		//f.setLayout(new FlowLayout());不设置 为了是文本框全部填充进去。美观
		
		mb=new MenuBar();
		m=new Menu("文件");
		subMenu=new Menu("子菜单");
		openItem=new MenuItem("打开文件");
		saveItem=new MenuItem("保存文件");	
		subItem=new MenuItem("子条目");
		closeItem=new MenuItem("退出");
		ta=new TextArea();
		      m.add(openItem);
		      m.add(saveItem);				
				//m.add(subItem);
				m.add(subMenu);
				subMenu.add(subItem);
				mb.add(m);
				f.setMenuBar(mb);
				m.add(closeItem);
				openDia=new FileDialog(f,"我要打开",FileDialog.LOAD);//调用Filedialog中自带的打开保存对话框。
				saveDia=new FileDialog(f,"我要保存",FileDialog.SAVE);
				f.add(ta);		
				
				myEvent();
				
				f.setVisible(true);
				
	}
	 private void myEvent(){
		 
		 openItem.addActionListener(new ActionListener(){//监听打开菜单。
			 public void actionPerformed(ActionEvent e){
				 openDia.setVisible(true);
				 String dirPath=openDia.getDirectory();//获取文档路径
				 String fileName=openDia.getFile();
				 if(dirPath==null|| fileName==null)//判断 如果为空 则返回
					 	return;
				 ta.setText("");
				 File file=new File(dirPath,fileName);
				 try{
					 BufferedReader bufr=new BufferedReader(new FileReader(file));//读取文件中内容
					 String line=null;
					 while((line =bufr.readLine())!=null){
						 ta.append(line+"\r\n");//添加到文本区域。
					 }
				 }
				 catch(IOException ex){
					 throw new RuntimeException("读取失败");
				 }
			 }
		 });
		 
		 
		 saveItem.addActionListener(new ActionListener(){//监听保存菜单
			 public void actionPerformed(ActionEvent e){	
				 if(file==null){//如果文件为空,则弹出保存菜单。
					 saveDia.setVisible(true);
					 String dirPath=saveDia.getDirectory();
					 String fileName=saveDia.getFile();
					 if(dirPath==null|| fileName==null)
						 	return;
					 file=new File(dirPath,fileName);//创建文件
				 }
				 try{
					 BufferedWriter bufw=new BufferedWriter(new FileWriter(file));
					 String text=ta.getText();
					 bufw.write(text);//循环写入,保存文件。
					 bufw.close();
				 }
				 catch(IOException ex){
					 throw new RuntimeException();
				 }
				
				 
				 
			 }
		 });
		 
		 f.addWindowListener(new WindowAdapter(){
			 public void windowClosing(WindowEvent e){
				 System.exit(0);
			 }
		 });
	 }
	 
	 public static void main(String [] args){
		 new MyMenuDemo();
	 }
}
监听键盘及鼠标的程序代码:

public class MouseandKeyEvent {
	 private Frame f;
	 private Button but;
	 private TextField tf;
	 MouseandKeyEvent()
	 {
		 init();
	 }
	 public void init(){
		 f=new Frame("my frame");
		 f.setBounds(300, 100, 600, 500);
		 f.setLayout(new FlowLayout());
		 tf=new TextField(20);
		 but=new Button("my button");
		 f.add(but);
		 f.add(tf);
		 myEvent();
		 f.setVisible(true);
	 }

	 private void myEvent(){
		 f.addWindowListener(new WindowAdapter(){
			 public void windowClosing(WindowEvent e){
				 System.exit(0);
			 }
		 });
		 but.addActionListener(new ActionListener(){
			 public void actionPerformed(ActionEvent e){
				 System.out.println("action ok");
			 }
			 
		 });
		 
		 
		 
		 //让按钮具备退出程序功能。添加活动监听。
		but.addMouseListener(new MouseAdapter(){
			public void mouseEntered(MouseEvent  e){
				System.out.println("鼠标进入");
			}
			public void mouseClicked(MouseEvent e){
				if(e.getClickCount()==2)//双击动作。
					System.out.println("鼠标点击");
			}
		}
		);
		//添加键盘监听事件
		but.addKeyListener(new KeyAdapter(){
			public void keyPressed(KeyEvent e){
				if(e.getKeyCode()==27){//if(e.getKeyCode==KEYEvent.VKENTER)
					System.exit(0);
				}
				System.out.println(e.getKeyChar()+"::"+e.getKeyCode());
				System.out.println(e.getKeyText(e.getKeyCode())+"::"+e.getKeyCode());
			}
			
		});
		//添加文本框事件
		tf.addKeyListener(new KeyAdapter(){
			public void keyPressed(KeyEvent e){
				int code=e.getKeyCode();
				if(!(code>=KeyEvent.VK_0&&code<=KeyEvent.VK_9)){
					System.out.println(code+"非法字符");
					e.consume();
				}
					
			}
		});
		
		
	 }
	 public static void main(String [] args){
		 new MouseandKeyEvent();
	 }
	 

}




---------------------- ASP.Net+Unity开发.Net培训、期待与您交流! ----------------------

详细请查看:http://edu.csdn.net

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值