SWING 的圖形介面元件
JLabel JLabel label = new JLabel(" 關閉 "); JLabel label1 = new JLabel( new ImageIcon("tree.jpg")); JLabel(String,int), JLabel(ImageIcon, int) –JLabel.LEFT –JLabel.RIGHT –JLabel.CENTER –JLabel.LEADING –JLabel.TRAILING
JButton JButton button1 = new JButton(" 切換 (Alt_S)"); JButton button2 = new JButton( new ImageIcon("yellow.gif")); button1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { // 切換標題的文字內容 if ( flag == 0 ) label.setText(" 開啟 "); else label.setText(" 關閉 "); flag = 1 - flag; } });
JCheckBox greenBox = new JCheckBox(" 綠色 (G)"); greenBox.setMnemonic(KeyEvent.VK_G); greenBox.setSelected(true); greenBox.addItemListener(this); Ch11_1_2
JRadio ButtonGroup() –add() –remove() JRadionButton() –setMnemoic() –setSelected(boolean) –addItemListener(ItemListener) –removeItemListener() CH11_1_3
JComboBox String[] items = { " 程式語言 ", " 計算機概論 ", " 資料庫系統 ", " 網頁設計 "}; JComboBox list = new JComboBox(items); Ch11_1_4
JList String[] names = {" 陳會安 ", " 江小魚 ", " 小龍女 ", " 張三丰 "}; dlistModel = new DefaultListModel(); for (int i = 0; i < names.length; i++) dlistModel.addElement(names[i]); // 新增元素 nameList = new JList(dlistModel); Ch11_1_5
JScroll JScrollBar() JScrollBar(int) JScrollBar(int,int,int,int,int) s1=new JScrollBar(JScrollBar.VERTICAL,50,10,0,100);
JSlider JSlider() JSlider(int) JSlider(int,int) JSlider(int,int,int) JSlider(int,int,int,int) Ch11_1_6
JTextComponent JTextField and JPasswordField JTextField text = new JTextField(12); JPasswordField password = new JPasswordField(12); Ch_2_1
JTextArea JTextArea() JTextArea(String) JTextArea(int,int) JTextArea(String,int,int) area = new JTextArea(" 等待輸入資料...\n", 15, 30); JScrollPane scroll = new JScrollPane(area); Ch11_2_2
JPopupMenu popup = new JPopupMenu(); popup.add(blue = new JMenuItem(" 藍色 ")); blue.addActionListener(this); popup.add(yellow = new JMenuItem(" 黃色 ")); yellow.addActionListener(this); popup.add(green = new JMenuItem(" 綠色 ")); green.addActionListener(this);
JPopupMenu addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent evt) { if ( evt.isPopupTrigger() ) // 顯示選單 popup.show(evt.getComponent(), evt.getX(), evt.getY()); } public void mouseReleased(MouseEvent evt) { if ( evt.isPopupTrigger() ) // 顯示選單 popup.show(evt.getComponent(), evt.getX(), evt.getY()); } }); Ch11_3_1
JMenuBar JMenuBar jmb = new JMenuBar(); setJMenuBar(jmb); // 新增下拉式功能表
JMenu JMenu file = new JMenu(" 檔案 (F)"); file.setMnemonic(KeyEvent.VK_F); JMenuItem item; file.add(item = new JMenuItem(" 新增 (N)",KeyEvent.VK_N)); file.add(item = new JMenuItem(" 開啟 (O)",KeyEvent.VK_O)); JMenu setting = new JMenu(“ 參數設定 ”); // 子選單 file.add(setting); // 新增子選單 file.addSeparator(); // 分隔線 file.add(item = new JMenuItem(" 關閉 (X)",KeyEvent.VK_X)); jmb.add(file); // 新增 file 選單 Ch11_3_2
JToolBar JToolBar toolBar = new JToolBar(); blue = new JButton(new ImageIcon("blue1.gif")); blue.setToolTipText(" 藍色 "); blue.addActionListener(this); yellow = new JButton(new ImageIcon("yellow1.gif")); yellow.setToolTipText(" 黃色 "); yellow.addActionListener(this); green = new JButton(new ImageIcon("green1.gif")); green.setToolTipText(" 綠色 "); green.addActionListener(this); toolBar.add(blue); toolBar.add(yellow); toolBar.add(green); Ch11_3_3
JFileChooser final JFileChooser jfc = new JFileChooser(); int n = jfc.showOpenDialog(Ch11_4_1.this); if ( n == JFileChooser.APPROVE_OPTION ) { File file = jfc.getSelectedFile(); area.append(" 開啟檔案名稱 : "); area.append(file.getName() + "\n"); }
JColorChooser final JColorChooser jcc = new JColorChooser(); JButton button = new JButton(" 選擇背景色彩 "); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { Color newColor; newColor = jcc.showDialog(Ch11_4_2.this, " 選擇背景色彩 ", c.getBackground()); if ( newColor != null ) c.setBackground(newColor); } });