flex的panel自定义titleBar

方式有二:

1)来源于网上:建立mxml格式组件。注,不能:直接将下列片段放主应用程序文件,原因是在主应用程序中this是主应用程序的引用,不知道还有其它方式引用否(var panel:Panel=event.target as Panel;//获取不到,不知为什么)。

<?xml version="1.0" encoding="utf-8"?>
<mx:Panel  xmlns:fx="http://ns.adobe.com/mxml/2009" 
		   xmlns:s="library://ns.adobe.com/flex/spark" 
		   xmlns:mx="library://ns.adobe.com/flex/halo" layout="vertical" creationComplete="init();"
	removedEffect="Fade" styleName="popUpPanel"  borderColor="#5C5C5C"
	width="150" height="150">

	<fx:Script>
		<![CDATA[
			import mx.controls.Button;
			import mx.containers.HBox;
			import mx.controls.Image;
			import mx.managers.PopUpManager;
			import mx.controls.Alert;
			

			
			private function init():void{

				var button:Button = new Button();
				button.width=16;
				button.height=16;
				button.addEventListener(MouseEvent.CLICK, buttonClick);

				//Create Hbox to add to title bar
				var titleHbox:HBox = new HBox();
				titleHbox.width=this.titleBar.width;
				titleHbox.height=this.titleBar.height;
				//Add image and button to HBox
				titleHbox.addChild(button);
				//Add Hbox with button and image
				this.titleBar.addChild(titleHbox);
				//center popup panel
			}
	
			//Function called when button is clicked
			private function buttonClick(event:Event):void{
				Alert.show('Button clicked now closing');
				PopUpManager.removePopUp(this);
			}
		]]>
	</fx:Script>
	
</mx:Panel>

 

2)直接扩展Panel,编译的速度很慢,

package module.procurement.components
{
	import mx.containers.Panel;
	import mx.controls.LinkButton;
	import flash.events.MouseEvent;
	import mx.core.UIComponent;
	
	public class PanelMy extends Panel
	{
		var topBar:UIComponent;
		public function PanelMy()
		{
			super();
		}
		
		
		private var maxClick:LinkButton;
		
		
		
		//下面的两个重载过的函数会在视图生成的时候自动执行
		
		override protected function createChildren():void
		{
			super.createChildren();
			
			maxClick = new LinkButton;
			maxClick.label = "Maximize";
			maxClick.addEventListener(MouseEvent.CLICK, toMaxHandler);
			titleBar.addChild(maxClick);
			
		}
		
		override protected function updateDisplayList (unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			
			maxClick.setActualSize( maxClick.getExplicitOrMeasuredWidth(),
				maxClick.getExplicitOrMeasuredHeight() );
			
			var y:int = 4;
			var x:int = this.width - maxClick.width - 12;
			maxClick.move(x, y);
			
		} 
		
		
		//点击linkbutton后的触发函数
		private function toMaxHandler(event:MouseEvent):void
		{
			if(maxClick.label == "Maximize")
			{
				//dispatchEvent(new Event("maxTalkBrowse"));
				maxClick.label = "Minimize";
			}
			else if(maxClick.label == "Minimize")
			{
				//dispatchEvent(new Event("miniTalkBrowse"));
				maxClick.label = "Maximize";
			}
		}
		

	}
}

 

 

后记:终于发现了java程序员用flex的好处了,很容易入手。用它做前台界面,和java简直绝配,给公司省不少钱。扫了下javaFX,发现用它必须身兼数职,这不是要程序员的老命么

你可能感兴趣的:(xml,Flex,Flash,Adobe,JavaFX)