java性别按钮_swing控件的介绍(开始是一个程序演示)

本文档介绍了如何使用Java Swing库创建一个用户信息输入界面,包括编号、姓名、性别按钮(男性和女性)、省份及城市组合框。通过JRadioButton和JComboBox控件的使用,展示了如何实现用户性别选择和根据省份动态更新城市列表。同时,添加了添加按钮,用于将用户信息添加到文本区展示。文章还提供了主要Swing组件如JComboBox、JRadioButton、JButton等的详细说明和使用方法。
摘要由CSDN通过智能技术生成

package picture;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class UserJFrame extends JFrame implements ActionListener,ItemListener {

private int number=1;                          //编号

private JTextField text_number,text_name;      //编号,姓名文本行

private JRadioButton radiobutton_male,radiobutton_female;   //性别按钮

private JComboBox combobox_province,combobox_city;          //省份,城市组合框

private JButton button_add;                                 //添加按钮

private JTextArea text_user;                               //文本区

public UserJFrame()

{

super("输入用户信息");

this.setSize(360,200);

this.setLocation(300,240);

this.setDefaultCloseOperation(EXIT_ON_CLOSE);    //单击窗口关闭按钮时,结束程序的运行

this.setLayout(new GridLayout(1,2));             //网格布局,1行2列,左右分隔窗口

text_user=new JTextArea();                       //创建文本区

this.add(text_user);                  //占据窗口左半部分

JPanel panel=new JPanel(new GridLayout(6,1));      //面板网格布局,6行1列

this.add(panel);                              //占据窗口的右半部分

text_number=new JTextField("1");        //编号文本行

text_number.setEditable(false);                //不可编辑,自动生成编号

panel.add(text_number);

text_name=new JTextField("姓名");

panel.add(text_name);

JPanel panel_radiobutton=new JPanel(new GridLayout(1,2));  //单选按钮子面板,网格

panel.add(panel_radiobutton);

ButtonGroup buttongroup=new ButtonGroup();      //按钮组

radiobutton_male=new JRadioButton("男",true);

buttongroup.add(radiobutton_male);                //单选按钮添加到按钮组

panel_radiobutton.add(radiobutton_male);          //单选按钮添加到子面板

radiobutton_female=new JRadioButton("女");

buttongroup.add(radiobutton_female);

panel_radiobutton.add(radiobutton_female);

Object province[]={"江苏省","浙江省"};

combobox_province=new JComboBox(province);     //省份组合框

combobox_province.addItemListener(this);      //注册组合框的选择事件监听器

panel.add(combobox_province);

Object city[]={"南京市","苏州市","无锡市"};

combobox_city=new JComboBox(city);

panel.add(combobox_city);

button_add=new JButton("添加");

button_add.addActionListener(this);

panel.add(button_add);

this.setVisible(true);

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值