1. JList
JList类似于flex的dataGroup控件,可以自己定义循环的内容
构造函数为:
JList(listData:*=null, cellFactory:ListCellFactory=null)
listData
数据源,可以是一个VectorListModel或Array
cellFactory
列表的CellFactory,一般调用GeneralListCellFactory
GeneralListCellFactory的构造函数是:
GeneralListCellFactory(listCellClass:Class, shareCelles:Boolean=true, sameHeight:Boolean=true, height:int=22)
listCellClass
实现ListCell接口的渲染器类,类似于dataGroup的itemRenderer,需要实现4个接口方法:
function setCellValue(v:*):void -- 设置item的显示内容,这里的参数v即为该item对应的数据
function getCellValue():* -- 获取item的相关内容,一般简单返回数据内容即可
function setListCellStatus(list:JList, isSelected:Boolean, index:int):void -- 设置item选中/未选中的事件
function getCellComponent():Component -- 返回item对应的组件,这里的返回值就是最终的item呈现内容
shareCelles
是否共享格子内容,默认为true(共享),就是当有多项的时候,只加载可见区域里的内容,当用下拉框滚动内容时,用共享的格子再来加载下一部分内容;设置为false就是加载所有内容,这样加载时间会长一点,但是可以避免共享格子时带来的闪动
sameHeight
所有的格子是否拥有同样的高度,默认为true
height
格子的高度,只有当sameHeight设置为true时才有效,默认为22
JList有两个很有用的方法:setVisibleCellWidth(设置格子宽度)和setVisibleRowCount(设置可见行数)
{
import flash.display.Sprite;
import org.aswing.AsWingManager;
import org.aswing.FlowLayout;
import org.aswing.GeneralListCellFactory;
import org.aswing.JList;
import org.aswing.JScrollPane;
import org.aswing.JWindow;
import org.aswing.VectorListModel;
import org.aswing.border.LineBorder;
public class Main extends Sprite
{
public function Main()
{
AsWingManager.initAsStandard(this);
var model:VectorListModel = new VectorListModel();
for(var j:int=1;j<10;j++)
{
var item:MyItem = new MyItem("条目"+j,"pic00"+j+".jpg");
model.append(item);
}
var list:JList = new JList(model,new GeneralListCellFactory(MyRenderer,false,true,60));
list.setVisibleCellWidth(180);
list.setVisibleRowCount(6);
//容器
var window:JWindow = new JWindow();
window.setSizeWH(400,400);
window.setLocationXY(100,100);
window.setBorder(new LineBorder());
window.getContentPane().setLayout(new FlowLayout());
window.getContentPane().append(new JScrollPane(list));
window.show();
}
}
}
{
import flash.events.Event;
import flash.net.URLRequest;
import org.aswing.ASColor;
import org.aswing.AssetPane;
import org.aswing.Component;
import org.aswing.FlowLayout;
import org.aswing.JLabel;
import org.aswing.JList;
import org.aswing.JLoadPane;
import org.aswing.JPanel;
import org.aswing.ListCell;
import org.aswing.geom.IntDimension;
public class MyRenderer implements ListCell
{
private var value:*;
private var panel:JPanel;
private var label:JLabel;
private var loader:JLoadPane;
public function MyRenderer()
{
//构造函数做初始化工作
panel = new JPanel(new FlowLayout());
panel.setOpaque(true);
panel.setBackground(ASColor.GRAY);
//文字
label = new JLabel();
//图片
loader = new JLoadPane();
loader.setScaleMode(AssetPane.SCALE_FIT_PANE);
loader.setPreferredSize(new IntDimension(60,60));
loader.addEventListener(Event.COMPLETE,onLoadComplete);
panel.append(loader);
panel.append(label);
}
private function onLoadComplete(event:Event):void
{
//这里必须要设置,不然加载的图片显示不正常
loader.doLayout();
}
public function setCellValue(v:*):void
{
value = MyItem(v);
//图片
loader.load(new URLRequest(value.getImage));
//文字
label.setText(value.getLabel);
}
public function getCellValue():*
{
return value;
}
public function setListCellStatus(list:JList, isSelected:Boolean, index:int):void
{
if(isSelected)
{
panel.setBackground(list.getSelectionBackground());
}
else
{
panel.setBackground(list.getBackground());
}
}
public function getCellComponent():Component
{
return panel;
}
}
}
{
public class MyItem
{
private var _label:String;
private var _image:String;
public function MyItem(label:String,image:String)
{
_label = label;
_image = image;
}
public function toString():String
{
return "label:" + _label + ",image:" + _image;
}
/**
* getter/setter
*/
public function get getLabel():String
{
return _label;
}
public function set setLabel(value:String):void
{
_label = value;
}
public function get getImage():String
{
return _image;
}
public function set setImage(value:String):void
{
_image = value;
}
}
}
2. PropertyTableModel
PropertyTableModel是JTable的表格模型之一,常常用于把一个对象列表中的对象数据,以对象为行,以对象属性为列来显示,它提供默认方式把属性值以直接字符串化显示,也提供转换器(PropertyTranslator 或Function)把值转换成期望的字符串格式来显示
{
import flash.display.Sprite;
import org.aswing.AsWingManager;
import org.aswing.FlowLayout;
import org.aswing.JTable;
import org.aswing.JWindow;
import org.aswing.VectorListModel;
import org.aswing.border.LineBorder;
import org.aswing.table.PropertyTableModel;
public class Test extends Sprite
{
public function Test()
{
AsWingManager.initAsStandard(this);
//容器
var window:JWindow = new JWindow();
window.setSizeWH(400,400);
window.setLocationXY(100,100);
window.setBorder(new LineBorder());
window.getContentPane().setLayout(new FlowLayout());
window.show();
//JTable
var data:Array = [{name:"iiley", sex:1, age:26},
{name:"Comeny", sex:0, age:24},
{name:"Tom", sex:1, age:30},
{name:"Lita", sex:0, age:16}
];
var model:VectorListModel = new VectorListModel();
model.appendAll(data);
model.append({name:"胡晓伟", sex:1, age:28});
var tableModel:PropertyTableModel = new PropertyTableModel(
model,
["姓名","性别","年龄"],
["name","sex","age"],
[null,sexTranslator,
function(info:*, key:String):*
{
return info[key]+"岁";
}
]);
var table:JTable = new JTable(tableModel);
window.getContentPane().append(table);
}
private function sexTranslator(info:*, key:String):String
{
if(info[key]=="1")
{
return "男";
}
else
{
return "女";
}
}
}
}
3. DefaultTableModel
DefaultTableModel是JTable的表格模型之一
{
import flash.display.Sprite;
import org.aswing.AsWingManager;
import org.aswing.FlowLayout;
import org.aswing.JTable;
import org.aswing.JWindow;
import org.aswing.border.LineBorder;
import org.aswing.table.DefaultTableModel;
public class Test extends Sprite
{
public function Test()
{
AsWingManager.initAsStandard(this);
//容器
var window:JWindow = new JWindow();
window.setSizeWH(400,400);
window.setLocationXY(100,100);
window.setBorder(new LineBorder());
window.getContentPane().setLayout(new FlowLayout());
window.show();
//JTable
var column:Array = ["姓名","性别","年龄"];
var data:Array = [["Sammy","男","25"],["Ruby","女","23"]];
//根据提供的列名和数据初始化
var model:DefaultTableModel = new DefaultTableModel().initWithDataNames(data,column);
//根据提供的列名和指定行数初始化
//var model:DefaultTableModel = new DefaultTableModel().initWithNamesRowcount(column,5);
//根据指定的行数和列数初始化
//var model:DefaultTableModel = new DefaultTableModel().initWithRowcountColumncount(5,5);
var table:JTable = new JTable(model);
window.getContentPane().append(table);
}
}
}
4. JFrame标题栏自定义
{
import flash.display.Sprite;
import org.aswing.AsWingManager;
import org.aswing.Component;
import org.aswing.JFrame;
import org.aswing.plaf.ASColorUIResource;
import org.aswing.plaf.ComponentUI;
public class JFrameSample extends Sprite
{
public function JFrameSample()
{
AsWingManager.initAsStandard(this);
var frame:JFrame = new JFrame(null,"My Title");
frame.setSizeWH(300,250);
frame.show();
//修改标题栏背景色
var ui:ComponentUI = frame.getUI();
ui.putDefault("Frame.mideground",new ASColorUIResource(0xFFFF00));
frame.setUI(ui);
//修改字体颜色与标题栏按钮背景色
var titleBar:Component = frame.getTitleBar().getSelf();
ui = titleBar.getUI();
ui.putDefault("FrameTitleBar.foreground",new ASColorUIResource(0xFF0000));
ui.putDefault("FrameTitleBar.mideground",new ASColorUIResource(0x0000FF,.5));
titleBar.setUI(ui);
}
}
}
5. JScrollPane应用于非ASWING组件
{
import flash.display.Sprite;
import flash.display.StageScaleMode;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import org.aswing.AssetPane;
import org.aswing.JScrollPane;
import org.aswing.geom.IntDimension;
public class Temp extends Sprite
{
public function Temp()
{
stage.scaleMode = StageScaleMode.NO_SCALE;
var textBox:TextField = new TextField();
textBox.autoSize = TextFieldAutoSize.LEFT;
textBox.multiline = true;
textBox.wordWrap = true;
textBox.width = 200;
textBox.text = "asdffffffffffffffffffffferewrfwerewfewwefwwrwwwwwwwwwwwww/:11/:13The 我 胡Ì 是 /:02 ab the /:03 with a /:04/:02/:01 as whye /:05 with a /:06 on egg /:07 with a /:02 /:02 /:55 中华人民共和国的成立,标志着100多年来半殖民地半封建的旧中国历史的 /:02 结束,揭开了中国历史的新篇章,使一个占世界人口近四分之一的大国成/:88为独立自主的国家,人民从此成为国家的主人/:77";
textBox.background = true;
textBox.backgroundColor = 0xFFFF00;
var scrollPane:JScrollPane = new JScrollPane(new AssetPane(textBox));
scrollPane.setLocationXY(100,100);
scrollPane.setPreferredSize(new IntDimension(214, 100));
scrollPane.pack();
addChild(scrollPane);
}
}
}
6. 颜色选取器
{
import flash.display.Sprite;
import flash.display.StageScaleMode;
import flash.events.MouseEvent;
import org.aswing.ASColor;
import org.aswing.border.CaveBorder;
import org.aswing.colorchooser.JColorSwatches;
import org.aswing.event.InteractiveEvent;
public class ColorTest extends Sprite
{
private var picker:JColorSwatches;
private var rect:Sprite;
public function ColorTest()
{
stage.scaleMode = StageScaleMode.NO_SCALE;
picker = new JColorSwatches();
picker.setForeground(new ASColor(0x0000FF, 1));
picker.setAlphaSectionVisible(false);
picker.setHexSectionVisible(false);
picker.setSelectedColor(ASColor.BLUE);
picker.addEventListener(InteractiveEvent.STATE_CHANGED,colorSelectedChange);
picker.setVisible(false);
picker.setBorder(new CaveBorder(null,6));
picker.pack();
this.addChild(picker);
rect = new Sprite();
rect.graphics.lineStyle(1);
rect.graphics.beginFill(picker.getSelectedColor().getRGB());
rect.graphics.drawRect(0,0,25,20);
rect.graphics.endFill();
rect.x = rect.y = 50;
rect.addEventListener(MouseEvent.CLICK,onClick);
this.addChild(rect);
}
private function onClick(event:MouseEvent):void
{
if(!picker.visible)
{
picker.x = rect.x+25+2;
picker.y = rect.y-picker.height+rect.width;
picker.setVisible(true);
stage.addEventListener(MouseEvent.MOUSE_UP,stageMouseUp);
}
}
private function colorSelectedChange(event:InteractiveEvent):void
{
rect.graphics.clear();
rect.graphics.lineStyle(1);
rect.graphics.beginFill(picker.getSelectedColor().getRGB());
rect.graphics.drawRect(0,0,25,20);
rect.graphics.endFill();
picker.setVisible(false);
stage.removeEventListener(MouseEvent.MOUSE_UP,stageMouseUp);
}
private function stageMouseUp(event:MouseEvent):void
{
if (event.target != picker)
{
picker.setVisible(false);
stage.removeEventListener(MouseEvent.MOUSE_UP,stageMouseUp);
}
event.stopImmediatePropagation();
}
}
}