企業(yè)架構(gòu)師成長(zhǎng)之路

          一些心得體會(huì)..

            BlogJava :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
            3 Posts :: 1 Stories :: 3 Comments :: 0 Trackbacks
          ++
          ++ 
          11-1:使用JDialog組件:
          JDialog的類層次結(jié)構(gòu)圖:
            java.lang.Object
              --java.awt.Component
               --java.awt.Container
                 --java.awt.Window
                  --java.awt.JDialog
                   --javax.swing.JDialog
             如果你為公司設(shè)計(jì)一套“物品工具借用系統(tǒng)”,在這個(gè)系統(tǒng)中,借用物品的員工必須詳細(xì)填員工編號(hào)、借用物品、借用器具、借
          用日期、預(yù)計(jì)歸還日期時(shí)間、借用原因等等,若沒(méi)有詳細(xì)填這此數(shù)據(jù),就無(wú)法取得準(zhǔn)許借用物品的證明文件。因此在設(shè)計(jì)這套系統(tǒng)
          的過(guò)程中,你必須查看用戶是否已經(jīng)填妥相關(guān)數(shù)據(jù),若員工忘記了填寫某些重要字段,系統(tǒng)應(yīng)該給予警示,提醒用戶哪些字段必須
          填寫。這個(gè)情況也常發(fā)生在網(wǎng)絡(luò)問(wèn)卷或網(wǎng)絡(luò)會(huì)員注冊(cè)系統(tǒng),用戶必須填寫相關(guān)數(shù)據(jù),例如用戶若沒(méi)有填寫E-Mail郵件地址,則系統(tǒng)
          會(huì)提示你應(yīng)當(dāng)填寫郵件地址,否則系統(tǒng)將不處理用戶填寫的信息。
                   為應(yīng)付這種情況,java提供了JDialog與JOptionPane供我們使用,事實(shí)上,JOptionPane是陽(yáng)春版的JDialog,當(dāng)你在使用
          JOptionPane時(shí),系統(tǒng)會(huì)自動(dòng)產(chǎn)生JDialog組件,并將JOptionPane的內(nèi)容放入JDialog的ContentPane中,而這些均由系統(tǒng)在背后自動(dòng)
          運(yùn)行,并不需要由我們介入。使用JOptionPane的好處是此組件已經(jīng)默認(rèn)了許多交互方式,你只用設(shè)置想要的顯示模式,JOptionPane
          就能輕易的顯示出來(lái),可說(shuō)相當(dāng)方便,若這些模式還是無(wú)法滿足你的需求,你就可以使用JDialog來(lái)自行設(shè)計(jì)你的對(duì)話框。
              我們先來(lái)看如何構(gòu)造JDialog,JOptionPane將在后半段介紹:
          JDialog構(gòu)造函數(shù):
          JDialog():建立一個(gè)non-modal的對(duì)話框,沒(méi)有title也不屬于任何事件窗口組件。
          JDialog(Dialog owner):建立一個(gè)屬于Dialog組件的對(duì)話框,為non-modal形式,也沒(méi)有title.
          JDialog(Dialog owner,Boolean modal):建立一個(gè)屬于Dialog組件的對(duì)話框,可決定modal形式,但沒(méi)有title.
          JDialog(Dialog owner,String title):建立一個(gè)屬于Dialog組件的對(duì)話框,為non-modal形式,對(duì)話框上有title.
          JDialog(Dialog owner,String title,Boolean modal):建立一個(gè)屬于Dialog組件的對(duì)話框,可決定modal形式,且對(duì)話框上有
                                                         title.
          JDialog(Frame owner):建立一個(gè)屬于Frame組件的對(duì)話框,為non-modal形式,也沒(méi)有title.
          JDialog(Frame owner,Boolean modal):建立一個(gè)屬于Frame組件的對(duì)話框,可決定modal形式,但沒(méi)有title.
          JDialog(Frame owner,String title):建立一個(gè)屬于Frame組件的對(duì)話框,為non-modal形式,對(duì)話框上有title.
          JDialog(Frame owner,String title,Boolean modal):建立一個(gè)屬于Frame組件的對(duì)話框,可決定modal形式,且對(duì)話框上有title.
              上面所說(shuō)的modal是一種對(duì)話框操作模式,當(dāng)modal為true時(shí),代表用戶必須結(jié)束對(duì)話框才能回到原來(lái)所屬的窗口。當(dāng)modal為
          false時(shí),代表對(duì)話框與所屬窗口可以互相切換,彼此之間在操作上沒(méi)有順序性。
              一般而言對(duì)話框都會(huì)依附在某個(gè)窗口上,例如JFrame或JDialog,原因在于對(duì)話框通常是一個(gè)程序運(yùn)行的過(guò)程中與用戶互動(dòng)的中
          間過(guò)程,在使用JDialog上跟JFrame非常相似,由上面的JDialog層次結(jié)構(gòu)圖中你可以發(fā)現(xiàn),JDialog是繼承AWT的Dialog類而來(lái),因
          此JDialog為一個(gè)Heavyweight組件。要加入組件到JDialog上與JFrame是一樣的,你必須先取得JDialog的ContentPane,然后再把組
          件加到此ContentPane中,JDialog默認(rèn)的版面管理器是BorderLayout.
          
          11-1-1:在JFrame上建立JDialog:
              我們來(lái)看一個(gè)JDialog的例子,在這個(gè)例子中,用戶在主窗口按下"借用物品"的按鈕時(shí),會(huì)跳出一個(gè)讓用戶填寫窗體的JDialog
          窗口,用戶必須結(jié)束此JDialog窗口后才能返回主窗口.
          
          import javax.swing.*;
          import javax.swing.border.*;
          import java.awt.*;
          import java.awt.event.*;
          import java.util.*;
          import com.incors.plaf.alloy.*;
          import com.incors.plaf.alloy.themes.glass.*;
          public class DialogDemo implements ActionListener{
            JFrame f=null;
            
            public void actionPerformed(ActionEvent e){
            	 String cmd=e.getActionCommand();
            	 if (cmd.equals("借用物品")){
            	 	new LendingSystem(f);
            	 }else if (cmd.equals("離開系統(tǒng)")){
            	    System.exit(0);  
               }
            }
            public DialogDemo(){
            	 f=new JFrame("JDialog Example");
            	 Container contentPane=f.getContentPane();
            	 JPanel buttonPanel=new JPanel();
            	 JButton b=new JButton("借用物品");
            	 b.addActionListener(this);
            	 buttonPanel.add(b);
            	 b=new JButton("離開系統(tǒng)");
            	 b.addActionListener(this);
            	 buttonPanel.add(b);
            	 
            	 buttonPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.blue,2),
            	                "借用物品系統(tǒng)",TitledBorder.CENTER,TitledBorder.TOP));
               contentPane.add(buttonPanel,BorderLayout.CENTER);
               f.pack();
               f.setVisible(true);
               f.addWindowListener(new WindowAdapter(){
               	   public  void windowClosing(WindowEvent e){
               	   	 System.exit(0);
               	   }
                 });  	            
            }
            public static void main(String[] args){
            	  SwingUtil.setLookAndFeel();
            	  new DialogDemo();
            }
          }
          class LendingSystem implements ActionListener{
                JTextField staffField,objectField,borrowDateField,returnDateField,reasonField;
                JDialog dialog;	
                public void actionPerformed(ActionEvent e){
                	 String cmd=e.getActionCommand();
                	 if (cmd.equals("確定")){
                	 	
                	 }else if (cmd.equals("取消")){
                	   dialog.dispose();	
                	 }
                }
              LendingSystem(JFrame f){
                  dialog = new JDialog(f,"借用物品",true);
                  GridBagConstraints c;
                  int gridx,gridy,gridwidth,
                      gridheight,anchor,fill,ipadx,ipady;
                  double weightx,weighty;
                  Insets inset;     
                  
                  GridBagLayout gridbag = new GridBagLayout();
                  Container dialogPane = dialog.getContentPane();
                  dialogPane.setLayout(gridbag);
                  
                  JLabel label = new JLabel("員工編號(hào) : ");
                  gridx=0;               //第0列
                  gridy=0;               //第0行
                  gridwidth = 1;         //占一單位寬度
                  gridheight = 1;        //占一單位高度
                  weightx = 0;           //窗口增大時(shí)組件寬度增大比率0
                  weighty = 0;           //窗口增大時(shí)組件高度增大比率0
                  anchor = GridBagConstraints.CENTER; //容器大于組件size時(shí)將組件置于容器中央
                  fill = GridBagConstraints.BOTH;     //窗口拉大時(shí)會(huì)填滿水平與垂直空間
                  inset = new Insets(0,0,0,0);        //組件間間距
                  ipadx = 0;                          //組件內(nèi)水平寬度
                  ipady = 0;                          //組件內(nèi)垂直高度
                  c = new GridBagConstraints(gridx,gridy,gridwidth,gridheight,
                      weightx,weighty,anchor,fill,inset,ipadx,ipady);
                  gridbag.setConstraints(label,c);
                  dialogPane.add(label); 
                  
                  label = new JLabel("借用器具 : ");
                  gridx=3;
                  gridy=0;
                  c = new GridBagConstraints(gridx,gridy,gridwidth,gridheight,
                      weightx,weighty,anchor,fill,inset,ipadx,ipady);
                  gridbag.setConstraints(label,c);
                  dialogPane.add(label);
                  
                  label = new JLabel("借用日期: ");
                  gridx=0;
                  gridy=1;
                  c = new GridBagConstraints(gridx,gridy,gridwidth,gridheight,
                      weightx,weighty,anchor,fill,inset,ipadx,ipady);
                  gridbag.setConstraints(label,c);
                  dialogPane.add(label);
                  
                  label = new JLabel("歸還日期: ");
                  gridx=3;
                  gridy=1;
                  c = new GridBagConstraints(gridx,gridy,gridwidth,gridheight,
                      weightx,weighty,anchor,fill,inset,ipadx,ipady);
                  gridbag.setConstraints(label,c);
                  dialogPane.add(label);
                  
                  label = new JLabel("借用原因 : ");
                  gridx=0;
                  gridy=2;
                  c = new GridBagConstraints(gridx,gridy,gridwidth,gridheight,
                      weightx,weighty,anchor,fill,inset,ipadx,ipady);
                  gridbag.setConstraints(label,c);
                  dialogPane.add(label);
                  
                  staffField = new JTextField();
                  gridx=1;
                  gridy=0;
                  gridwidth = 2;
                  gridheight = 1;
                  weightx = 1;
                  weighty = 0;
                  c = new GridBagConstraints(gridx,gridy,gridwidth,gridheight,
                      weightx,weighty,anchor,fill,inset,ipadx,ipady);
                  gridbag.setConstraints(staffField,c);
                  dialogPane.add(staffField);
                  
                  objectField = new JTextField();
                  gridx=4;
                  gridy=0;
                  c = new GridBagConstraints(gridx,gridy,gridwidth,gridheight,
                      weightx,weighty,anchor,fill,inset,ipadx,ipady);
                  gridbag.setConstraints(objectField,c);
                  dialogPane.add(objectField);
                  
                  borrowDateField = new JTextField();
                  gridx=1;
                  gridy=1;
                  c = new GridBagConstraints(gridx,gridy,gridwidth,gridheight,
                      weightx,weighty,anchor,fill,inset,ipadx,ipady);
                  gridbag.setConstraints(borrowDateField,c);
                  dialogPane.add(borrowDateField);
                  
                  returnDateField = new JTextField();
                  gridx=4;
                  gridy=1;
                  c = new GridBagConstraints(gridx,gridy,gridwidth,gridheight,
                      weightx,weighty,anchor,fill,inset,ipadx,ipady);
                  gridbag.setConstraints(returnDateField,c);
                  dialogPane.add(returnDateField);
                  
                  reasonField = new JTextField();
                  gridx=1;
                  gridy=2;
                  gridwidth = 5;
                  c = new GridBagConstraints(gridx,gridy,gridwidth,gridheight,
                      weightx,weighty,anchor,fill,inset,ipadx,ipady);
                  gridbag.setConstraints(reasonField,c);
                  dialogPane.add(reasonField);
                  
                  JPanel panel = new JPanel();
                  panel.setLayout(new GridLayout(1,2));
                  JButton b = new JButton("確定");
                  panel.add(b);
                  b = new JButton("取消");
                  b.addActionListener(this);
                  panel.add(b);
                  
                  gridx=0;
                  gridy=3;
                  gridwidth = 6;
                  weightx = 1;
                  weighty = 1;
                  c = new GridBagConstraints(gridx,gridy,gridwidth,gridheight,
                      weightx,weighty,anchor,fill,inset,ipadx,ipady);
                  gridbag.setConstraints(panel,c);
                  dialogPane.add(panel);
                  
                  dialog.setBounds(200,150,400,130);
                  dialog.show();
              }
          }
          
          class SwingUtil{
            public static final void setLookAndFeel() {  	
            	try{
                      Font font = new Font("JFrame", Font.PLAIN, 12);
                      Enumeration keys = UIManager.getLookAndFeelDefaults().keys();
           
                      while (keys.hasMoreElements()) {
                         Object key = keys.nextElement();
                         if (UIManager.get(key) instanceof Font) {
                            UIManager.put(key, font);
                          }
                      } 
                  	AlloyLookAndFeel.setProperty("alloy.isLookAndFeelFrameDecoration", "true");
                  	AlloyTheme theme = new GlassTheme();
                  	LookAndFeel alloyLnF = new AlloyLookAndFeel(theme);                    
                      UIManager.setLookAndFeel(alloyLnF);
            	}catch(UnsupportedLookAndFeelException ex){
            	  ex.printStackTrace();	
            	}
            	/*
            	try{
                      Font font = new Font("JFrame", Font.PLAIN, 12);
                      Enumeration keys = UIManager.getLookAndFeelDefaults().keys();
           
                      while (keys.hasMoreElements()) {
                         Object key = keys.nextElement();
                         if (UIManager.get(key) instanceof Font) {
                            UIManager.put(key, font);
                          }
                      } 
            	   	    UIManager.setLookAndFeel("com.stefankrause.xplookandfeel.XPLookAndFeel");		
            	}catch(Exception e){
            	  e.printStackTrace();	
            	}*/
           }
          }	
          
          11-2:使用JOptionPane類的靜態(tài)方法:
          JOptionPane的類層次結(jié)構(gòu)圖:
             java.lang.Object
               --java.awt.Component
                 --java.awt.Container
                   --javax.swing.JComponent
                    --javax.swing.JOptionPane
              以JDialog來(lái)制作對(duì)話框,你必須實(shí)作對(duì)話框中的每一個(gè)組件,但有時(shí)候我們的對(duì)話框只是要顯示一段文字,或是一些簡(jiǎn)單的
          選擇(是或否),這時(shí)候可以利用JOptionPane類,它可以讓你很簡(jiǎn)單的做出這樣的效果,不僅大大的減少了程序代碼的編寫,也
          讓整個(gè)程序看起來(lái)清爽許多!
          JOptionPane的構(gòu)造函數(shù):
          JOptionPane():建立一個(gè)顯示測(cè)試信息的JOptionPane組件。
          JOptionPane(Object message):建立一個(gè)顯示特定信息的JOptionPane組件。
          JOptionPane(Object message,int messageType):建立一個(gè)顯示特定信息的JOptionPane組件,并設(shè)置信息類型。
          JOptionPane(Object message,int messageType,int optionType):建立一個(gè)顯示特定信息的JOptionPane組件,并設(shè)置信息與選項(xiàng).
          JOptionPane(Object message,int messageType,int optionType,Icon icon):建立一個(gè)顯示特定信息的JOptionPane組件,并設(shè)置
                                                     信息與選項(xiàng),且可顯示出圖案.
          JOptionPane(Object message,int messageType,int optionType,Icon icon,Object[] options):建立一個(gè)顯示特定信息的
                                                JOptionPane組件,并設(shè)置信息與選項(xiàng),且可顯示出圖案.選項(xiàng)值是一個(gè)Object Array,可
                                                用作更改按鈕上的文字.
          JOptionPane(Object message,int messageType,int optionType,Icon icon,Object[] options,Object initialValue):建立一個(gè)
                                                顯示特定信息的JOptionPane組件,并設(shè)置信息與選項(xiàng)類型,且可以顯示出圖案.選項(xiàng)值是
                                                一個(gè)Object Array,可用作更改按鈕上的文字,并設(shè)置默認(rèn)按鈕.
              使用JOptionPane對(duì)象所得到的對(duì)話框是modal為true形式,也就是說(shuō)我們必須先關(guān)閉對(duì)話框窗口才能回到產(chǎn)生對(duì)話框的母窗口上
          .
              要利用JOptionPane類來(lái)輸出對(duì)話框,通常我們不會(huì)new一個(gè)JOptionPane對(duì)象出來(lái),而是使用JOptionPane所提供的一些靜態(tài)方法,
          不用產(chǎn)生JOptionPane對(duì)象就可以直接使用,這些方法方法都是以showXxxxxDialog的形式出現(xiàn),若你的對(duì)話框是出現(xiàn)在
          InternaleFrame上,你可以用showInternalXxxxxDialog的各種方法產(chǎn)生對(duì)話框.以下我們整理出JOptionPane提供輸出對(duì)話框的所有
          靜態(tài)方法:
                                                       Message Dialog
               ----------------------------------------------------------------------------------------------------------
               方法:  |void  showMessageDialog(Component parentComponent,Object message)
                      |void  showMessageDialog(Component parentComponent,Object message,String title,int messageType)
                      |void  showMessageDialog(Component parentComponent,Object message,String title,int messageType,Icon
                      |                        icon)
                      |void  showInternalMessageDialog(Component parentComponent,Object message)
                      |void  showInternalMessageDialog(Component parentComponent,Object message,String title,int messageType)
                      |void  showInternalMessageDialog(Component parentComponent,Object message,String title,int messageType
                      |                        ,Icon icon)
               ------------------------------------------------------------------------------------------------------------
               說(shuō)明:  |顯示信息對(duì)話框,對(duì)話框中只含有一個(gè)按鈕,通常是"確定"按鈕,例如安裝完某個(gè)軟件時(shí)通常會(huì)跳出一個(gè)對(duì)話框告知你
                      |安裝已經(jīng)成功.這類的方法有5種參數(shù):
                      |parentComponent:是指產(chǎn)生對(duì)話框的組件為何,通常是指Frame或Dialog組件.
                      |        message:是指要顯示的組件,通常是String或Label類型
                      |          title:對(duì)話框標(biāo)題列上顯示的文字.
                      |    messageType:指定信息類型,共有5種類型,分別是ERROR_MESSAGE,INFORMATION_MESSAGE,WARING_MESSAGE,
                      |                QUESTION_MESSAGE,PLAIN_MESSAGE(不顯示圖標(biāo)).指定類型后對(duì)話框就會(huì)出現(xiàn)相對(duì)應(yīng)的圖標(biāo).
                      |           icon:若你不喜歡java給的圖標(biāo),你可以自己自定圖標(biāo).
               ------------------------------------------------------------------------------------------------------------  
                                                       Confirm Dialog
               ------------------------------------------------------------------------------------------------------------  
               方法:  |int  showConfirmDialog(Component parentComponent,Object message)
                      |int  showConfirmDialog(Component parentComponent,Object message,String title,int optionType)
                      |int  showConfirmDialog(Component parentComponent,Object message,String title,int optionType, 
                      |                  int messageType)
                      |int  showConfirmDialog(Component parentComponent,Object message,String title,int optionType, 
                      |                  int messageType,Icon icon)
                      |int  showInternalConfirmDialog(Component parentComponent,Object message)
                      |int  showInternalConfirmDialog(Component parentComponent,Object message,String title,int optionType)
                      |int  showInternalConfirmDialog(Component parentComponent,Object message,String title,int optionType
                      |                  ,int messageType)
                      |int  showInternalConfirmDialog(Component parentComponent,Object message,String title,int optionType
                      |                  ,int messageType,Icon icon)
               -----------------------------------------------------------------------------------------------------------
               說(shuō)明:  |顯示確認(rèn)對(duì)話框,這類的對(duì)話框通常會(huì)問(wèn)用戶一個(gè)問(wèn)題,然后用戶回答是或不是,例如當(dāng)我們修改了某個(gè)文件的內(nèi)容    
                      |卻沒(méi)存盤就要離開時(shí),系統(tǒng)大部份都會(huì)跳出確認(rèn)對(duì)話框.詢問(wèn)我們是否要存儲(chǔ)修改過(guò)的內(nèi)容.確認(rèn)對(duì)話框方法有6種
                      |參數(shù):
                      |parentComponent:是指產(chǎn)生對(duì)話框的組件為何,通常是指Frame或Dialog組件.
                      |        message:是指要顯示的組件,通常是String或Label類型
                      |          title:對(duì)話框標(biāo)題列上顯示的文字.
                      |     optionType:確定按鈕的類型,有5種類型,分別是DEFAULT_OPTION,YES_NO_OPTION,YES_NO_CANCEL,與
                      |                OK_CANCEL_OPTION.
                      |    messageType:指定信息類型,共有5種類型,分別是ERROR_MESSAGE,INFORMATION_MESSAGE,WARING_MESSAGE,
                      |                QUESTION_MESSAGE,PLAIN_MESSAGE(不顯示圖標(biāo)).指定類型后對(duì)話框就會(huì)出現(xiàn)相對(duì)應(yīng)的圖標(biāo).
                      |           icon:若你不喜歡java給的圖標(biāo),你可以自己自定圖標(biāo).
                      |       返回值為一整數(shù)值,依用戶按下什么鈕而定,YES_OPTION=0,NO_OPTION=1,CANCEL_OPTION=2,OK_OPTION=0
                      |,CLOSED_OPTION=-1(當(dāng)用戶都不選直接關(guān)掉對(duì)話框時(shí))
                ----------------------------------------------------------------------------------------------------------
                
                                                        Input Dialog
                -----------------------------------------------------------------------------------------------------------
                方法: |String showInputDialog(Object message)
                      |String showInputDialog(Component parentComponent,Object message)
                      |String showInputDialog(Component parentComponent,Object message,String title,int messageType)
                      |Object showInputDailog(Component parentComponent,Object message,String title,int messageType,Icon icon
                      |                       ,Object[] selectionValues,Object initialSelectionValue)
                      |String showInternalInputDialog(Object message)
                      |String showInternalInputDialog(Component parentComponent,Object message)
                      |String showInternalInputDialog(Component parentComponent,Object message,String title,int messageType)
                      |Object showInternalInputDialog(Component parentComponent,Object message,String title,int messageType,
                      |                               Icon icon,Object[] selectionValues,Object initialSelectionValue)
               -----------------------------------------------------------------------------------------------------------
                說(shuō)明: |顯示輸入對(duì)話框,這類的對(duì)話框可以讓用戶輸入相關(guān)的信息,當(dāng)用戶按下確定鈕后,系統(tǒng)會(huì)得到用戶所輸入的信息.輸入
                      |對(duì)話框不僅可以讓用戶輸入文字,也可以提供Combo Box組件讓用戶選擇相關(guān)信息,避免用戶輸入錯(cuò)誤.輸入對(duì)話框方法
                      |有6種參數(shù):
                      |parentComponent:是指產(chǎn)生對(duì)話框的組件為何,通常是指Frame或Dialog組件.
                      |        message:是指要顯示的組件,通常是String或Label類型
                      |          title:對(duì)話框標(biāo)題列上顯示的文字.
                      |    messageType:指定信息類型,共有5種類型,分別是ERROR_MESSAGE,INFORMATION_MESSAGE,WARING_MESSAGE,
                      |                QUESTION_MESSAGE,PLAIN_MESSAGE(不顯示圖標(biāo)).指定類型后對(duì)話框就會(huì)出現(xiàn)相對(duì)應(yīng)的圖標(biāo).
                      |           icon:若你不喜歡java給的圖標(biāo),你可以自己自定圖標(biāo).
                      |selectionValues:給用戶選擇的可能值.Object Array中的數(shù)據(jù)會(huì)以ComboBox方式顯示出來(lái).
                      |initialSelectionValue:對(duì)話框初始化時(shí)所顯示的值.
                      |     當(dāng)用戶按下確定按鈕時(shí)會(huì)返回用戶輸入的信息,若按下取消按鈕則返回null.
               -------------------------------------------------------------------------------------------------------------
                      
          
                                                           Option Dialog
               -------------------------------------------------------------------------------------------------------------
                方法: |int showOptionDialog(Component parentComponent,Object message,String title,int optionType,int 
                      |                    messageType,Icon icon,Object[] options,Object initalValue)
                      |int showInternalOptionDialog(Component parentComponent,Object message,String title,int optionType,
                      |                   int messageType,Icon icon,Object[] options,Object initialValue)
               -------------------------------------------------------------------------------------------------------------
                說(shuō)明: |顯示選擇對(duì)話框,這類的對(duì)話框可以讓用戶自定義對(duì)話類型,最大的好處是可以改變按鈕上的文字.選擇對(duì)話框方法有
                      |6種參數(shù):
                      |parentComponent:是指產(chǎn)生對(duì)話框的組件為何,通常是指Frame或Dialog組件.
                      |        message:是指要顯示的組件,通常是String或Label類型
                      |          title:對(duì)話框標(biāo)題列上顯示的文字.
                      |     optionType:確定按鈕的類型,有5種類型,分別是DEFAULT_OPTION,YES_NO_OPTION,YES_NO_CANCEL,與
                      |                OK_CANCEL_OPTION.
                      |    messageType:指定信息類型,共有5種類型,分別是ERROR_MESSAGE,INFORMATION_MESSAGE,WARING_MESSAGE,
                      |                QUESTION_MESSAGE,PLAIN_MESSAGE(不顯示圖標(biāo)).指定類型后對(duì)話框就會(huì)出現(xiàn)相對(duì)應(yīng)的圖標(biāo).
                      |           icon:若你不喜歡java給的圖標(biāo),你可以自己自定圖標(biāo).
                      |        options:給用戶選擇的按鈕顯示文字.
                      |    initalValue:對(duì)話框初始化時(shí)按鈕默認(rèn)值.
                      |       返回值為一整數(shù)值,依用戶按下什么鈕而定,YES_OPTION=0,NO_OPTION=1,CANCEL_OPTION=2,OK_OPTION=0
                      |,CLOSED_OPTION=-1(當(dāng)用戶都不選直接關(guān)掉對(duì)話框時(shí))
                ------------------------------------------------------------------------------------------------------------
              上面表格看起來(lái)好像很多方法似的,但實(shí)際上只區(qū)分成四大類,你只需要選擇要用哪類的對(duì)話框,再?zèng)Q定使用那類中的哪個(gè)方法即
          可.我們慢慢來(lái)為你介紹這四個(gè)種類的對(duì)話框.
          
          11-2-1:輸出Message Dialog
              Message Dialog是在對(duì)話框上顯示出一段信息,目的在告知用戶一些相關(guān)信息,因此Message Dialog只會(huì)有一個(gè)確定按鈕,讓用戶
          看完信息后就可以關(guān)閉這個(gè)對(duì)話框.下面這個(gè)例子我們使用Message對(duì)話框,我們來(lái)看看不同的MessageType會(huì)有什么樣的圖案產(chǎn)生.
          MessageDialog.java
          
          import java.awt.*;
          import javax.swing.*;
          import java.awt.event.*;
          import java.util.*;
          import com.incors.plaf.alloy.*;
          import com.incors.plaf.alloy.themes.glass.*;
          public class MessageDialog implements ActionListener{
             JFrame f=null;
             public MessageDialog(){
               f=new JFrame("optionPane");
               Container contentPane=f.getContentPane();
               contentPane.setLayout(new GridLayout(2,3));
               JButton b=new JButton("Show Error Icon");
               b.addActionListener(this);
               contentPane.add(b);
               b=new JButton("Show Information Icon");
               b.addActionListener(this);
               contentPane.add(b);
               b=new JButton("Show Waring Icon");
               b.addActionListener(this);
               contentPane.add(b);
               b=new JButton("Show Question Icon");
               b.addActionListener(this);
               contentPane.add(b);
               b=new JButton("Show Plain Icon");
               b.addActionListener(this);
               contentPane.add(b);
               b=new JButton("Show User Define Icon");
               b.addActionListener(this);
               contentPane.add(b);
               f.pack();
               f.setVisible(true);
               f.addWindowListener(new WindowAdapter(){
               	 public void windowClosing(WindowEvent e){
               	   System.exit(0);	
               	 }
               });
             }	
             public static void main(String[] args){
             	  SwingUtil.setLookAndFeel();
             	  new MessageDialog();
             }
             public void actionPerformed(ActionEvent e){
             	  String cmd=e.getActionCommand();
             	  String title="Message Dialog";
             	  String message="";
             	  int type=JOptionPane.PLAIN_MESSAGE;
             	  if (cmd.equals("Show Error Icon")){
             	  	  type=JOptionPane.ERROR_MESSAGE;
             	  	  message="Error Message";
             	  }else if (cmd.equals("Show Information Icon")){
             	  	  type=JOptionPane.INFORMATION_MESSAGE;
             	  	  message="information Message";
             	  }else if (cmd.equals("Show Waring Icon")){
             	  	  type=JOptionPane.WARNING_MESSAGE;
             	  	  message="Waring Message";
             	  }else if (cmd.equals("Show Question Icon")){
             	  	  type=JOptionPane.QUESTION_MESSAGE;
             	  	  message="Question Message";
             	  }else if (cmd.equals("Show Plain Icon")){
             	  	  type=JOptionPane.PLAIN_MESSAGE;
             	  	  message="Plain Message";
             	  }else if (cmd.equals("Show User Define Icon")){
             	  	  type=JOptionPane.PLAIN_MESSAGE;
             	  	  JOptionPane.showMessageDialog(f,message,title,type,new ImageIcon("..\\icons\\glass.jpg"));
             	  	  return ;
             	  }
             	  JOptionPane.showMessageDialog(f,message,title,type);
             }
          }
          class SwingUtil{
            public static final void setLookAndFeel() {
            	try{
                      Font font = new Font("JFrame", Font.PLAIN, 12);
                      Enumeration keys = UIManager.getLookAndFeelDefaults().keys();
           
                      while (keys.hasMoreElements()) {
                         Object key = keys.nextElement();
                         if (UIManager.get(key) instanceof Font) {
                            UIManager.put(key, font);
                          }
                      } 
                  	AlloyLookAndFeel.setProperty("alloy.isLookAndFeelFrameDecoration", "true");
                  	AlloyTheme theme = new GlassTheme();
                  	LookAndFeel alloyLnF = new AlloyLookAndFeel(theme);
                     JFrame.setDefaultLookAndFeelDecorated(true); 
                    
                     UIManager.setLookAndFeel(alloyLnF);
            	}catch(UnsupportedLookAndFeelException ex){
            	  ex.printStackTrace();	
            	}
           }
          }	
          
          11-2-2:輸出Confirm Dialog:
              看過(guò)了信息對(duì)話框,接著我們來(lái)看確認(rèn)對(duì)話框到底怎樣.Confirm Dialog目的在讓用戶對(duì)某個(gè)問(wèn)題選擇"Yes"或"No",可算是一種
          相當(dāng)簡(jiǎn)單的是非選擇對(duì)話框.下面這個(gè)范例中,用戶可選擇不同按鈕類型的確認(rèn)對(duì)話框,為方便說(shuō)明,在此我們將Message Type都默認(rèn)
          為JOptionPane.INFORMATION_MESSAGE.
          ConfirmDialog.java
          
          import java.awt.*;
          import javax.swing.*;
          import java.awt.event.*;
          import java.util.*;
          import com.incors.plaf.alloy.*;
          import com.incors.plaf.alloy.themes.glass.*;
          
          public class ConfirmDialog implements ActionListener
          {
              JFrame f = null;
              JLabel label = null;
              
              public ConfirmDialog() 
              {
                  f = new JFrame("OptionPane Demo");
                  Container contentPane = f.getContentPane();
                  
                  JPanel panel = new JPanel();
                  panel.setLayout(new GridLayout(2,2));
                  
                  JButton b = new JButton("Show DEFAULT_OPTION");
                  b.addActionListener(this);
                  panel.add(b);
                  b = new JButton("Show YES_NO_OPTION");
                  b.addActionListener(this);
                  panel.add(b);
                  b = new JButton("Show YES_NO_CANCEL_OPTION");
                  b.addActionListener(this);
                  panel.add(b);
                  b = new JButton("Show OK_CANCEL_OPTION");
                  b.addActionListener(this);
                  panel.add(b);
                  
                  label = new JLabel(" ",JLabel.CENTER);
                  contentPane.add(label,BorderLayout.NORTH);
                  contentPane.add(panel,BorderLayout.CENTER);
                  f.pack();
                  f.setVisible(true);
                  
                  f.addWindowListener(new WindowAdapter() {
                      public void windowClosing(WindowEvent e) {
                          System.exit(0);
                      }
                  });
              }    
              
              public static void main(String[] args)
              {
              	SwingUtil.setLookAndFeel();
                  new ConfirmDialog();
              }
              
              public void actionPerformed(ActionEvent e) 
              {
                  //處理用戶按鈕事件,默認(rèn)的messageType是JoptionPane.INFORMATION_MESSAGE.
                  String cmd = e.getActionCommand();
                  String title = "Confirm Dialog";
                  String message ="";
                  int messageType = JOptionPane.INFORMATION_MESSAGE;
                  int optionType = JOptionPane.YES_NO_OPTION;
          
                  if(cmd.equals("Show DEFAULT_OPTION")) {
                      optionType = JOptionPane.DEFAULT_OPTION;
                      message = "Show DEFAULT_OPTION Buttons";
                  } else if(cmd.equals("Show YES_NO_OPTION")) {
                      optionType = JOptionPane.YES_NO_OPTION;
                      message = "Show YES_NO_OPTION Buttons";
                  } else if(cmd.equals("Show YES_NO_CANCEL_OPTION")) {
                      optionType = JOptionPane.YES_NO_CANCEL_OPTION;
                      message = "Show YES_NO_CANCEL_OPTION Buttons";
                  } else if(cmd.equals("Show OK_CANCEL_OPTION")) {
                      optionType = JOptionPane.OK_CANCEL_OPTION;
                      message = "Show OK_CANCEL_OPTION Buttons";
                  }
              
                  int result = JOptionPane.showConfirmDialog(f, message, 
                               title, optionType, messageType);
                  
                  if (result == JOptionPane.YES_OPTION)
                      label.setText("您選擇:Yes or OK");
                  if (result == JOptionPane.NO_OPTION)
                      label.setText("您選擇:No");
                  if (result == JOptionPane.CANCEL_OPTION)
                      label.setText("您選擇:Cancel");
                  if (result == JOptionPane.CLOSED_OPTION)
                      label.setText("您沒(méi)做任何選擇,并關(guān)閉了對(duì)話框");
              }
          }
          class SwingUtil{
            public static final void setLookAndFeel() {
            	try{
                      Font font = new Font("JFrame", Font.PLAIN, 12);
                      Enumeration keys = UIManager.getLookAndFeelDefaults().keys();
           
                      while (keys.hasMoreElements()) {
                         Object key = keys.nextElement();
                         if (UIManager.get(key) instanceof Font) {
                            UIManager.put(key, font);
                          }
                      } 
                  	AlloyLookAndFeel.setProperty("alloy.isLookAndFeelFrameDecoration", "true");
                  	AlloyTheme theme = new GlassTheme();
                  	LookAndFeel alloyLnF = new AlloyLookAndFeel(theme);
                      UIManager.setLookAndFeel(alloyLnF);
            	}catch(UnsupportedLookAndFeelException ex){
            	  ex.printStackTrace();	
            	}
           }
          }	
          
          
          11-2-3:輸出Input Dialog:
              Input Dialog可以讓用戶輸入相關(guān)信息,當(dāng)用戶按下確定鈕后,系統(tǒng)會(huì)得到用戶所輸入的信息.輸入對(duì)話框不僅可以讓用戶自輸入
          文字,也可以顯示出ComboBox組件讓用戶選擇相關(guān)信息,避免用戶輸入錯(cuò)誤,當(dāng)用戶輸入完畢按下確定按鈕時(shí)會(huì)返回用戶輸入的信息,
          若按下取消則返回null值.下面為InputDialog的范例.
          InputDialog.java
          
          import java.awt.*;
          import javax.swing.*;
          import java.awt.event.*;
          import java.util.*;
          import com.incors.plaf.alloy.*;
          import com.incors.plaf.alloy.themes.glass.*;
          
          public class InputDialog implements ActionListener
          {
              JFrame f = null;
              JLabel label = null;
              
              public InputDialog() 
              {
                  f = new JFrame("OptionPane Demo");
                  Container contentPane = f.getContentPane();
                  
                  JPanel panel = new JPanel();
                  panel.setLayout(new GridLayout(1,2));
                  
                  JButton b = new JButton("Show Text Input");
                  b.addActionListener(this);
                  panel.add(b);
                  b = new JButton("Show ComboBox Input");
                  b.addActionListener(this);
                  panel.add(b);
                  
                  label = new JLabel(" ",JLabel.CENTER);
                  contentPane.add(label,BorderLayout.NORTH);
                  contentPane.add(panel,BorderLayout.CENTER);
                  f.pack();
                  f.setVisible(true);
                  
                  f.addWindowListener(new WindowAdapter() {
                      public void windowClosing(WindowEvent e) {
                          System.exit(0);
                      }
                  });
              }    
              
              public static void main(String[] args)
              {
              	SwingUtil.setLookAndFeel();
                  new InputDialog();
              }
              
              public void actionPerformed(ActionEvent e) 
              {
                  String cmd = e.getActionCommand();
                  String title = "Input Dialog";
                  String message ="您最熟悉哪一種程序語(yǔ)言?";
                  int messageType = JOptionPane.QUESTION_MESSAGE;
                  String[] values = {"JAVA","PHP","ASP","C++","VB"};
                  String result ="";
          
                  if(cmd.equals("Show Text Input")) {
                      result = JOptionPane.showInputDialog(f, message, 
                               title, messageType);
                      
                  } else if(cmd.equals("Show ComboBox Input")) {
                      result = (String)JOptionPane.showInputDialog(f, message, 
                               title, messageType,null,values,values[0]);
                  }
              
                  if (result == null)
                      label.setText("您取消了對(duì)話框");
                  else{
                      label.setText("您輸入:"+result);
                  }
              }
          }
          class SwingUtil{
            public static final void setLookAndFeel() {
            	try{
                      Font font = new Font("JFrame", Font.PLAIN, 12);
                      Enumeration keys = UIManager.getLookAndFeelDefaults().keys();
           
                      while (keys.hasMoreElements()) {
                         Object key = keys.nextElement();
                         if (UIManager.get(key) instanceof Font) {
                            UIManager.put(key, font);
                          }
                      } 
                  	AlloyLookAndFeel.setProperty("alloy.isLookAndFeelFrameDecoration", "true");
                  	AlloyTheme theme = new GlassTheme();
                  	LookAndFeel alloyLnF = new AlloyLookAndFeel(theme);
                      UIManager.setLookAndFeel(alloyLnF);
            	}catch(UnsupportedLookAndFeelException ex){
            	  ex.printStackTrace();	
            	}
           }
          }	
          
          
          11-2-3:輸出Option Dialog:
             Option Dialog可以讓用戶自定義對(duì)話框類型,比較具有彈性,最大的好處是可以改變按鈕上的文字.我們來(lái)看下面的例子:
          OptionDialog.java
          
          import java.awt.*;
          import javax.swing.*;
          import java.awt.event.*;
          import java.util.*;
          import com.incors.plaf.alloy.*;
          import com.incors.plaf.alloy.themes.glass.*;
          
          public class OptionDialog implements ActionListener
          {
              JFrame f = null;
              JLabel label = null;
              
              public OptionDialog() 
              {
                  f = new JFrame("OptionPane Demo");
                  Container contentPane = f.getContentPane();
                  
                  JButton b = new JButton("Show Option Dialog");
                  b.addActionListener(this);
                  
                  label = new JLabel(" ",JLabel.CENTER);
                  contentPane.add(label,BorderLayout.NORTH);
                  contentPane.add(b,BorderLayout.CENTER);
                  f.pack();
                  f.setVisible(true);
                  
                  f.addWindowListener(new WindowAdapter() {
                      public void windowClosing(WindowEvent e) {
                          System.exit(0);
                      }
                  });
              }    
              
              public static void main(String[] args)
              {
              	SwingUtil.setLookAndFeel();
                  new OptionDialog();
              }
              
              public void actionPerformed(ActionEvent e) 
              {
                  String title = "Option Dialog";
                  String message ="您喜歡吃漢堡嗎?";
                  int messageType = JOptionPane.QUESTION_MESSAGE;
                  //由于我們的optionType設(shè)置成JOptionPane.YES_NO_CANCEL_OPTION,因此對(duì)話框中會(huì)有三個(gè)按鈕.我們?cè)趏ptions
                  //的String Arrray中設(shè)置這三個(gè)按鈕的名稱,并以options[1]按鈕為默認(rèn)值,若將options參數(shù)設(shè)為null,系統(tǒng)會(huì)原來(lái) 
                  //的按鈕名稱來(lái)顯示.
                  int optionType = JOptionPane.YES_NO_CANCEL_OPTION;
                  String[] options = {"喜歡","不喜歡","取消"};
                  
                  int result = JOptionPane.showOptionDialog(f, message, title, 
                             optionType, messageType,null,options,options[1]);
                  
                  if (result == JOptionPane.YES_OPTION)
                      label.setText("您選擇:喜歡");
                  if (result == JOptionPane.NO_OPTION)
                      label.setText("您選擇:不喜歡");
                  if (result == JOptionPane.CANCEL_OPTION)
                      label.setText("您選擇:取消");
                  if (result == JOptionPane.CLOSED_OPTION)
                      label.setText("您沒(méi)做任何選擇,并關(guān)閉了對(duì)話框");
              }
          }
          
          class SwingUtil{
            public static final void setLookAndFeel() {
            	try{
                      Font font = new Font("JFrame", Font.PLAIN, 12);
                      Enumeration keys = UIManager.getLookAndFeelDefaults().keys();
           
                      while (keys.hasMoreElements()) {
                         Object key = keys.nextElement();
                         if (UIManager.get(key) instanceof Font) {
                            UIManager.put(key, font);
                          }
                      } 
                  	AlloyLookAndFeel.setProperty("alloy.isLookAndFeelFrameDecoration", "true");
                  	AlloyTheme theme = new GlassTheme();
                  	LookAndFeel alloyLnF = new AlloyLookAndFeel(theme);
                      UIManager.setLookAndFeel(alloyLnF);
            	}catch(UnsupportedLookAndFeelException ex){
            	  ex.printStackTrace();	
            	}
           }
          }	
          
          11-2-5:輸出Internal Dialog:
               我們之前曾經(jīng)說(shuō)過(guò),JOptionPane也可以顯示出Internal Dialog對(duì)話框,使用方法跟上面的范例一模一樣,只是在方法名稱上多
          了Internal這個(gè)字眼,例如showInternalMessageDialog()等等,我們來(lái)看下面的范例:
          
          import java.awt.*;
          import javax.swing.*;
          import java.awt.event.*;
          import java.util.*;
          import com.incors.plaf.alloy.*;
          import com.incors.plaf.alloy.themes.glass.*;
          
          public class InternalDialog implements ActionListener
          {
              JInternalFrame internalFrame = null;
              JLabel label = null;
              
              public InternalDialog() 
              {
                  JFrame f = new JFrame("OptionPane Demo");
                  Container contentPane = f.getContentPane();
                  
                  JDesktopPane desktopPane = new JDesktopPane(); 
                  internalFrame = new JInternalFrame(
                  "Internal Frame", true,  
                                    true,  
                                    true,  
                                    true); 
                  internalFrame.setLocation( 20,20);
                  internalFrame.setSize(200,200); 
                  internalFrame.setVisible(true);
                  
                  Container icontentPane = internalFrame.getContentPane();
                  JButton b = new JButton("Show Internal Dialog");
                  b.addActionListener(this);
                  icontentPane.add(b,BorderLayout.CENTER);
                  label = new JLabel(" ",JLabel.CENTER);
                  icontentPane.add(label,BorderLayout.NORTH);
                  
                  desktopPane.add(internalFrame);  
                  
                  contentPane.add(desktopPane,BorderLayout.CENTER);
                  f.setSize(350, 350);
                  f.setVisible(true);
                  
                  f.addWindowListener(new WindowAdapter() {
                      public void windowClosing(WindowEvent e) {
                          System.exit(0);
                      }
                  });
              }    
              
              public static void main(String[] args)
              {
              	SwingUtil.setLookAndFeel();
                  new InternalDialog();
              }
              
              public void actionPerformed(ActionEvent e) 
              {
                  String title = "Option Dialog";
                  String message ="您喜歡吃漢堡嗎?";
                  int messageType = JOptionPane.QUESTION_MESSAGE;
                  int optionType = JOptionPane.YES_NO_CANCEL_OPTION;
                  String[] options = {"喜歡","不喜歡","取消"};
                  
                  int result = JOptionPane.showInternalOptionDialog(internalFrame, message,
                            title, optionType, messageType,null,options,options[1]);
                  
                  if (result == JOptionPane.YES_OPTION)
                      label.setText("您選擇:喜歡");
                  if (result == JOptionPane.NO_OPTION)
                      label.setText("您選擇:不喜歡");
                  if (result == JOptionPane.CANCEL_OPTION)
                      label.setText("您選擇:取消");
                  if (result == JOptionPane.CLOSED_OPTION)
                      label.setText("您沒(méi)做任何選擇,并關(guān)閉了對(duì)話框");
              }
          }
          
          class SwingUtil{
            public static final void setLookAndFeel() {
            	try{
                      Font font = new Font("JFrame", Font.PLAIN, 12);
                      Enumeration keys = UIManager.getLookAndFeelDefaults().keys();
           
                      while (keys.hasMoreElements()) {
                         Object key = keys.nextElement();
                         if (UIManager.get(key) instanceof Font) {
                            UIManager.put(key, font);
                          }
                      } 
                  	AlloyLookAndFeel.setProperty("alloy.isLookAndFeelFrameDecoration", "true");
                  	AlloyTheme theme = new GlassTheme();
                  	LookAndFeel alloyLnF = new AlloyLookAndFeel(theme);
                      UIManager.setLookAndFeel(alloyLnF);
            	}catch(UnsupportedLookAndFeelException ex){
            	  ex.printStackTrace();	
            	}
           }
          }	
            讀者在使用Internal Dialog時(shí)要特別注意,一般我們利用JOptionPane所產(chǎn)生的對(duì)話框均是modal為true狀態(tài),可是當(dāng)你使用
          Internal Dialog時(shí)對(duì)話框會(huì)變成modal為false狀態(tài).因此你可以不用關(guān)閉之前的對(duì)話框,就可以再按一次按鈕,再產(chǎn)生一個(gè)Internal 
          Dialog.你再運(yùn)行上一例操作后就知道了.
          
          若你想將JInternalFrame內(nèi)的Internal Dialog model設(shè)為true,你可以有下面兩種解決方法:
          1.你可以建立JOptionPane對(duì)象,而非直接去調(diào)用JOptionPane的static方法來(lái)輸出對(duì)話框.然后利用JOptionPane的createDialog()方
            法,取得JDialog對(duì)象,再利用Dialog(JDialog繼承Dialog)所提供的setModal()方法將modal設(shè)為true.
          2.直接使用JDialog.
          
          11-3:使用JOptionPane組件建立對(duì)話框:
              我們?cè)?1-2節(jié)中已經(jīng)介紹了JOptionPane的構(gòu)造函數(shù),雖然大部份的情況下我們只需要使用JOptionPane的靜態(tài)方法來(lái)產(chǎn)生對(duì)話框
          ,但如果你想直接使用JOptionPane對(duì)象來(lái)產(chǎn)生對(duì)話框,當(dāng)然也可以.下面我們就舉一個(gè)以JOptionPane對(duì)象產(chǎn)生對(duì)話框的范例:
          
          import java.awt.*;
          import javax.swing.*;
          import java.awt.event.*;
          import java.util.*;
          import com.incors.plaf.alloy.*;
          import com.incors.plaf.alloy.themes.glass.*;
          
          public class OptionPaneDemo implements ActionListener
          {
              JFrame f = null;
              JLabel label = null;
              
              public OptionPaneDemo() 
              {
                  f = new JFrame("OptionPane Demo");
                  Container contentPane = f.getContentPane();
                  
                  JButton b = new JButton("Show Text Input");
                  b.addActionListener(this);
                  
                  label = new JLabel(" ",JLabel.CENTER);
                  contentPane.add(label,BorderLayout.NORTH);
                  contentPane.add(b,BorderLayout.CENTER);
                  f.pack();
                  f.setVisible(true);
                  
                  f.addWindowListener(new WindowAdapter() {
                      public void windowClosing(WindowEvent e) {
                          System.exit(0);
                      }
                  });
              }    
              
              public static void main(String[] args)
              {
              	SwingUtil.setLookAndFeel();
                  new OptionPaneDemo();
              }
              
              public void actionPerformed(ActionEvent e) 
              {
                  String title = "Input Dialog";
                  JLabel message =new JLabel("您最喜歡吃什么食物?",JLabel.CENTER);
                  int messageType = JOptionPane.QUESTION_MESSAGE;
                  int optionType = JOptionPane.OK_CANCEL_OPTION;
                  String result ="";
          
                  //利用message,messageType,optionType來(lái)建立JOptionPane對(duì)象
                  JOptionPane optionPane = new JOptionPane(message,messageType,optionType);
          
                  //利用JOptionPane的setWandtsInput()方法,使對(duì)話框有一個(gè)輸入字段讓用戶輸入信息
                  optionPane.setWantsInput(true);
           
                  //利用JOptionPane的setInitialSelectionValue()方法,使得輸入字段上的初始值為"請(qǐng)輸入";
                  optionPane.setInitialSelectionValue("請(qǐng)輸入!");
          
                  //利用JOptionPane的setInputValue()方法,使得當(dāng)用戶按下"Cancel"鍵或關(guān)閉對(duì)話框時(shí),result的默認(rèn)字符串為
                  //"你沒(méi)有輸入";
                  optionPane.setInputValue("您沒(méi)有輸入!");
                  JDialog dialog = optionPane.createDialog(f, title);
                  dialog.show();
                  
                  //JOptionPane的getInputValue()方法可以取得用戶輸入的信息.
                  result = (String)optionPane.getInputValue();
          
                  label.setText("您輸入:"+result);
              }
          }
          
          class SwingUtil{
            public static final void setLookAndFeel() {
            	try{
                      Font font = new Font("JFrame", Font.PLAIN, 12);
                      Enumeration keys = UIManager.getLookAndFeelDefaults().keys();
           
                      while (keys.hasMoreElements()) {
                         Object key = keys.nextElement();
                         if (UIManager.get(key) instanceof Font) {
                            UIManager.put(key, font);
                          }
                      } 
                  	AlloyLookAndFeel.setProperty("alloy.isLookAndFeelFrameDecoration", "true");
                  	AlloyTheme theme = new GlassTheme();
                  	LookAndFeel alloyLnF = new AlloyLookAndFeel(theme);
                      UIManager.setLookAndFeel(alloyLnF);
            	}catch(UnsupportedLookAndFeelException ex){
            	  ex.printStackTrace();	
            	}
           }
          }
          
           
          
          posted on 2005-12-19 09:39 風(fēng)風(fēng) 閱讀(1803) 評(píng)論(0)  編輯  收藏

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 蓝山县| 义乌市| 永济市| 新沂市| 襄垣县| 西乌| 将乐县| 福海县| 临汾市| 钦州市| 垫江县| 来宾市| 华坪县| 凯里市| 隆尧县| 西城区| 和田市| 潮安县| 龙南县| 铁岭县| 治多县| 洱源县| 石首市| 大庆市| 东源县| 阿拉善右旗| 黑山县| 醴陵市| 吐鲁番市| 衡阳市| 新营市| 剑河县| 湟中县| 漳州市| 孝感市| 盈江县| 桑植县| 泸西县| 喀什市| 八宿县| 垫江县|