Java——JRadioButton单选按钮的使用例子

阅读: 评论:0

Java——JRadioButton单选按钮的使用例子

Java——JRadioButton单选按钮的使用例子

┏(ω)=☞ 本专栏的目录(为您提供更好的查询方式)(点这里说不定有你想要的)

代码示例


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class a extends JFrame implements ActionListener{public static void main(String[] args) {new a();}JLabel JL1 = new JLabel ("用户名:");JLabel JL2  = new JLabel ("密码    :");JComboBox JC = new JComboBox(new String[] {"123","456"});JPasswordField JP1 = new JPasswordField(10);JButton JB1 = new JButton("确定");JButton JB2 = new JButton("取消");JRadioButton JR1 =new JRadioButton("用户登陆");//创建单选按钮JRadioButton JR2 =new JRadioButton("管理员登陆");//创建单选按钮ButtonGroup BG=new ButtonGroup(); //创建按钮组public a() {JPanel JP = (JPanel) ContentPane();JP.setLayout(new GridLayout(4,2,10,10));//设置网格布局四行二列上下左右间距为十JB1.addActionListener(this);JB2.addActionListener(this);BG.add(JR1);BG.add(JR2);JP.add(JL1);JP.add(JC);JP.add(JL2);JP.add(JP1);JP.add(JR1);JP.add(JR2);JP.add(JB1);JP.add(JB2);this.setSize(300,200);//设置窗口大小this.setLocationRelativeTo(null);//窗口居中this.setVisible(true);//窗口可见this.setDefaultCloseOperation(EXIT_ON_CLOSE);//设置关闭模式}@Overridepublic void actionPerformed(ActionEvent e) {Source()==JB1) {//是否点击JB1按钮if(JR1.isSelected()) {//是否选择了单选按钮的"用户登陆"SelectedItem().equals("123") && Text().equals("123")) JOptionPane.showMessageDialog(this,"欢迎您,用户"&#SelectedItem()+"登陆成功");else JOptionPane.showMessageDialog(this, "用户名或密码错误");}else if(JR2.isSelected()) {//是否选择了单选按钮的"管理员登陆"SelectedItem().equals("456") && Text().equals("456")) JOptionPane.showMessageDialog(this,"欢迎您,管理员"&#SelectedItem()+"登陆成功");else JOptionPane.showMessageDialog(this, "用户名或密码错误");}}Source()==JB2) {//是否点击JB2按钮"取消"按钮it(0);//关闭窗口}}
}

图例

本文发布于:2024-02-02 06:02:00,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170682491941840.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:单选   按钮   例子   Java   JRadioButton
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23