import javax.swing.*;
import java.awt.*;
import java.awt.event.*;class MaceGUI extends JFrame implements ActionListener {
   public MaceGUI() {
      super( "Midwest Association for Charisma Enhancement" ); //窗口名
      pane = getContentPane();//这里应该是得到类似窗口句柄之类的用途吧(容器?)!
      buttons = new JPanel( new FlowLayout() ); //新的布局
      misc = new JPanel( new FlowLayout() ); //新的布局
      stList = new JPanel();
      married = new JRadioButton( "Married", false ); //单选按钮
      email = new JTextField( promptE ); //初始化JTextField类,分别赋于初始值
      name = new JTextField( promptN );  //同上
      reasons = new JTextArea( promptR );//同上
      list = new JList( states ); //** Midwest state names
      init();  
      setVisible( true );
   }
   /*
   *****************************************
   该段函数判断执行的操作:submit clear exit
   *****************************************
   */
   public void actionPerformed( ActionEvent e ) {
      String cmd = e.getActionCommand();
      if ( cmd.equals( submit ) )
        submitForm();
      else if ( cmd.equals( clear ) )
        clearForm();
      else if ( cmd.equals( exit ) )
        System.exit( 0 );
   }
   
   
   private void submitForm() {
      String t = (list.getSelectedValue() == null) ? "none" : 
                  list.getSelectedValue().toString();
      post( name.getText(), email.getText(), t,
            (married.isSelected()) ? "married" : "not maried", 
            reasons.getText() );                    
   }
   
   /*
   **************************************************
   清除name reasons email类的输入值,赋于各自的初始值
   **************************************************
   */
   private void clearForm() {
      reasons.setText( promptR );
      name.setText( promptN );
      email.setText( promptE );
      married.setSelected( false );
      list.getSelectionModel().clearSelection();
   }
   
   //*** For now, a stub function that prints to the standard error.
   private void post( String n, String e, String s, 
                      String m, String r ) {
      System.err.println( n + " " + e + " " + s + "\n" + m + "\n" + r );
   }
   private void init() {
      JLabel j;
      Font f = null;
      pane.setLayout( new BorderLayout() ); //系统默认布局方式 按照东西南北中布局
      buttons.setBackground( Color.gray );
      email.setFont( f = new Font( "Dialog", Font.BOLD, 12 ) );
      name.setFont( f );
      reasons.setFont( f );
      reasons.setLineWrap( true );
      misc.add( name ); misc.add( email ); misc.add( married );
      list.setFont( f );
      list.setBackground( Color.cyan );
      list.getSelectionModel().setSelectionMode( 
         ListSelectionModel.SINGLE_SELECTION );//设置List的选择模式,这里为单一选择模式
      stList.setLayout( new BoxLayout( stList, BoxLayout.Y_AXIS ) );
      stList.add( j = new JLabel( "Home state  " ) );
      j.setFont( f );
      j.setForeground( Color.black );
      stList.add( list );
      addButton( submit ); addButton( clear ); addButton( exit );
      pane.add( buttons, BorderLayout.NORTH );
      pane.add( reasons, BorderLayout.CENTER );
      pane.add( stList, BorderLayout.WEST );
      pane.add( misc, BorderLayout.SOUTH );
      pack();
   }
   //按照给定的名字添加按钮类
   private void addButton( String name ) {
      JButton b = new JButton( name );
      b.setFont( new Font( "Dialog", Font.BOLD, 14 ) );
      b.addActionListener( this ); 
      buttons.add( b );                   
   }
   //*** instance fields
   //类属性定义
   private Container pane;
   private JPanel buttons, misc, stList;
   private JList list;
   private JRadioButton married;
   private JTextField email, name;
   private JTextArea reasons;
   //*** class fileds
   private final static String submit = "  Submit  ";
   private final static String clear = "  Clear  ";
   private final static String exit = "  Exit  ";
   private final static String promptN = 
        "First and last name            ";
   private final static String promptE = 
        "E-mail address                 ";
   private final static String promptR = 
        "   My charisma needs enhancement because";
   private final static String[ ] states = 
        { "Illinois", "Indiana", "Iowa", "Kansas", "Minnesota", 
          "Michigan", "Missouri", "Nebraska", "Ohio", "Wisconsin" };