圖1.1java
圖1.2c++
分爲容器組件和經常使用功能組件。容器組件能夠容納其餘經常使用功能組件,而經常使用功能組件中不能放置其餘組件。編程
JFrame是一個包含標題和邊框的頂層窗口,是java.awt.Frame的擴展版本。數組
構造方法框架 |
說 明eclipse |
JFrame()jsp |
構造一個初始時不可見的新窗體ide |
JFrame(String title)工具 |
建立一個新的、初始不可見的、具備指定標題的 Frame佈局 |
須要顯示窗體:setVisible(true)方法
經常使用方法 |
說明 |
JRootPane getRootPane() |
返回此窗體的 rootPane 對象 |
Component add(Component comp) |
將指定組件添加到容器 |
void remove(Component comp) |
今後容器中移除指定組件 |
void pack() |
調整此窗口的大小,以適合其子組件的首選大小和佈局 |
void setSize(int width,int height) |
調整組件大小,使其寬度爲width,高度爲height |
Dimension getSize() |
返回窗體大小 |
JRootPane 的結構參見JDK API
public class TestJFrame { static JFrame winForm = new JFrame("測試窗體"); public static void main(String[] args) { try { // 獲取系統風格 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); // 將系統風格應用於窗體 SwingUtilities.updateComponentTreeUI(winForm); } catch (Exception e) { e.printStackTrace(); } winForm.setSize(500, 300);// 設置窗體大小 winForm.setLocationRelativeTo(null);// 窗體顯示器居中顯示 FlowLayout flow = new FlowLayout();// 實例化佈局管理器 winForm.getContentPane().setLayout(flow);// 將佈局管理器加入窗體 JButton btn1 = new JButton("按鈕1");// 實例化按鈕 JButton btn2 = new JButton("按鈕2"); JButton btn3 = new JButton("按鈕3"); winForm.getContentPane().add(btn1);// 添加按鈕到窗體 winForm.getContentPane().add(btn2); winForm.getContentPane().add(btn3); winForm.setVisible(true);// 窗體可見 } }
注意:
JPanel是輕量級容器,它能夠用來容納其餘的界面組件(窗體除外),且默認的佈局管理器是FlowLayout。
構造方法 |
說 明 |
JPanel() |
以默認佈局FlowLayout建立一個新面板 |
JPanel(LayoutManager layout) |
以指定的佈局管理器建立一個新面板 |
經常使用方法 |
說 明 |
void add(Component comp) |
將指定界面組件添加到面板 |
void remove(Component comp) |
從該面板中移除指定組件 |
void setLayout(LayoutManager layout) |
設置面板的佈局管理器 |
public static void main(String[] args) { JFrame winForm = new JFrame("JPanel測試");// 建立窗體 winForm.setSize(400, 300);// 設置窗體大小 winForm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 關閉按鈕 FlowLayout flow = new FlowLayout();// 佈局管理器 winForm.getContentPane().setLayout(flow); JPanel jp = new JPanel();// 建立JPanel對象 JButton btn1 = new JButton("按鈕1");// 建立按鈕 JButton btn2 = new JButton("按鈕2"); JButton btn3 = new JButton("按鈕3"); jp.setBackground(Color.GRAY);// 設定JPnel背景顏色 jp.add(btn1);// Jpanel對象添加按鈕 jp.add(btn2); jp.add(btn3); winForm.getContentPane().add(jp);// Jpanel對象添加到窗體 winForm.setVisible(true); }
JScrollPane是一個可滾動的視圖面板。它包括JScrollBar和JViewPort。
JScrollBar:水平或垂直的滾動對象,可選。
JViewPort:顯示數據的窗口對象(例如圖像文件)。
構造方法 |
說 明 |
JScrollPane() |
建立一個空的JScrollPane(沒有視圖區),其中水平和垂直滾動條在須要的時候出現。 |
JScrollPane(Component view) |
建立一個顯示指定組件內容的JScrollpane,其中水平和垂直滾動條在組件內容大於視圖範圍時出現。 |
經常使用方法 |
說 明 |
JViewport getViewport() |
返回當前JViewport對象 |
void setHorizontalScrollBarPolicy(int policy) |
肯定水平滾動條什麼時候顯示在滾動窗格上 |
void setVerticalScrollBarPolicy(int policy) |
肯定垂直滾動條什麼時候顯示在滾動窗格上 |
public static void main(String[] args) { JFrame jf = new JFrame("帶滾動條的視圖面板"); try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());// 獲取系統風格 SwingUtilities.updateComponentTreeUI(jf);// 將系統風格應用於窗體 } catch (Exception e) { e.printStackTrace(); } jf.setSize(400, 300);// 設置窗體大小 jf.setLocationRelativeTo(null);// 窗體顯示器居中顯示 FlowLayout flow = new FlowLayout();// 窗體設置佈局管理器 jf.getContentPane().setLayout(flow); JScrollPane jspImage = new JScrollPane();// 建立JScrollPane對象 Dimension ds = new Dimension(350, 250); // 封裝組件的高度和寬度 jspImage.setPreferredSize(ds);// 設置JScrollPane的高度和寬度 JLabel jlImage = new JLabel();// 建立放置圖像的JLabel標籤 ImageIcon image = new ImageIcon("E:\\Dev\\J-eclipse\\SwingDemo\\src\\com\\etc\\swing\\demo2\\auto.jpg");// 建立圖片對象 jlImage.setIcon(image);// 圖像放入JLabel標籤 jspImage.setViewportView(jlImage);// 把標籤添加到JScrollPane中 jspImage.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);// 設置垂直滾動條可見性 jspImage.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); // 設置水平滾動條可見性 jf.add(jspImage);// JScrollPane添加到窗體中 jf.setVisible(true);// 窗體可見 }
輸出效果:
注意:JScrollPane默認使用ScrollPaneLayout
佈局,不能更改其佈局。
1) 組件自左向右,自上向下佈局。
2) 遇到障礙折回,自動換行顯示。
3) 能夠設置每一行組件的對齊方式,默認居中對齊。
4) Panel的默認佈局管理器。
構造方法 |
說 明 |
FlowLayout() |
使用默認的居中對齊方式,水平和垂直間距爲默認值。 |
FlowLayout (int align) |
使用指定對齊方式,水平和垂直間距爲默認值。參數值爲:LEFT、RIGHT、CENTER。 |
FlowLayout (int align, int hgap,int vgap) |
使用指定的對齊方式,指定水平間距、垂直間距。 |
JFrame jf=new JFrame("流式佈局測試"); jf.setLayout(new FlowLayout());//如何居右顯示? for(int i=1;i<=6;i++){ jf.add(new JButton("按鈕"+i)); } jf.setSize(200, 200); jf.setVisible(true);
1) 按照地理方位佈局:上北下南左西右東+中間
2) Window、Frame、Dialog的默認佈局管理器
3) 組件須要明確指定添加到邊界佈局的位置
構造方法 |
說 明 |
BorderLayout() |
構造一個組件之間沒有間距的邊界佈局。 |
BorderLayout (int hgap,int vgap) |
構造一個具備指定組件間距的邊界佈局。水平間距由 hgap 指定,垂直間距由 vgap 指定。 |
add(new Button("North"), BorderLayout.NORTH);//上 add(new Button("South"), BorderLayout.SOUTH);//下 add(new Button("East"), BorderLayout.EAST);//右 add(new Button("West"), BorderLayout.WEST);//左 add(new Button("Center"), BorderLayout.CENTER);//中間
JFrame jf = new JFrame("邊界佈局測試"); jf.setLayout(new BorderLayout()); jf.add(new JButton("North"), BorderLayout.NORTH); jf.add(new JButton("South"), BorderLayout.SOUTH); jf.add(new JButton("East"), BorderLayout.EAST); jf.add(new JButton("West"), BorderLayout.WEST); jf.add(new JButton("Center"), BorderLayout.CENTER); jf.setSize(200, 200); jf.setVisible(true);
1) 網格形式。
2) 默認從左到右,從上到下添加組件。
3) 容器中每一個組件佔用大小徹底相同的一個區域。
4) 各個組件的大小由所在區域決定,組件自動長大到區域大小。
構造方法 |
說 明 |
GridLayout() |
建立具備默認值的網格佈局,即每一個組件佔據一行一列。 |
GridLayout(int rows, int cols) |
建立具備指定行數和列數的網格佈局。 |
GridLayout(int rows, int cols, int hgap, int vgap) |
建立具備指定行數和列數的網格佈局。組件之間的距離由hgap和vgap肯定。 |
JFrame jf = new JFrame("網格佈局測試"); jf.setLayout(new GridLayout(3,4)); for(int i=1;i<=12;i++){ jf.add(new JButton("按鈕"+i)); } jf.setSize(200, 200); jf.setVisible(true);
提示:
一、JFrame默認佈局爲邊界佈局。
二、輸入框爲JTextField文本框組件,放在JFrame上部。
三、計算器按鈕放在JPanel組件中,JPanel組件採用網格佈局。
組件的疊放形式,一堆組件放在容器中,每次只有最上面的組件課件
以時間方式管理組件而非按照空間方式管理
構造方法 |
說 明 |
CardLayout() |
建立具備默認值的卡片佈局。 |
CardLayout(int hgap, int vgap) |
建立具備指定行間距和列間距的卡片佈局。 |
構造方法 |
說 明 |
void first(Container) |
顯示第一個加入到CardLayout的卡片。 |
void last(Container) |
顯示最後一個加入到CardLayout的卡片。 |
void next(Container) |
顯示當前組件的下一個卡片。 |
void previous(Container) |
顯示當前組件的前一個卡片。 |
show(Container,String) |
在卡片父級容器中顯示指定名字的卡片。 |
public static void main(String[] args) { JFrame jf = new JFrame("卡片佈局測試");// 建立測試窗口 try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());// 獲取系統風格 SwingUtilities.updateComponentTreeUI(jf);// 將系統風格應用於窗體 } catch (Exception e) { e.printStackTrace(); } // 建立一個面板視圖,用於存放按鈕 JPanel menuJP = new JPanel(); menuJP.setLayout(new FlowLayout(FlowLayout.LEFT));// 流式佈局,居左顯示 JButton btnOne = new JButton("第一個選項卡"); JButton btnTwo = new JButton("第二個選項卡"); menuJP.add(btnOne); menuJP.add(btnTwo); jf.add(menuJP, BorderLayout.NORTH);// 按鈕面板添加到頂部 // 建立一個父級面板,用於存放卡片面板 final JPanel jpMain = new JPanel(); final CardLayout cardLayout = new CardLayout(); jpMain.setLayout(cardLayout); jf.add(jpMain, BorderLayout.CENTER);// 把父級面板放入窗體 // 建立兩個子級面板,顯示具體內容 JPanel jp1 = new JPanel(); JLabel jl1 = new JLabel("第一個選項卡頁面..."); jl1.setForeground(Color.white);// 設置標籤組件文字顏色 jp1.add(jl1); jp1.setBackground(Color.green);// 設置面板背景色爲綠色 jpMain.add(jp1, "1");// 子級面板添加到父級面板中,指定名稱爲"1" JPanel jp2 = new JPanel(); JLabel jl2 = new JLabel("第二個選項卡頁面..."); jl2.setForeground(Color.white);// 設置標籤組件文字顏色 jp2.add(jl2); jp2.setBackground(Color.red);// 設置面板背景色爲紅色 jpMain.add(jp2, "2");// 子級面板添加到父級面板中,指定名稱爲"2" // 設置按鈕監聽事件 class ActionListenerDemo implements ActionListener { @Override public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("第一個選項卡")) { cardLayout.show(jpMain, "1");// 在父級面板中顯示名稱爲"1"的子面板 } else { cardLayout.show(jpMain, "2");// 在父級面板中顯示名稱爲"2"的子面板 } } } ActionListenerDemo ald = new ActionListenerDemo();// 實例化監聽 btnOne.addActionListener(ald);// 註冊事件監聽 btnTwo.addActionListener(ald);// 註冊事件監聽 jf.setSize(500, 300);// 設置窗體大小 jf.setLocationRelativeTo(null);// 窗體顯示器居中顯示 jf.setVisible(true);// 窗體可見 }
運行效果
插 件 |
特 點 |
Visual Editor |
Eclipse官方提供。 |
SWT-Designer |
功能強大,收費。 |
jigloo |
收費。non_commercial版本免費,功能和收費版本同樣。 |
1) 項目包右鍵,new,other,myeclipse:
2) 填寫Matisse Form相關信息並選擇模板
上圖圖包含組件:
最簡單的swing組件之一,用於在框架上顯示標籤。
標籤能夠顯示文本,也能夠顯示圖像。
不可交互,不響應任何輸入事件,不能得到鍵盤和鼠標的響應。
構造方法 |
說 明 |
JLabel() |
建立無圖像而且其標題爲空字符串的 JLabel |
JLabel(Icon image) |
建立具備指定圖像的 JLabel 實例。image通常使用ImageIcon(String filename)構建。 |
JLabel(String text) |
建立具備指定文本的 JLabel 實例 |
經常使用方法 |
說 明 |
getText() |
返回標籤顯示的文本字符串 |
setText(String text) |
定義此組件將要顯示的單行文本 |
setIcon(Icon icon) |
定義標籤將顯示的圖標 |
在建立JLabel對象的時候,通常建議添加lbl前綴,把代碼規範變成本身的編碼習慣。
JFrame jf = new JFrame("JLabel測試"); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.getContentPane().setLayout(new FlowLayout()); // JLabel的三種構造方法 JLabel lbl1 = new JLabel(); JLabel lbl2 = new JLabel("Label2"); JLabel lbl3 = new JLabel(new ImageIcon("圖片url地址")); jf.add(lbl1); jf.add(lbl2); jf.add(lbl3); jf.setSize(300, 200); jf.setVisible(true);
swing中的文本框組件,提供輸入和編輯單行文本。
構造方法 |
說 明 |
JTextField() |
構造一個新的 TextField |
JTextField(String text) |
構造一個用指定文本初始化新的 TextField |
經常使用方法 |
說 明 |
getText() |
返回文本框中的文本字符串 |
setHorizontalAlignment(int alignment) |
設置文本的水平對齊方式 |
JTextField對象名前面加txt。
package com.etc.coponment; import java.awt.*; import javax.swing.*; public class JTextFieldT { static JFrame aWindow=new JFrame("JTextField Test"); static JTextField txtT1=null; static JTextField txtT2=null; public static void main(String[] args) { aWindow.setSize(400, 150); aWindow.setDefaultCloseOperation(JFrame .EXIT_ON_CLOSE); FlowLayout flow=new FlowLayout(); aWindow.getContentPane().setLayout(flow); //演示其2種構造方法 txtT1=new JTextField(); txtT2=new JTextField("JTextField"); aWindow.getContentPane().add(txtT1); aWindow.getContentPane().add(txtT2); //此方法用來演示JTextField的經常使用方法 setTxtT1Pro(); aWindow.setVisible(true); } public static void setTxtT1Pro(){ System.out.println(txtT2.getText()); txtT2.setHorizontalAlignment(JTextField.LEFT); //經常使用參數:JTextField.LEFT、JTextField.CENTER 和 JTextField.RIGHT } }
JTextArea組件從用戶接收多行文本,並容許用戶編輯輸入的文本。
構造方法 |
說 明 |
JTextArea() |
構造一個新的多行文本區 |
JTextArea(String text) |
構造一個用指定文本初始化新的多行文本區 |
經常使用方法 |
說 明 |
getText() |
返回文本區中的文本字符串 |
setFont(Font f) |
設置文本區的字體 |
JTextArea對象名前面加txa。
package com.etc.coponment; import java.awt.*; import javax.swing.*; public class JTextAreaT { static JFrame aWindow=new JFrame("JTextArea Test"); static JTextArea txaT1=null; static JTextArea txaT2=null; public static void main(String[] args) { aWindow.setSize(400, 150); aWindow.setDefaultCloseOperation(JFrame .EXIT_ON_CLOSE); FlowLayout flow=new FlowLayout(); aWindow.getContentPane().setLayout(flow); //演示其2種構造方法 txaT1=new JTextArea(); txaT2=new JTextArea("JTextArea"); aWindow.getContentPane().add(txaT1); aWindow.getContentPane().add(txaT2); //此方法用來演示經常使用方法 setPro(); aWindow.setVisible(true); } public static void setPro(){ System.out.println(txaT2.getText()); txaT2.setFont(new Font(txaT2.getText(),Font.ITALIC,24)); } }
JButton用來建立圖形用戶界面上的按鈕。經過給JButton註冊監聽事件,當按鈕的一個點擊事件觸發時,就會執行相應的動做。
構造方法 |
說 明 |
JButton() |
建立不帶文本或圖標的按鈕 |
JButton(Icon icon) |
建立一個帶圖標的按鈕 |
JButton(String text) |
建立一個帶文本的按鈕 |
JButton(String text, Icon icon) |
建立一個帶初始文本和圖標的按鈕 |
經常使用方法 |
說 明 |
void setRolloverIcon(Icon rolloverIcon) |
鼠標通過時顯示的圖標 |
void setPressedIcon(Icon pressedIcon) |
點擊按鈕時顯示的圖表 |
JButton對象名前面加btn。
package com.etc.coponment; import java.awt.*; import javax.swing.*; public class JButtonT { static JFrame aWindow=new JFrame("JTextArea Test"); static JButton btnT1=null; static JButton btnT2=null; static JButton btnT3=null; static JButton btnT4=null; public static void main(String[] args) { aWindow.setSize(500, 250); aWindow.setDefaultCloseOperation(JFrame .EXIT_ON_CLOSE); FlowLayout flow=new FlowLayout(); aWindow.getContentPane().setLayout(flow); //演示其4種構造方法 btnT1=new JButton(); btnT2=new JButton("JTextArea"); btnT3=new JButton(new javax.swing.ImageIcon( "D:\\Program Files\\HyperSnap6\\tmp\\圖0120.JPG")); btnT4=new JButton("JTextArea",new javax.swing.ImageIcon( "D:\\Program Files\\HyperSnap6\\tmp\\圖0120.JPG")); aWindow.getContentPane().add(btnT1); aWindow.getContentPane().add(btnT2); aWindow.getContentPane().add(btnT3); aWindow.getContentPane().add(btnT4); //此方法用來演示經常使用方法 setPro(); aWindow.setVisible(true); } public static void setPro(){ btnT1.setIcon(new javax.swing.ImageIcon( "D:\\Program Files\\HyperSnap6\\tmp\\圖0122.JPG")); btnT1.setRolloverIcon(new javax.swing.ImageIcon( "D:\\Program Files\\HyperSnap6\\tmp\\圖0121.JPG")); btnT1.setPressedIcon(new javax.swing.ImageIcon( "D:\\Program Files\\HyperSnap6\\tmp\\圖0120.JPG")); btnT3.setPressedIcon(new javax.swing.ImageIcon( "D:\\Program Files\\HyperSnap6\\tmp\\圖0120.JPG")); } }
建立可供用戶多選的複選框,能夠選擇也能夠取消,並顯示所選擇狀態。
構造方法 |
說 明 |
JCheckBox() |
建立一個沒有文本、沒有圖標而且最初未被選定的複選框 |
JCheckBox(Icon icon) |
建立有一個圖標、最初未被選定的複選框 |
JCheckBox(Icon icon, boolean selected) |
建立一個帶圖標的複選框,並指定其最初是否處於選定狀態 |
JCheckBox(String text) |
建立一個帶文本的、最初未被選定的複選框 |
JCheckBox(String text, boolean selected) |
建立一個帶文本的複選框,並指定其最初是否處於選定狀態 |
經常使用方法 |
說 明 |
Icon getIcon() |
返回默認圖標 |
String getText() |
返回JCheckBox對象的文本 |
void setVerticalAlignment(int alignment) |
設置圖標和文本的垂直對齊方式 |
int getVerticalAlignment() |
返回文本和圖標的垂直對齊方式 |
void setEnabled(boolean b) |
啓用或者禁用JCheckBox對象 |
void setText(String text) |
設置JCheckBox對象的文本 |
boolean isSelected() |
返回按鈕的選擇狀態 |
JCheckBox對象名前面加chk。
package com.etc.coponment; import java.awt.*; import javax.swing.*; public class JCheckBoxT { static JFrame aWindow=new JFrame("JTextArea Test"); static JCheckBox chkT1=null; static JCheckBox chkT2=null; public static void main(String[] args) { aWindow.setSize(500, 250); aWindow.setDefaultCloseOperation(JFrame .EXIT_ON_CLOSE); FlowLayout flow=new FlowLayout(); aWindow.getContentPane().setLayout(flow); //演示其2種構造方法 chkT1=new JCheckBox("JCheckBox"); chkT2=new JCheckBox("JCheckBox",new javax.swing.ImageIcon( "D:\\Program Files\\HyperSnap6\\tmp\\圖0122.JPG"),true); aWindow.getContentPane().add(chkT1); aWindow.getContentPane().add(chkT2); //此方法用來演示經常使用方法 setPro(); aWindow.setVisible(true); } public static void setPro(){ chkT2.setVerticalAlignment(SwingConstants.BOTTOM); System.out.println(chkT1.getIcon()); System.out.println(chkT1.getText()); System.out.println(chkT2.getVerticalAlignment()); } }
建立單選按鈕的組件。一般多個JRadioButton對象和一個ButtonGroup對象一塊兒使用。
構造方法 |
說 明 |
JRadioButton() |
建立初始非選中的按鈕,不設置文本 |
JRadioButton(String text) |
用指定文本建立非選中按鈕 |
JRadioButton(String text, boolean selected) |
用指定文本和選中狀態建立按鈕 |
經常使用方法 |
說 明 |
Icon getIcon() |
返回默認圖標。沒有圖標返回null |
String getText() |
返回JRadioButton對象文本 |
void setEnabled(Boolean b) |
啓用或者禁用JRadioButton對象 |
void setText(String text) |
設置JRadioButton對象文本 |
boolean isSelected() |
返回按鈕選擇狀態 |
JRadioButton對象名前面加rad。
package com.etc.coponment; import java.awt.*; import java.util.*; import javax.swing.*; public class JRadioButtonT { static JFrame aWindow=new JFrame("JTextArea Test"); static JRadioButton radT1=null; static JRadioButton radT2=null; static JRadioButton radT3=null; public static void main(String[] args) { aWindow.setSize(500, 250); aWindow.setDefaultCloseOperation(JFrame .EXIT_ON_CLOSE); FlowLayout flow=new FlowLayout(); aWindow.getContentPane().setLayout(flow); //演示其3種構造方法 radT1=new JRadioButton(); radT2=new JRadioButton("男"); radT3=new JRadioButton("男",true); aWindow.getContentPane().add(radT1); aWindow.getContentPane().add(radT2); aWindow.getContentPane().add(radT3); //此方法用來演示經常使用方法 setPro(); aWindow.setVisible(true); } public static void setPro(){ System.out.println(radT1.getIcon()); System.out.println(radT2.getText()); radT3.setEnabled(false); System.out.println(radT3.isSelected()); } }
建立可供用戶選擇的下拉框。可編輯狀態下可接收文本。
構造方法 |
說 明 |
JComboBox() |
建立一個不包含選擇項的下拉框對象 |
JComboBox(Object[] items) |
建立一個包含指定數組中元素的下拉框對象 |
JComboBox(Vector<?> items) |
建立一個包含指定Vector中元素的下拉框對象 |
經常使用方法 |
說 明 |
addItem(Object obj) |
將列表項添加到下拉框中 |
getItemAt(int index) |
返回指定索引位置的列表項。列表項索引從0開始。 |
getItemCount() |
返回列表項最大數目 |
getSelectedItem() |
返回當前選擇的列表項 |
getSelectedIndex() |
返回當前選擇的索引 |
JComboBox對象名前面加cbo。
package com.etc.coponment; import java.awt.*; import javax.swing.*; import java.util.*; public class JComboBoxT { static JFrame aWindow=new JFrame("JTextArea Test"); static JComboBox cboT1=null; static JComboBox cboT2=null; static JComboBox cboT3=null; static JComboBox cboT4=null; static JLabel lbl1=null; public static void main(String[] args) { aWindow.setSize(500, 250); aWindow.setDefaultCloseOperation(JFrame .EXIT_ON_CLOSE); FlowLayout flow=new FlowLayout(); aWindow.getContentPane().setLayout(flow); Vector vec=new Vector(); vec.add("選項一"); vec.add("選項二"); //演示其4種構造方法 cboT1=new JComboBox(); cboT2=new JComboBox(new String[]{"選項一","選項二"}); cboT3=new JComboBox(vec); cboT4=new JComboBox(JComboBoxItem.addItem()); lbl1=new JLabel(); aWindow.getContentPane().add(cboT1); aWindow.getContentPane().add(cboT2); aWindow.getContentPane().add(cboT3); aWindow.getContentPane().add(cboT4); aWindow.getContentPane().add(lbl1); //此方法用來演示經常使用方法 setPro(); aWindow.setVisible(true); } public static void setPro(){ cboT2.addItem(new String("選項三")); System.out.println(cboT2.getItemAt(2)); System.out.println(cboT2.getItemCount()); System.out.println(cboT2.getSelectedItem()); System.out.println(cboT2.getSelectedIndex()); cboT4.addItemListener(new java.awt.event.ItemListener() { public void itemStateChanged(java.awt.event.ItemEvent evt) { changed(evt); } }); } private static void changed(java.awt.event.ItemEvent evt) { // TODO add your handling code here: if(cboT4.getSelectedItem().toString().indexOf("圖0132.JPG")>-1) lbl1.setText("您選擇的是測試圖標"); else lbl1.setText("您選擇的是運行圖標"); } } package com.etc.coponment; import javax.swing.*; import java.awt.*; public class JComboBoxItem { public static ComboBoxModel addItem(){ DefaultComboBoxModel cbom=new DefaultComboBoxModel(); cbom.addElement(new ImageIcon("E:\\tmp\\圖0132.JPG")); cbom.addElement(new ImageIcon("E:\\tmp\\圖0133.JPG")); return cbom; } }
需求描述:
在JComboBox列出一些國家名稱。每當用戶在選擇一個國家的時候,將該國的國旗做爲標籤顯示,並在JTextArea中顯示該國的介紹。
完成時間:20分鐘。
需求描述:
設計和完成學生信息添加窗體的製做。
要求必要的驗證
完成時間:30分鐘。
JTable是一個表格控件,它容許開發人員經過一個表格模型來組織和顯示數據,並且經過相應的設置能夠編輯表格中的數據。
構造方法 |
說 明 |
JTable() |
構造一個默認的 JTable,使用默認的數據模型、默認的列模型和默認的選擇模型對其進行初始化。 |
JTable(int numRows, int numColumns) |
使用 DefaultTableModel 構造具備 numRows 行和 numColumns 列個空單元格的 JTable。 |
JTable(Object[][] rowData, Object[] columnNames) |
構造一個 JTable 來顯示二維數組 rowData 中的值,其列名稱爲 columnNames。 |
JTable(TableModel dm) |
構造一個 JTable,使用數據模型 dm、默認的列模型和默認的選擇模型對其進行初始化。 |
JTable(Vector rowData, Vector columnNames) |
構造一個 JTable 來顯示 Vector 所組成的 Vector rowData 中的值,其列名稱爲 columnNames。 |
方法 |
說 明 |
void addColumn(TableColumn aColumn) |
將 aColumn 追加到此 JTable 的列模型所保持的列數組的尾部。 |
void columnAtPoint(Point point) |
返回 point 所在的列索引;若是結果不在 [0, getColumnCount()-1] 範圍內,則返回 -1。 |
int getColumnCount() |
返回列模型中的列數。 |
String getColumnName(int column) |
返回出如今視圖中 column 列位置處的列名稱。 |
int getSelectedRowCount() |
返回選定行數。 |
int rowAtPoint(Point point) |
返回 point 所在的行索引;若是結果不在 [0, getRowCount()-1] 範圍內,則返回 -1。 |
Object getValueAt(int row, int column) |
返回 row 和 column 位置的單元格值。 |
獲取單元格的值示例:
Object selected = jTable1.getModel().getValueAt( jTable1.getSelectedRow(),jTable1.getSelectedColumn());
JTable對象名前加tbl
JTable對象不能直接添加到JFrame中,須要先添加到JScrollPane中,再把JscrollPane添加到JFrame中。
經過Matisse建立表格對象。
package com.etc.demo; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; public class JTableTest { JFrame jf = new JFrame("簡單表格"); JTable jt = null; // 表格數據 Object[][] tableData = { new Object[] { "Java", "23%", "持平" }, new Object[] { "Python", "21%", "上升" }, new Object[] { "c", "19%", "持平" }, new Object[] { "c++", "20%", "持平" }, new Object[] { "PHP", "18%", "上升" } }; // 表頭 Object[] columnTitle = { "語言", "佔用比率", "趨勢" }; public void init() { jt = new JTable(tableData, columnTitle); jf.add(new JScrollPane(jt)); jf.pack(); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setVisible(true); } public static void main(String[] args) { new JTableTest().init(); } }
形象化展現分層數據的樹形組件。
根節點:上不包含節點,下包含節點。
枝節點:上下都包含節點。
葉節點:上包含節點,下不包含節點。
相似磁盤(根節點)、文件夾(枝節點)和文件(葉節點)的關係。
構造方法 |
說 明 |
JTree() |
返回帶有示例模型的 JTree。 |
JTree(Hashtable<?,?> value) |
返回從 Hashtable 建立的 JTree,它不顯示根。 |
JTree(Object[] value) |
返回 JTree,指定數組的每一個元素做爲不被顯示的新根節點的子節點。 |
JTree(TreeNode root) |
返回 JTree,指定的 TreeNode 做爲其根,它顯示根節點。 |
JTree(Vector<?> value) |
返回 JTree,指定 Vector 的每一個元素做爲不被顯示的新根節點的子節點。 |
方法 |
說 明 |
void collapsePath(TreePath path) |
確保指定路徑標識的節點是摺疊的,而且可查看。 |
void collapseRow(int row) |
確保指定行中的節點是摺疊的。 |
void expandPath(TreePath path) |
確保指定路徑標識的節點展開,而且可查看。 |
void expandRow(int row) |
確保指定行中的節點展開,而且可查看。 |
TreePath getPathForRow(int row) |
返回指定行的路徑。 |
int[] getSelectionRows() |
返回全部當前選擇的行。 |
JTree不能直接添加到JFrame對象中,須要先添加到JScrollPane中。使用JScrollPane的setViewportView(Jtree jt)添加。
package com.etc.demo; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTree; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeModel; public class JTreeTest { JFrame f = new JFrame("樹形節點測試"); JScrollPane jsp = new JScrollPane(); public void init() { // 建立根節點 DefaultMutableTreeNode root = new DefaultMutableTreeNode("根節點"); // 建立枝節點 DefaultMutableTreeNode parent = new DefaultMutableTreeNode("枝節點"); // 建立葉節點 DefaultMutableTreeNode leaf = new DefaultMutableTreeNode("葉節點"); // 關聯節點 parent.add(leaf); root.add(parent); // 添加到帶滾動條的面板中 DefaultTreeModel dtm= new DefaultTreeModel(root); JTree jt = new JTree(dtm); jsp.setViewportView(jt);//屬性菜單添加到JScrollPane中 f.add(jsp); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); } public static void main(String[] args) { new JTreeTest().init(); } }
需求描述:
完成以下公司組織結構的樹形菜單開發。
完成時間:20分鐘。
窗體上的選項列表。做爲軟件功能或者任務的劃分,指明用戶的操做。
JMenuBar:菜單欄的實現。
JMenu:包含了JMenuItem的彈窗窗口。
JMenuItem:菜單項的實現。當用戶選中他的時候就會執行相應的操做。
package com.etc.demo; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; public class JMenuTest { JFrame jf = new JFrame("菜單欄測試"); public void init() { // 建立菜單欄 JMenuBar mnuBar = new JMenuBar(); // 建立JMenu對象 JMenu mnuFile = new JMenu(); JMenu mnuEdit = new JMenu(); JMenu mnuView = new JMenu(); JMenu mnuHelp = new JMenu(); // 建立JMenuItem對象 JMenuItem mnuiNew = new JMenuItem(); JMenuItem mnuiOpen = new JMenuItem(); JMenuItem mnuiSave = new JMenuItem(); JMenuItem mnuiSaveAs = new JMenuItem(); JMenuItem mnuiExit = new JMenuItem(); // 爲菜單項賦顯示值 mnuFile.setText("文件"); mnuEdit.setText("編輯"); mnuView.setText("視圖"); mnuHelp.setText("幫助"); mnuiNew.setText("新建"); mnuiOpen.setText("打開"); mnuiSave.setText("保存"); mnuiSaveAs.setText("另存爲"); mnuiExit.setText("退出"); // 關聯菜單項 mnuBar.add(mnuFile); mnuBar.add(mnuEdit); mnuBar.add(mnuView); mnuBar.add(mnuHelp); mnuFile.add(mnuiNew); mnuFile.add(mnuiOpen); mnuFile.add(mnuiSave); mnuFile.add(mnuiSaveAs); mnuFile.add(mnuiExit); // 把菜單放入窗體中 jf.setSize(300, 200); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setJMenuBar(mnuBar); jf.setVisible(true); } public static void main(String[] args) { new JMenuTest().init(); } }