Swing组件中单选按钮是JRadioButton类,该类是JToggleButton的子类。JRadioButton类的构造方法如表12.7所示。 JRadioButton类的构造方法 构造方法 功能描述 public JRadioButton() 创建一个未选择内容的单选按钮,其文本未设定 public JRadioButton(Icon icon 表12.7中列出了JRadioButton类的构造方法,接下来通过案例来演示JRadioButton类的使用,如例12-7所示。 rb1=new JRadioButton("Java",true); 10 JRadioButton rb2=new JRadioButton("大数据"); 11 JRadioButton rb3=new JRadioButton("前端"); 12 ButtonGroup group=new ButtonGroup(); 13 /
import java.awt.Color; import javax.swing.JRadioButton; import java.awt.Font; /** * 通过继承JRadioButton 来实现为每个radioButton组件指定值 * @author waitatlee@163.com */ public class CustomRadioButton extends JRadioButton
(创建一个ButtonGroup对象,并使用其add方法在该组中包括JRadioButton对象。) 注意:ButtonGroup对象是一个逻辑分组,而不是物理分组。 构造方法: 无文本,未选中 JRadioButton() 有文本,未选中 JRadioButton(String text) 有文本,并指定是否选中 JRadioButton(String
在Swing中可以使用JRadioButton完成一组单选按钮的操作,JRadioButton的常用方法如下表。 ) 创建一个单选按钮,设定图片,并设定是否选中 3 public JRadioButton(String text) 创建一个单选按钮,制定按钮文本,默认不选中 4 public JRadioButton jradio1 = new JRadioButton("男"); private JRadioButton jradio2 = new JRadioButton("女"); //定义一个面板 private jradio1 = new JRadioButton("男"); private JRadioButton jradio2 = new JRadioButton("女"); //定义一个面板 private jradionmale = new JRadioButton("男"); private JRadioButton jradiofemale = new JRadioButton("女"); private
单选按钮(JRadioButton)和复选框(JCheckBox) 实现一个单选按钮(或复选框),此按钮项可被选择或取消选择,并显示其状态。 JRadioButton对象与ButtonGroup对象配合使用可创建一组按钮,一次只能选择其中的一个按钮。 单选按钮的构造方法有如下所示几点: 1) JRadioButton() 创建一个初始化为选择的单选按钮,其文本未设定 2) JRadioButton(Icon icon) 创建一个初始化为选择的单选按钮 4) JRadioButton(String text) 创建一个具有指定文本的状态为未选择的单项按钮。 ,把上面的JRadioButton换成JCheckBox便可得到复选框。
概述 官方JavaDocsApi: javax.swing.JRadioButton JRadioButton,单选按钮。 JButton 常用构造方法: // 无文本,未选中 JRadioButton() // 有文本,未选中 JRadioButton(String text) // 有文本,并指定是否选中 JRadioButton (String text, boolean selected) JRadioButton 常用方法: // 设置单选按钮的 文本、字体 和 字体颜色 void setText(String text) (WindowConstants.EXIT_ON_CLOSE); JPanel panel = new JPanel(); // 创建两个单选按钮 JRadioButton radioBtn01 = new JRadioButton("男"); JRadioButton radioBtn02 = new JRadioButton("女");
如上图:我给btn组件添加了事件监听事件,当触发了监听事件时(点击按钮)时,在控制台中输出字符串“123” JRadioButton JRadioButton来自javax.swing包下提供的JRadioButton 如何创建JRadioButton单选框: JRadioButton() radio = new JRadioButton(); 如果要创建多个单选框,可以使用数组方式创建: JRadioButton [] radio1 = new JRadioButton[6]; 若是出现多个单选框,但只能选中其中一个怎么办? java提供了ButtonGroup按钮组,我们只需要实例化按钮组 JRadioButton() group = new JRadioButton(); 实例化按钮组后,将要并在一起的单选框添加进一个按钮组中 复选框跟单选框是很类似的,可以把复选框认为是单选框不加按钮组的单选框; 如何创建JRadioButton单选框: JCeckBox() check = new JCeckBox(); 如果要创建多个单选框
; import javax.swing.WindowConstants; /** * 1:在默认情况下,单选按钮JRadioButton显示一个圆形图标,并且通常在该图标旁放置 * 一些说明性文字 class JRadioButtonTest extends JFrame{ private JRadioButton jr1=new JRadioButton(); private JRadioButton jr2=new JRadioButton(); private JRadioButton jr3=new JRadioButton();//初始化单选按钮 / ; import javax.swing.WindowConstants; public class Test extends JFrame{ private JRadioButton jr1 ("男"); jr2=new JRadioButton("女"); bg1=new ButtonGroup(); setLayout(new
按钮组件:JButton---普通按钮 JCheckBox---复选框 JRadioButton—单选框 JComboBox –下拉列表框 c) JTextField中常用方法 con.add(button); f) JRadioButton 单选框 i. 实例化:JRadioButton radio = new JRadioButton(String titile, Boolean Selected); g) ButtonGroup按钮组的使用 radio0 = new JRadioButton("Radio1"); 6 7 JRadioButton radio1 = new JRadioButton("Radio2 radio3 = new JRadioButton("Radio3"); iv.
import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JRadioButton ActionEvent e) { // TODO Auto-generated method stub console.setText("多选框被点击"); } }); JRadioButton jRadioButton = new JRadioButton("单选框"); centerP.add(jRadioButton); jRadioButton.addActionListener
(二)界面的设计 1.显示的界面:创建JFrame实例、JPanel面板,然后把面板添加到JFrame中; 2.构造一个按钮组对象ButtonGroup,把JRadioButton类型的对象添加到该按钮组中 JPanel(); private JLabel problem =new JLabel(); private ButtonGroup group=new ButtonGroup(); private JRadioButton buttona=new JRadioButton(); private JRadioButton buttonb=new JRadioButton(); private JRadioButton buttonc =new JRadioButton(); private JRadioButton buttond=new JRadioButton(); private String str_problem[]=new 四、总结 1.本文主要介绍了JLabel、JButton、JPanel、ButtonGroup、JRadioButton单选框组件的基本使用,完成界面的窗口、题目和选项、显示进度、显示按钮和时间、显示总分和表情
实验步骤: 1.定义类显示窗口,标题为“计算器” 2.在类中添加窗口组件作为类的成员变量, JLabel文本, JRadioButton为单选按钮, JTextField为输入框, JPanel ,j12,j13; //JTextField为输入框 JTextField jtf1,jtf2,jtf3; //JPanel为容器 JPanel jp1,jp2,jp3,jp4; //JRadioButton 为单选按钮 JRadioButton j1,j2,j3,j4; //ButtonGroup 为单选组按钮 ButtonGroup gd; CaculateDemo(){ //在构造方法为每个组件及容器申请内存空间 jp2=new JPanel(); jp3=new JPanel(); jp4=new JPanel(); j1=new JRadioButton ("+"); j2=new JRadioButton("-"); j3=new JRadioButton("*"); j4=new JRadioButton
public TestJRadioButton() { Container container = getContentPane(); //单选框 JRadioButton rb1 = new JRadioButton("RB1"); JRadioButton rb2 = new JRadioButton("RB2"); JRadioButton rb3 = new JRadioButton("RB3"); //由于单选框只能选一个,我们将他们成组 ButtonGroup buttonGroup = new ButtonGroup
import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JRadioButton add(label,BorderLayout.WEST); // 定义按钮组 bg = new ButtonGroup(); // 定义单选按钮 Radio_number = new JRadioButton ("按编号",true); Radio_name = new JRadioButton("按书名"); Radio_author = new JRadioButton("按作者"); // 将按钮添加到按钮组中 textField ; private JTable table_search ; private Book book_search ; private ButtonGroup bg; private JRadioButton Radio_number ; private JRadioButton Radio_name ; private JRadioButton Radio_author ; class SearchResult
panel5; private JTextField textField1; private JCheckBox professionalCheckBox; private JRadioButton sandpiperRadioButton; private JRadioButton mayfairRadioButton; private JRadioButton blackHawkRadioButton ; private JRadioButton theRanchRadioButton; private JTextField feeField; private JButton
六、单选按钮(JRadioButton) Swing 中通过类 JRadioButton 实例化单选按钮对象。 JRadioButton的构造方法 JRadioButton类的构造方法有以下几种: JRadioButton():创建一个默认未选中的单选按钮。 JRadioButton常用成员方法 JRadioButton类是Java Swing库中的一个组件,用于创建单选按钮。 radioButton1 = new JRadioButton("Option 1"); JRadioButton radioButton2 = new JRadioButton("Option 2"); JRadioButton radioButton3 = new JRadioButton("Option 3"); ButtonGroup buttonGroup
JRadioButton:单选按钮 作用:用于一组互斥选项。 常用方法: isSelected():检查单选按钮是否被选中。 使用 ButtonGroup 将多个按钮分组。 JRadioButtonExample { public static void main(String[] args) { JFrame frame = new JFrame("JRadioButton frame.setSize(300, 200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JRadioButton option1 = new JRadioButton("Option 1"); JRadioButton option2 = new JRadioButton("Option 2");
protected Matcher m; protected JTextField pattTF, strTF; protected JCheckBox compiledOK; protected JRadioButton }; JPanel switchPane = new JPanel(); ButtonGroup bg = new ButtonGroup(); match = new JRadioButton true); match.addChangeListener(cl); bg.add(match); switchPane.add(match); find = new JRadioButton Find"); find.addChangeListener(cl); bg.add(find); switchPane.add(find); findAll = new JRadioButton
panel5; private JTextField textField1; private JCheckBox professionalCheckBox; private JRadioButton sandpiperRadioButton; private JRadioButton mayfairRadioButton; private JRadioButton blackHawkRadioButton ; private JRadioButton theRanchRadioButton; private JTextField feeField; private JButton
---- 8.JRadioButton | 代表Swing单选按钮 构造方法 解释 JRadioButton() 创建一个没有设置文本的初始未选单选按钮。 JRadioButton(Action a) 创建一个radiobutton,其中的属性取自提供的操作。 JRadioButton(String text) 用指定的文本创建未选中的单选按钮。 JRadioButton jr =new JRadioButton(); ? 2.用指定的文本创建未选中的单选按钮。 JRadioButton jr =new JRadioButton("香蕉"); ?