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;
private JTextField tfdID, tfdName;
private JRadioButton rbnMale, rbnFemale;
private JComboBox cbxProvince, cbxCity;
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));
taInfo = new JTextArea();
taInfo.setFont(new Font("", Font.PLAIN, 16));
taInfo.setEditable(false);
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);
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]);
}
}
}
}