一、JButtonjava
Swing的按鈕組件類spa
構造方法 | 說明 |
JButton() | 無圖標,文字 |
JButton(Icon icon) | 有圖標 |
JButton(String text) | 有文字 |
JButton(String text,Icon icon) | 有圖標文字 |
package Eleven; import javax.swing.JFrame; import javax.swing.ImageIcon; import javax.swing.WindowConstants; import javax.swing.JButton; import java.awt.FlowLayout; public class JButtonD extends JFrame{ public static void main(String[] args){ JButtonD frame = new JButtonD(); frame.setVisible(true); } public JButtonD(){ super(); setTitle("JButton按鈕"); ImageIcon editIcon = new ImageIcon(getClass().getResource("modify.jpg")); ImageIcon delIcon = new ImageIcon(getClass().getResource("del.jpg")); getContentPane().setLayout(new FlowLayout()); setBounds(100,100,215,74); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); final JButton editButton = new JButton("edit",editIcon); getContentPane().add(editButton); final JButton delButton = new JButton("del",delIcon); getContentPane().add(delButton); } }
二、單選按鈕組件JRadioButtoncode
構造方法 | 說明 |
JRadioButton() | |
JRadioButton(Icon icon) | 有圖標 |
JRadioButton(String text) | 文字 |
JRadioButton(String text,Icon icon) | 圖標、文字 |
JRadioButton(String text,boolean selected) | 文本,選中狀態 |
package Eleven; /*單選按鈕JRadioButton*/ import javax.swing.JFrame; import javax.swing.ButtonGroup; import java.awt.GridLayout; import javax.swing.JRadioButton; public class JRadioButtonD extends JFrame{ public static void main(String [] args){ JRadioButtonD frame = new JRadioButtonD(); frame.setVisible(true); } public JRadioButtonD(){ super(); /*單選按鈕組的組合須要使用ButtonGroup類將按鈕分組,經過add()方法 * 將按鈕添加到按鈕組中。這樣組合起來的按鈕組中同一時間只能有一個按鈕能夠被選中*/ ButtonGroup group = new ButtonGroup(); getContentPane().setLayout(new GridLayout(1,0)); setBounds(100,100,230,87); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JRadioButton radioButton = new JRadioButton(); group.add(radioButton); radioButton.setText("A"); getContentPane().add(radioButton); final JRadioButton radioButton2 = new JRadioButton("B"); group.add(radioButton2); //radioButton2.setText("B"); getContentPane().add(radioButton2); //javax.swing.JRadioButton.JRadioButton(String text, boolean selected) //選中 final JRadioButton radioButton3 = new JRadioButton("C",true); group.add(radioButton3); radioButton3.setText("C"); getContentPane().add(radioButton3); setVisible(true); } }
三、複選框組件JCheckBox對象
package Eleven; import javax.swing.JFrame; import javax.swing.JCheckBox; import javax.swing.JLabel; public class JCheckBoxD extends JFrame{ public JCheckBoxD(){ super(); setTitle("JCheckBox複選框"); getContentPane().setLayout(null); /*窗體對象調用setBounds()參數表明窗體在整個屏幕上出現的位置 * 組件調用表明組件在窗體中的位置大小*/ setBounds(100,100,230,103); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JCheckBox cb0 = new JCheckBox(); cb0.setBounds(10,34,64,26); cb0.setText("Tennis"); getContentPane().add(cb0); JCheckBox cb1 = new JCheckBox(); cb1.setBounds(84,34,104,26); cb1.setText("Tabletennis"); cb1.setSelected(true); getContentPane().add(cb1); JCheckBox cb2 = new JCheckBox(); cb2.setBounds(188,34,64,26); cb2.setText("swim"); getContentPane().add(cb2); final JLabel label = new JLabel(); label.setText("what's your favorite"); label.setBounds(10,10,191,18); getContentPane().add(label); setVisible(true); } public static void main(String[] args){ new JCheckBoxD(); } }