一個好的用戶界面(GUI)的設計通常可以在現實世界找到相應的表現。
例如,假如在您的面前擺放著一個類似於電腦鍵盤按鍵的一個簡單的按鈕,然而就是這麼簡單的一個按鈕,我們就可以看出一個GUI設計的規則,它由兩個主要的部分構成,一部分使得它具有了按鈕應該具有的動作特性,例如可以被按下。另外一部分則負責它的表現,例如這個按鈕是代表了A還是B。
//[C] 2002 Sun Microsystems, Inc.---
import Java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class RunMVCPattern {
public static void main(String [] arguments){
System.out.println("Example for the MVC pattern");
System.out.println();
System.out.println("In this example, a Contact is divided into");
System.out.println(" Model, View and Controller components.");
System.out.println();
System.out.println("To illustrate the flexibility of MVC, the same");
System.out.println(" Model will be used to provide information");
System.out.println(" to two View components.");
System.out.println();
System.out.println("One view, ContactEditView, will provide a Contact");
System.out.println(" editor window and will be paired with a controller");
System.out.println(" called ContactEditController.");
System.out.println();
System.out.println("The other view, ContactDisplayView, will provide a");
System.out.println(" display window which will reflect the changes made");
System.out.println(" in the editor window. This view does not support");
System.out.println(" user interaction, and so does not provide a controller.");
System.out.println();
System.out.println("Creating ContactModel");
ContactModel model = new ContactModel();