javaswing的動態增加控件: class dynmaicBtnListener implements java.awt.event.ActionListener{ @Override public void actionPerformed(ActionEvent e) { System.out.println("new button clicked."); } } private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { CenterViewPanel.setLayout(new BoxLayout(CenterViewPanel, BoxLayout.Y_AXIS)); JButton newbtn = new JButton("MyDynamicButton"); newbtn.addActionListener(new dynmaicBtnListener ()); CenterViewPanel.add(newbtn); // CenterViewPanel.validate(); // CenterViewPanel.repaint(); CenterViewPanel.revalidate(); } 在動態增加控件的時候,必須在add之前調用layout設置方法。不然沒有效果。 CenterViewPanel.setLayout(new BoxLayout(CenterViewPanel, BoxLayout.Y_AXIS)); 在add之后要調用 CenterViewPanel.validate(); CenterViewPanel.repaint(); 或 CenterViewPanel.revalidate(); 去刷新重繪控件。 javaswing的控件屬性: 了解各種用戶界面組件: JButton、JLabel、JTextField、JTextArea、JComboBox、 Jlist、JCheckBox、JRadioButton、JMenuBar、JMenu、JMenuItem、JCheckBoxMenuItem、JRadioButtonMenuItem、JScrollBar、JScrollPane、JTabbedPane等 一、JButton 按鈕是一種點擊時觸發行為事件的組件。 按鈕的四個構造方法: public Jbutton()創建一個空按鈕 public JButton(String text) 創建一個標有指定文字的按鈕 public JButton(Icon icon) 創建一個標有指定圖標的按鈕 public JButton(String text,Icon icon) 創建一個標有指定文字和圖標的按鈕 圖標:是一個固定大小的圖片,典型的圖標體形較小,用於裝飾組件。利用類ImageIcon可以從圖像文件中得到圖標,如:Icon icon=new ImageIcon(“photo.gif”); JButton 的屬性 text:按鈕上的標簽,例如可用jbt.setText(“OK”)給按鈕jbt設置標簽。 icon:按鈕上的圖標,例如可用jbt.setTextIcon(new ImageIcon(“a.gif”))。 mnemonic:指定熱鍵。同時按下ALT鍵和指定熱鍵相當於按下該按鈕。例如使用jbt.setMnemonic(‘O’)可將O設置為按鈕jbt的熱鍵。 horizontalAlignment:此屬性只有三個值SwingConstants.LEFT,SwingConstants.CENTER, SwingConstants.RIGHT。它指定按鈕上標簽的水平對齊方式。默認值為居中。 verticalAlignment:此屬性也取三個值SwingConstants.TOP, SwingConstants.CENTER和SwingConstants.BOTTOM。它指定按鈕上標簽的垂直對齊方式。默認值為居中。 horizontalTextPosition:此屬性有三個值SwingConstants.LEFT, SwingConstants.CENTER, SwingConstants.RIGHT。它指定文本相對於圖標的水平位置,默認為SwingConstants.RIGHT。對應方法setHorizontalTextPosition。 verticalTextPosition:此屬性有三個值SwingConstants.TOP, SwingConstants.CENTER, SwingConstants.BOTTOM。它指定文字相對圖標的垂直位置,默認值為SwingConstants.CENTER。對應方法setVerticalTextPosition。 二、JLabel 標簽是顯示一小段文字、一幅圖片或者二者皆有的區域。 它的六個構造方法如下: public JLabel() 創建一個空標簽。 public JLabel(String text,int horizontalAlignment) 創建一個指定內容字符串和水平對齊方式的標簽。其中水平對齊方式可取值SwingConstants.LEFT, SwingConstants.CENTER, SwingConstants.RIGHT。 public JLabel(String text) 創建一個指定文字的標簽。 public JLabel(Icon icon) 創建一個指定圖標的標簽。 public JLabel(Icon icon,int horizontalAlignment) 創建一個指定圖標和水平對齊方式的標簽。 public JLabel(String text,Icon icon,int horizontalAlignment) 創建一個指定文本、圖標和水平對齊方式的標簽。 例如,下面語句創建一個文本內容為“Interest Rate”的標簽: JLabel myLabel = new JLabel(“Interest Rate”); 下面的語句創建一個標簽,它使用文件“images/map.gif”中的圖像作圖標: JLabel mapLabel = new JLabel(new ImageIcon(“images/map.gif”); JLabel 的屬性 JLabel繼承了類Jcomponent的所有屬性,並具有JButton類的許多屬性,如: text icon horizontalAlignment verticalAlignment 三、JTextField 文本域是一個用戶可以輸入字符的輸入區。允許用戶輸入各種數據,如姓名和描述文字。 JTextField的四個構造方法: public JTextField() 創建一個空文本域。 public JTextField(int columns) 創建一個指定列數的空文本域。 public JTextField(String text) 用指定初始文字創建一個文本域。 public JTextField(String text,int columns) 創建一個文本域,並用指定文字和列數初始化。 JTextField 屬性 除了text、horizontalAlignment等屬性外,JTextField還有如下屬性: editable 布爾型屬性,表明用戶能否修改文本域。 columns 文本域的寬度。 JTextField 方法 getText() 從文本域返回字符串。 setText(String text) 將給定字符串寫入文本域當中 setEditable(boolean editable) 使文本域變為可編輯的,默認為true。 setColumns(int) 設置文本與的列數,文本域的長度可變。 四、JTextArea 如想讓用戶輸入多行文本,只能通過創建多個JTextField實例來實現,解決問題的更好辦法是使用JTextField,它允許用戶輸入多行文字。 JTextArea的三個構造方法: public JTextArea() 創建一個空的文本區。 JTextArea(int rows, int columns) 創建一個指定行數和列數的文本區。 JTextArea(String s, int rows, int columns) 創建一個指定文本、行數和列數的文本區。 JTextArea 屬性 除了text、editable、columns外,JTextArea還有以下屬性: lineWrap wrapStyleWord rows lineCount tabSize JTextArea 的方法 以下方法用於插入、追加和替換文本: public void inser(String s,int pos) 將字符串s插入到文本區的指定位置pos。 public void append(String s) 將字符串s添加到文本的末尾。 public void replaceRange(String s,int start,int end) 用字符串s替換文本中從位置start到end的文字。 五、JComboBox 組和框是一些項目的簡單列表,用戶能夠從中進行選擇。 JComboBox的兩個構造方法: public JComboBox()默認構造方法。 public JComboBox(Object[] stringItems) 帶有字符串列表的構造方法,其中stringItems是一個字符串數祖。 JComboBox的屬性 JComboBox的有用的屬性: selectedIndex int值,表示組合框中選定項的序號。 selectedItem Object類型,表示選定項。 JComboBox 的方法 有用的方法: public void addItem(Object item)在組和框中添加一個選項,它可以是任何對象。 public Object getItemAt(int index) 得到組合框中指定序號的項。 public void removeItem(Object anObject)刪除指定的項。 public void removeAllItems() 刪除列表中所有項。 六、JList 列表框的作用和組合框的作用基本相同,但它允許用戶同時選擇多項。 JList的兩個構造方法: JList() 創建一個空的列表框 JList(Object[] stringItems) 創建一個有初始項的列表框 JList不會自動滾動。給列表框加滾動條的方法與文本區相同,只需創建一個滾動窗格並將列表框加入其中即可。 Jlist的 屬性 selectedIndexd selectedIndices selectedValue selectedValues selectionMode visibleRowCount 七、JCheckBox 復選框是一種用戶能夠打開、關閉選項的組件,如同電燈開關一般。 JCheckBox的七個構造方法: JCheckBox() JCheckBox(String text) JCheckBox(String text, boolean selected) JCheckBox(Icon icon) JCheckBox(Icon icon, boolean selected) JCheckBox(String text, Icon icon) JCheckBox(String text, Icon icon, boolean selected) JCheckBox 的屬性 JCheckBox 除了具有JButton的所有屬性如text、icon、mnemonic、verticalAlignment、horizontalAlignment、horizontalTextPosition和verticalTextPosition 外,還有selected屬性,該屬性指明復選框是否被選中。 八、JRadioButton 單選按鈕,或叫選擇按鈕,讓用戶從一組組件中選擇唯一的一個選項。 JRadioButton的七個構造方法: JRadioButton() JRadioButton(String text) JRadioButton(String text, boolean selected) JRadioButton(Icon icon) JRadioButton(Icon icon, boolean selected) JRadioButton(String text, Icon icon) JRadioButton(String text, Icon icon, boolean selected) JRadioButton 的屬性 JRadioButton 除具有JButton的所有屬性如text、icon、mnemonic、verticalAlignment、 horizontalAlignment、horizontalTextPosition、verticalTextPosition外,還具有 selected屬性,該屬性指明單選按鈕是否被選中。 將單選鈕組合成組 單選按鈕可以象按鈕一樣添加到容器中。要將單選按鈕分組,需要創建java.swing.ButtonGroup的一個實例,並用add方法把單選按鈕添加到該實例中,如: ButtonGroup btg = new ButtonGroup(); btg.add(jrb1); btg.add(jrb2); 上述代碼創建了一個單選按鈕組,這樣就不能同時選擇jrb1和jrb2。 九、消息對話框 對話框通常用作臨時窗口,用來接受用戶的附加信息或提示用戶發生了某事件。 消息對話框是一種簡單卻頻繁使用的對話框,用來顯示消息提醒用戶。 消息對話框是模式的,即消息對話框消失前其他窗口均不可用。 創建消息對話框 使用 JOptionPane類中的靜態方法: showMessageDialog(Component parentComponent, Object message, String title, int messageType) parentComponet是對話框的父組件,對話框都是由它派生而來的。message是要顯示的對象,它通常是一個字符串。title是對話框的標題。messageType決定了所顯示消息的類型。 showMessageDialog(Component parentComponent, Object message, String title, int messageType, Icon icon) 除PLAIN_MESSAGE外,每種消息都有相應的圖標,以上方法可支持自制圖標。 javaswing的注冊事件: Swing 是目前Java中不可缺少的窗口工具組,是建立圖形化用戶界面(GUI)程序的強大工具。Java Swing組件自動產生各種事件來響應用戶行為。Java將事件封裝成事件類,並且為每個事件類定義了一個事件監聽器。一個組件注冊事件監聽器方法,表明該組件要響應指定事件。也就是說我們可以通過注冊監聽器,監聽事件源產生的事件,從而在事件處理程序中處理我們所需要處理的用戶行為。 Java Swing中處理各組件事件的一般步驟是: 1. 新建一個組件。 2. 將該組件添加到相應的面板。 3. 注冊監聽器以監聽事件源產生的事件 4. 定義處理事件的方法。 注冊事件我們一般采用兩種方式:一是:利用一個監聽器以及多個if語句來決定是哪個組件產生的事件;二是使用多個內部類來響應不同組件產生的各種事件,它又分兩種方式,一種是采用匿名內部類,一種是采用一般內部類。 下面我們采用以上三種方式來注冊事件。來說明以上三種方式是如何實現事件的處理方法。 一、采用一個監聽器多個if語句來實現 在這種方式下:我們要繼承ActionListener接口,並且要實現actionPerformed方法。通過getActionCommand()方法來獲取事件的事件源。 [java] view plain copy public class Test_01 extends JFrame implements ActionListener { Test_01() { JPanel panel = new JPanel(); JButton button1 = new JButton("按鈕一"); JButton button2 = new JButton("按鈕二"); panel.add(button1); panel.add(button2); this.getContentPane().add(panel); this.setVisible(true); button1.addActionListener(this); button2.addActionListener(this); } public void actionPerformed(ActionEvent e) { String source = e.getActionCommand(); if (source.equals("按鈕一")) { System.out.println("你按了按鈕一"); } if (source.equals("按鈕二")) { System.out.println("你按了按鈕二"); } } public static void main(String args[]) { new Test_01(); } } 利用一個監聽器來處理事件的缺點是:其實當處理的事件比較少的時候,這種方式還是一種比較好的方式,它簡單。當程序比較復雜時,需要一大串的if語句來實現。程序的代碼比較難閱讀和維護。 一、利用匿名內部類來是實現 [java] view plain copy public class Test_02 extends JFrame{ Test_02(){ JPanel panel = new JPanel(); JButton button1 = new JButton("按鈕一"); JButton button2 = new JButton("按鈕二"); panel.add(button1); panel.add(button2); this.getContentPane().add(panel); this.setVisible(true); button1.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e) { System.out.println("你按了按鈕一"); } }); button2.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e) { System.out.println("你按了按鈕二"); } }); } public static void main(String args[]){ new Test_02(); } } 使用匿名內部類來實現可以解決使用if來獲取事件源帶來的麻煩。但是使用匿名內部類同樣存在着一些問題。由於它是和事件組一起的。根據事件組在代碼中的位置不同,類的定義以及處理事件,同樣不便於閱讀。如果事件處理程序比較復雜,內部類中的代碼就會變的很長。 三、利用一般內部類來實現 [java] view plain copy public class Test_03 extends JFrame{ Test_03(){ JPanel panel = new JPanel(); JButton button1 = new JButton("按鈕一"); JButton button2 = new JButton("按鈕二"); panel.add(button1); panel.add(button2); this.getContentPane().add(panel); this.setVisible(true); button1.addActionListener(new Button1ActionListener()); button2.addActionListener(new Button2ActionListener()); } private class Button1ActionListener implements ActionListener{ public void actionPerformed(ActionEvent e) { System.out.println("你按了按鈕一"); } } private class Button2ActionListener implements ActionListener{ public void actionPerformed(ActionEvent e) { System.out.println("你按了按鈕二"); } } public static void main(String[] args) { new Test_03(); } } 利用一般內部類我們可以解決很多的問題,該方法避免了第二種方法中由於使用匿名內部類而導致的代碼混亂。它把所有的事件處理方法都集中在一塊,並且都具有有意義的名稱,程序非常容易閱讀與維護。單個的事件處理程序也可以被工具欄、菜單欄等重復使用。 基於上面的總結,我們一般采用第三種方法來注冊事件