小程序---信息添加

71 篇文章 0 订阅
5 篇文章 0 订阅
//package 课后练习.GUI.Swing;

import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class UserJFrame extends JFrame implements ActionListener {
    private JTextArea taInfo;// 显示user信息
    private JTextField tfdID, tfdName;// userID和user姓名
    private JRadioButton rbnMale, rbnFemale;// user性别
    private JComboBox cbxProvince, cbxCity;// user的省份和城市
    private JButton btnAdd;// 添加资料按钮

    private int ID = 1;

    private Object[][] citys;

    public UserJFrame(Object[] priObjects, Object[][] citys) {
        super("用户信息添加");
        this.citys = citys;
        setBounds(400, 200, 500, 300);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        Container c = getContentPane();
        c.setBackground(Color.WHITE);
        c.setLayout(new GridLayout(1, 2));
        // 添加显示信息块
        // JScrollPane p = new JScrollPane();
        taInfo = new JTextArea();
        taInfo.setFont(new Font("", Font.PLAIN, 16));
        // taInfo.add(p);
        taInfo.setEditable(false);
        // p.add(taInfo);
        c.add(taInfo);

        JPanel p = new JPanel();
        p.setLayout(new GridLayout(6, 1));
        c.add(p);

        tfdID = new JTextField("1");
        tfdID.setEditable(false);
        p.add(tfdID);

        tfdName = new JTextField("姓名");
        p.add(tfdName);

        JPanel pSex = new JPanel();
        pSex.setLayout(new GridLayout(1, 2));
        p.add(pSex);
        rbnMale = new JRadioButton("男", true);
        rbnFemale = new JRadioButton("女");
        pSex.add(rbnMale);
        pSex.add(rbnFemale);
        // 把rbnMale和rbnFemale打包形成互斥的单选按钮
        ButtonGroup bgp = new ButtonGroup();
        bgp.add(rbnMale);
        bgp.add(rbnFemale);

        cbxProvince = new JComboBox(priObjects);// 需要一维对象数组
        cbxProvince.addActionListener(this);

        p.add(cbxProvince);
        cbxCity = new JComboBox(citys[0]);// 需要一维对象数组
        p.add(cbxCity);

        btnAdd = new JButton("添加");
        btnAdd.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String name = tfdName.getText();
                String sex = "";
                if (rbnMale.isSelected()) {
                    sex = rbnMale.getText();
                } else {
                    sex = rbnFemale.getText();
                }
                String province = cbxProvince.getSelectedItem().toString();
                String city = cbxCity.getSelectedItem().toString();
                taInfo.append(ID + "," + name + "," + sex + "," + province + "," + city + "\n");
                ID++;
                tfdID.setText(ID + "");
                tfdName.setText("");
            }
        });
        p.add(btnAdd);

        setVisible(true);
    }

    public static void main(String[] args) {
        Object provinces[] = { "湖南省", "福建省" };
        Object citys[][] = { { "长沙市", "益阳市", "株洲市" }, 
                             { "福州市", "莆田市", "厦门市" } };
        new UserJFrame(provinces, citys);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if ( cbxProvince.getSelectedIndex()==1) {
            cbxCity.removeAllItems();
            for (int i = 0; i < citys[1].length; i++) {
                cbxCity.addItem(citys[1][i]);
            }
        }else {
            cbxCity.removeAllItems();
            for (int i = 0; i < citys[0].length; i++) {
                cbxCity.addItem(citys[0][i]);
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值