一、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(); } }