java小项目之用户信息添加



main 函数

package hncu.cn.baseCode.trainCode.gui.swing.userInfo;

public class Main {
	public static void main(String[] args) {
		Object  provinces[] = {"江苏省","浙江省"};
		Object  cities[][]={
				{"南京市","苏州市","无锡市","常州市"},
				{"杭州市","温州市","金华市","义乌市"}
		};
		UserFrame u = new UserFrame (provinces,cities);
		u.setVisible(true);
	}
}

JFrame

package hncu.cn.baseCode.trainCode.gui.swing.userInfo;

import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.Panel;
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.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

@SuppressWarnings("all")
public class UserFrame extends JFrame {
	private static final long serialVersionUID = 1L;
	private JTextArea tArea;
	private JTextField tfNum;// 不可编辑的ID信息
	private JTextField tfName;// 姓名
	private JRadioButton rbMan;// 单选按钮 男
	private JRadioButton rbGril;// 单选按钮 女
	private JComboBox cbProvience;// 组合框 省份
	private JComboBox cbCity;// 组合框 城市
	private JButton btAdd;// 添加按钮
	Object  provinces[];
	Object  cities[][];
	private int num = 1;
	public UserFrame(Object[] provinces,Object[][] citise) {
		super("用户信息");
		this.provinces = provinces;
		this.cities = citise;
		Container container = getContentPane();
		setBounds(400, 150, 500, 300);
		setLayout(new GridLayout(1, 2));
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		// 左边
		tArea = new JTextArea();
		tArea.setEditable(false);
//		tArea.setBackground(Color.LIGHT_GRAY);
		//添加滚动条
		container.add(new JScrollPane(tArea));//container.add(tArea);

		// 右边
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(6, 1));
		container.add(panel);
		//ID
		tfNum = new JTextField(""+num);
		tfNum.setEditable(false);
		panel.add(tfNum);
		//name
		tfName = new JTextField("姓名");
		panel.add(tfName);
		//sex
		JPanel panSex = new JPanel();
		panSex.setLayout(new GridLayout(1,2));
		panel.add(panSex);
		rbMan = new JRadioButton("男",true);
		rbGril = new JRadioButton("女");
		panSex.add(rbMan);
		panSex.add(rbGril);
		ButtonGroup bGroup = new ButtonGroup();
		bGroup.add(rbMan);
		bGroup.add(rbGril);
		//组合框
		//省份
		cbProvience = new JComboBox(provinces);
		panel.add(cbProvience);
		cbProvience.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				int index = cbProvience.getSelectedIndex();//省份选了第几个
				cbCity.removeAllItems();
				for (int i = 0; i < citise[index].length; i++) {
					cbCity.addItem(citise[index][i]);
				}
			}
		});
		//城市
		cbCity = new JComboBox(citise[0]);
		panel.add(cbCity);
		
		//添加
		btAdd = new JButton("添加");
		panel.add(btAdd);
		btAdd.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				//表现层技术边界
				//1收集参数: num(现成),name,sex,province,city
				String sexString = "";
				if(rbGril.isSelected()){
					sexString = "女";
				}else {
					sexString = "男";
				}
				UserInfo uf = new UserInfo();
				uf.setNum(num);
				uf.setName(tfName.getText());
				uf.setSex(sexString);
				uf.setPrivince((String )cbProvience.getSelectedItem());
				uf.setCity((String )cbCity.getSelectedItem());
				
				if(num ==1){
					tArea.append(uf.toString());
				}else {
					tArea.append("\n"+uf.toString());
					
				}
				num++;
				tfNum.setText(""+num);
			}
		});
	}
}

UserInfo

package hncu.cn.baseCode.trainCode.gui.swing.userInfo;

public class UserInfo {
	private int num;
	private String name;
	private String  sex;
	private String privince;
	private String city;
	public UserInfo() {
		super();
	}
	public UserInfo(int num, String name, String sex, String privince, String city) {
		super();
		this.num = num;
		this.name = name;
		this.sex = sex;
		this.privince = privince;
		this.city = city;
	}
	public int getNum() {
		return num;
	}
	public void setNum(int num) {
		this.num = num;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getPrivince() {
		return privince;
	}
	public void setPrivince(String privince) {
		this.privince = privince;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	@Override
	public String toString() {
		return num + "," + name + "," + sex + "," + privince + "," + city;
	}
	
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值