Label textLabel = new Label("I am a Label"); //創(chuàng)建一個(gè)單行文本
Image icon = Image.createImage("/images/duke.png");
Label imageLabel = new Label(icon); //創(chuàng)建一個(gè)帶圖片的Label
setTextPosition(int alignment); //設(shè)置布局方式,CENTER, LEFT, RIGHT,LEFT,TOP, BOTTOM, LEFT, RIGHT, 3。Button Button 可以用于觸發(fā)一個(gè)點(diǎn)擊的事件Action,使用一個(gè)ActionListeners 進(jìn)行監(jiān)聽(tīng)
final Button button = new Button("Old Text");
//使用匿名的ActionListener接口實(shí)現(xiàn)類進(jìn)行監(jiān)聽(tīng)
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
button.setText("New Text");
}});
Button繼承Label, 所以可以設(shè)置不同圖文混編的風(fēng)格,并且提供了三種狀態(tài)ollover, pressed, and the default state 4。RadioButton,ButtonGroup,CheckBox,ComboBox
RadioButton 單選按鈕,繼承自Button
RadioButton radioButton = new RadioButton(“Radio Button”);
同樣,也會(huì)觸發(fā)action
ButtonGroup 用于保證RadioButton只能選中一項(xiàng)
RadioButton rb1 = new RadioButton("First RadioButton in Group 1");
RadioButton rb2 = new RadioButton("Second RadioButton in Group 1");
ButtonGroup group1 = new ButtonGroup();
group1.add(rb1);
group1.add(rb2);
exampleContainer.addComponent(rb1);
exampleContainer.addComponent(rb2);
這里需要注意,只需要添加addComponent(RadioButton) 到容器中,不需要添加ButtonGroup,只用于后臺(tái)管理
CheckBox 多選框,不需要添加到group中,同樣也觸發(fā)事件
final CheckBox checkBox = new CheckBox(“Check Box”);
通過(guò)checkBox.isSelected() 判斷是否被選中
TextArea textArea = new TextArea(5, 20, TextArea.NUMERIC);
textArea.setEditable(false);
構(gòu)造函數(shù)的前兩個(gè)參數(shù)為 總行數(shù)和單行長(zhǎng)度,第三個(gè)參數(shù)用于傳遞到本地文本輸入,用于指定限制的類型
可以使用TextArea的常量 ANY,EMAILADDR, NUMERIC, PHONENUMBER, URL, or DECIMAL,PASSWORD, UNEDITABLE, SENSITIVE,NON_PREDICTIVE, INITIAL_CAPS_SENTENCE, INITIAL_CAPS_WORD.也可以使用 | 設(shè)置復(fù)合約束 ,默認(rèn)情況下可編輯 6。TabbedPane
TabbedPane 通過(guò)類似Tab的選項(xiàng)卡方式并排放置一組容器,容器的tab標(biāo)簽可以使用圖文混編方式
可以使用addTab or insertTab methods 方法添加容器
removeTabAt(int index) 移除容器
tab的索引由0開(kāi)始
tab標(biāo)簽可以有四種不同的放置方法 LEFT, RIGHT, TOP or BOTTOM using the setTabPlacement method.
TabbedPane tabbedPane = new TabbedPane(TabbedPane.TOP);
tabbedPane.addTab("Tab 1", new Label("I am a TabbedPane!"));
tabbedPane.addTab("Tab 2", new Label("Tab number 2"));
構(gòu)造函數(shù)使用一個(gè)Tab標(biāo)簽布局的常量作為參數(shù)
addTab,第一個(gè)為tab標(biāo)簽名,第二個(gè)參數(shù)為直接放置到容器中的內(nèi)容 7。 Using Lists
List,使用Swing的 MVC模式進(jìn)行創(chuàng)建
rendered using a ListCellRenderer and are extracted using the ListModel.
使用ListCellRenderer 作為顯示的V
使用ListModel. 作為數(shù)據(jù)源M
其他部分為C進(jìn)行操作
如下五個(gè)區(qū)域
Center
East
North
South
West
在添加Components的時(shí)候進(jìn)行設(shè)置
addComponent(BorderLayout.CENTER, component) // preferred
或者直接使用字符串
addComponent(“Center”, component) // valid but error prone 容易出錯(cuò)
BoxLayout 盒子布局
包括了 X_AXIS(橫向) Y_AXIS(豎向)的布局,并排的方式
BoxLayout boxLayout = new BoxLayout(BoxLayout.X_AXIS);
BoxLayout boxLayout = new BoxLayout(BoxLayout.Y_AXIS);
FlowLayout 流式布局
這個(gè)也是容器默認(rèn)的布局方式
橫向布局,如果長(zhǎng)度超出,則自動(dòng)換行處理
FlowLayout exampleLayout = new FlowLayout();
container.setLayout(exampleLayout);
也可以通過(guò)設(shè)置構(gòu)造函數(shù),用于設(shè)置布局起始的位置,比如 Left, Right, or Center
FlowLayout exampleLayout = new FlowLayout(Component.RIGHT);
GridLayout 表格式布局,可以用于設(shè)置九宮圖
可以定制行和列,
GridLayout exampleLayout = new GridLayout(0,2); //2表示每行顯示兩個(gè)單元格cell,0不知道是啥
GroupLayout
// GUI builders 的方式進(jìn)行布局,用于NetBeans中的Swing開(kāi)發(fā) 11。Using Painters
是一個(gè)接口,可以用于繪制一個(gè)Components的背景,將繪制在Components限定的范圍內(nèi)
如果要查看painter繪制結(jié)果,需要設(shè)置對(duì)應(yīng)的transparency //需要查看下
painter可以使用絕對(duì)坐標(biāo)的繪圖,所以可以重用在其他組件之上
Painter diagonalPainter = new Painter() {
public void paint(Graphics g, Rectangle rect) {
g.drawLine(rect.getX(),
rect.getY(),
rect.getX() + rect.getSize().getWidth(),
rect.getY() + rect.getSize().getHeight());
}
};
注意查看這里獲取位置的方法 x y, size width height
使用時(shí)候只要給指定的組件設(shè)置Painter即可
myComponent.getStyle().setBgPainter(diagonalPainter);
將會(huì)自動(dòng)應(yīng)用繪圖
記得顯示的調(diào)用設(shè)置透明度 0-255之間
myButton.getStyle().setBgTransparency(100); 12。Using the Style Object 設(shè)置樣式對(duì)象
The Style object sets colors, fonts, transparency, margin, padding, images, and borders to define the style for a given component.
使用component.getStyle(). 獲取該對(duì)象,可以在運(yùn)行時(shí)候改變
顏色Style
Foreground color 前景色,主要指文字顏色
Foreground selection color 當(dāng)組件獲取焦點(diǎn)時(shí),字體的顏色
Background color 背景色
Background selection color 獲取焦點(diǎn)時(shí)候的背景色
這里顏色使用的是RGB(格式為0x000000),不帶alpha透明色,有而外的透明設(shè)置在Style中
Font 字體
Transparency 透明度
setBgTransparency進(jìn)行設(shè)置 范圍是0-255之間
Margin and Padding 外邊距和內(nèi)間距,與css的盒模型一樣
設(shè)置的方式
setPadding(int top, int bottom, int left, int right)
setPadding(int orientation, int gap)
setMargin(int top, int bottom, int left, int right)
setMargin(int orientation, int gap)
// orientation can be Component.TOP, BOTTOM, LEFT or RIGHT
這兩個(gè)類型方法的區(qū)別在于同時(shí)設(shè)置一個(gè)屬性,還是多個(gè)屬性