本站電子書

您可以在這些電子書店找到本站電子書: Google Play 圖書iBooks StoreHyReadReadmooPubu
===>>>本站推出學習遊戲意見調查<<<===《如何自學程式設計》入選 Google Play 台灣地區2016年度最佳書籍

Java 入門指南 - inner 類別

Inner 類別 (inner class) 顧名思義,就是類別 (class) 中的類別,也就是巢狀類別




我們將 EncryptorGUI 改成用 inner 類別來實作傾聽者介面 (interface) ,如下
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;

public class EncryptorGUI {
    private JFrame frame;
    private String[] name;
    private int att[][];
    private ArrayList<JComponent> GUIComponent;
    private String userinput;
    
    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);
        } 
        
        JTextField t = (JTextField) GUIComponent.get(3);
        t.addActionListener(new InputListener());
        t.addCaretListener(new InputListener());
        
        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);
    }

    class InputListener implements ActionListener, CaretListener {
        public void actionPerformed(ActionEvent event) {
            JTextField t2 = (JTextField) GUIComponent.get(3);
            userinput = t2.getText();
            
            JLabel t1 = (JLabel) GUIComponent.get(2);
            t1.setText("This is Input textfield. Your input is \'" + userinput + "\'");
        }
        
        public void caretUpdate(CaretEvent event) {
            JTextField t2 = (JTextField) GUIComponent.get(3);
            userinput = t2.getText();
            
            JLabel t1 = (JLabel) GUIComponent.get(2);
            t1.setText("This is Input textfield. Your input is \'" + userinput + "\'");
        } 
    }
}
    
/* 《程式語言教學誌》的範例程式
    http://pydoing.blogspot.com/
    檔名:EncryptorGUI.java
    功能:示範 Java 程式 
    作者:張凱慶
    時間:西元 2011 年 4 月 */



inner 類別在第 100 行到第 116 行的地方
class InputListener implements ActionListener, CaretListener {
    public void actionPerformed(ActionEvent event) {
        JTextField t2 = (JTextField) GUIComponent.get(3);
        userinput = t2.getText();
            
        JLabel t1 = (JLabel) GUIComponent.get(2);
        t1.setText("This is Input textfield. Your input is \'" + userinput + "\'");
    }
        
    public void caretUpdate(CaretEvent event) {
        JTextField t2 = (JTextField) GUIComponent.get(3);
        userinput = t2.getText();
            
        JLabel t1 = (JLabel) GUIComponent.get(2);
        t1.setText("This is Input textfield. Your input is \'" + userinput + "\'");
    } 
}


InputListener 為實作接收使用者輸入的文字方塊 (textfield) 的傾聽者介面類別,因之取名為 InputListener 。因為 ActionListener 與 CaretListener 在這裡實作 (implement) ,所以
public EncryptorGUI() { 


就不需要 implements ActionListener 與 CaretListener 了。然後 JTextField 登記方面
JTextField t = (JTextField) GUIComponent.get(3);
t.addActionListener(new InputListener());
t.addCaretListener(new InputListener());


這裡改成新建的 InputListener 物件 (object) 。請讀者自行重新編譯 EncryptorGUI ,然後呼叫執行 EncryptorGUIDemo 看看,效果會是一樣的。雖然 inner 類別還有很多細節需要學習,就目前我們的例子而言,認識 GUI 事件處理的部份就夠了 :)


接下來,我們要開始整合 Encrypt 到 EncryptorGUI 中囉!


中英文術語對照
inner 類別inner class
類別class
介面interface
文字方塊textfield
實作implement
物件object


您可以繼續參考
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 月訂正

沒有留言: