
這些容器類別是實作如 Set 、 List 或 Map 等介面 (interface) 的類別,例如我們會用到的 ArrayList 便是實作 List 介面的。想要建立 ArrayList 型態的參考變數 (reference variable) ,可利用 ArrayList 類別 (class) 的建構子 (constructor)
建構子 | 說明 |
---|---|
public ArrayList<E>() | 建立空的 ArrayList |
public ArrayList<E>(int initialCapacity) | 建立含有 initialCapacity 個元素的空 ArrayList |
public ArrayList<E>(Collection<? extends E> c) | 使用 c 建立 ArrayList |
這裡,我們看到建構子中還有角括號圍起來的 E ,這個 <E> 是指定的型態名稱,這是 Java 5.0 之後加入的泛型 (generic) 的特性,使用類別增加許多彈性,因此不像陣列,指定了資料型態就只能放入指定資料型態的參考變數。
ArrayList 的操作依賴方法 (method) ,我們只需要用到其中兩個,如下
方法 | 說明 |
---|---|
boolean add(E e) | 依序增加元素,索引值遞增 |
E get(int index) | 依索引值 index 取得元素 |
int size() | 取得大小 |
add() 是增加 ArrayList 中的元素 (element) , get() 則是取得元素。
因此,原先的 Encryptor 需要修改如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | import java.awt.*; import javax.swing.*; import java.util.*; public class EncryptorGUI { private JFrame frame; private String[] name; private int att[][]; private ArrayList<JComponent> GUIComponent; public EncryptorGUI() { int fill[] = { GridBagConstraints.BOTH, GridBagConstraints.VERTICAL, GridBagConstraints.HORIZONTAL, GridBagConstraints.NONE}; int anchor[] = { GridBagConstraints.CENTER, GridBagConstraints.EAST, GridBagConstraints.SOUTHEAST, GridBagConstraints.SOUTH, GridBagConstraints.SOUTHWEST, GridBagConstraints.WEST, GridBagConstraints.NORTHWEST, GridBagConstraints.NORTH, GridBagConstraints.NORTHEAST}; String n[] = { "Input" , "Output" , "hint..." , "New" , "Load" , "Save" , "Encode" , "Decode" , "Clear" , "Copy" }; name = n; int a[][] = {{ 0 , 0 , 1 , 1 , 0 , 0 , fill[ 3 ], anchor[ 5 ]}, { 0 , 1 , 1 , 1 , 0 , 0 , fill[ 3 ], anchor[ 5 ]}, { 0 , 3 , 7 , 1 , 0 , 0 , fill[ 3 ], anchor[ 5 ]}, { 1 , 0 , 6 , 1 , 0 , 0 , fill[ 0 ], anchor[ 5 ]}, { 1 , 1 , 6 , 1 , 0 , 0 , fill[ 0 ], anchor[ 5 ]}, { 0 , 2 , 1 , 1 , 0 , 0 , fill[ 0 ], anchor[ 0 ]}, { 1 , 2 , 1 , 1 , 0 , 0 , fill[ 0 ], anchor[ 0 ]}, { 2 , 2 , 1 , 1 , 0 , 0 , fill[ 0 ], anchor[ 0 ]}, { 3 , 2 , 1 , 1 , 0 , 0 , fill[ 0 ], anchor[ 0 ]}, { 4 , 2 , 1 , 1 , 0 , 0 , fill[ 0 ], anchor[ 0 ]}, { 5 , 2 , 1 , 1 , 0 , 0 , fill[ 0 ], anchor[ 0 ]}, { 6 , 2 , 1 , 1 , 0 , 0 , fill[ 0 ], anchor[ 0 ]}}; att = a; frame = new JFrame(); GUIComponent = new ArrayList<JComponent>( 12 ); } public void run() { frame.setSize( 600 , 160 ); frame.setLayout( new GridBagLayout()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); int i; for (i = 0 ; i < 3 ; i++) { JLabel nLabel = new JLabel(name[i]); GUIComponent.add(nLabel); } for (i = 0 ; i < 2 ; i++) { JTextField nText = new JTextField( "" , 32 ); GUIComponent.add(nText); } for (i = 3 ; i < 10 ; i++) { JButton nButton = new JButton(name[i]); GUIComponent.add(nButton); } for (i = 0 ; i < GUIComponent.size(); i++) { addComponent(i); } frame.setVisible( true ); } private void addComponent( int i) { GridBagConstraints c = new GridBagConstraints(); int a[] = att[i]; c.gridx = a[ 0 ]; c.gridy = a[ 1 ]; c.gridwidth = a[ 2 ]; c.gridheight = a[ 3 ]; c.weightx = a[ 4 ]; c.weighty = a[ 5 ]; c.fill = a[ 6 ]; c.anchor = a[ 7 ]; frame.add(GUIComponent.get(i), c); } } /* 《程式語言教學誌》的範例程式 檔名:EncryptorGUI.java 功能:示範 Java 程式 作者:張凱慶 時間:西元 2011 年 4 月 */ |
因為 ArrayList 在 java.util 之中,所以我們需要先 import 進來
3 | import java.util.*; |
然後我們增加了幾個屬性
8 9 | private int att[][]; private ArrayList<JComponent> GUIComponent; |
我們把常數值放到建構子中,然後逐一設定給屬性,其中 att 是用來儲存每個視窗元件所需要 GridBagConstraints 的屬性設定值,這些屬性實際上都屬於 int 型態,注意, att 後面有兩個中括弧,表示這是個二維陣列,就是說 att 的元素為整數陣列。
GUIComponent 當作 ArrayList 的參考變數,使用 JComponent 當作 ArrayList 元素的儲存型態,這是因為 JComponent 為 JLabel 、 JTextField 、 JButton 的父類別 (superclass) 。父類別可作為子類別 (subclass) 的通用類別,這也是物件導向程式設計多型 (polymorphism) 的基礎之一。
我們另外把 GridBagConstraints 的設定與視窗元件的 add() 都寫在 addComponent() 裡頭,藉由 i 、 name 與 att 相對應的索引值,因此 addComponent() 使用一個迴圈就可以將 GridBagConstraints 設定完成,並且加入視窗 frame 之中
78 79 80 81 82 83 84 85 86 87 88 89 90 91 | private void addComponent( int i) { GridBagConstraints c = new GridBagConstraints(); int a[] = att[i]; c.gridx = a[ 0 ]; c.gridy = a[ 1 ]; c.gridwidth = a[ 2 ]; c.gridheight = a[ 3 ]; c.weightx = a[ 4 ]; c.weighty = a[ 5 ]; c.fill = a[ 6 ]; c.anchor = a[ 7 ]; frame.add(GUIComponent.get(i), c); } |
addComponent() 的最後一行, GUIComponent 使用 get() 取得元素作為 addComponent() 的參數。
我們只有修改 EncryptorGUI ,所以重新編譯 EncryptorGUI 就可以唷!

結果會是一樣的 :)

嗯,不過按鈕沒有反應,因為我們還沒處理按鈕事件。接下來我們來看看 GUI 設計的最後一環,事件處理囉!
中英文術語對照 | |
---|---|
陣列 | array |
基本資料型態 | primitive data type |
數值 | value |
介面 | interface |
參考變數 | reference variable |
類別 | class |
建構子 | constructor |
泛型 | generic |
方法 | method |
元素 | element |
子類別 | subclass |
多型 | polymorphism |
您可以繼續參考
GUI 篇
相關目錄
回 Java 入門指南
回 Java 教材目錄
回首頁
參考資料
The JavaTM Tutorials: Getting Started
The JavaTM Tutorials: Learning the Java Language
The JavaTM Tutorials: Essential Classes
The Java Language Specification, Third Edition
本文於 2013 年 1 月訂正
1 則留言:
沒有MAIN?
張貼留言