package cn.hucu.gui4;
import java.awt.Color;
import java.awt.GraphicsConfiguration;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
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;
public class UserJFrame extends JFrame implements ActionListener {
private JTextArea textUser;
private JTextField textName,textNumber;
private JRadioButton radioMan,radioFamale;
private JComboBox comCity,comProvice;
private JCheckBox check;
private JButton btnAdd;
private int number=1;
private Object provinces[] = {"江苏省","浙江省","湖南省"};
private Object cities[][] = { {"南京市","苏州市","无锡市"},
{"杭州市","宁波市","温州市","衢州市"},
{"益阳市","长沙市","岳阳市","湘潭市"}
};
public UserJFrame(){
super("输入用户信息");
this.setBounds(100, 200, 360, 200);
this.getContentPane().setBackground(Color.cyan);
this.setDefaultCloseOperation(HIDE_ON_CLOSE);
this.setResizable(false);
this.getContentPane().setLayout(new GridLayout(1,2));
textUser=new JTextArea();
this.getContentPane().add(new JScrollPane(textUser) );
JPanel p=new JPanel();
p.setLayout(new GridLayout(6,1));
textNumber=new JTextField("1");
textNumber.setEditable(false);
p.add(textNumber);
textName=new JTextField("姓名");
p.add(textName);
JPanel pSex=new JPanel();
radioMan=new JRadioButton("男");
radioFamale=new JRadioButton("女");
radioMan.setSelected(true);
pSex.add(radioMan);
pSex.add(radioFamale);
p.add(pSex);
ButtonGroup group=new ButtonGroup();
group.add(radioMan);
group.add(radioFamale);
comProvice=new JComboBox(provinces);
p.add(comProvice);
comProvice.addActionListener(this);
comCity=new JComboBox(cities[0]);
p.add(comCity);
btnAdd=new JButton("添加");
p.add(btnAdd);
btnAdd.setActionCommand("a");
btnAdd.addActionListener(this);
this.getContentPane().add(p);
this.setVisible(true);
}
public static void main(String[] args) {
new UserJFrame();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==comProvice){
int i=comProvice.getSelectedIndex();
comCity.removeAllItems();
for(int j=0;j<cities[i].length;j++){
comCity.addItem(cities[i][j]);
}
}
if(e.getActionCommand().equals("a")){
String str=""+this.number+"、"+textName.getText()+":";
if(radioMan.isSelected()){
str+=" "+radioMan.getText();
}
if(radioFamale.isSelected()){
str+=""+radioFamale.getText();
}
str+=" "+comProvice.getSelectedItem()+" "+comCity.getSelectedItem();
textUser.append(str+"\n");
this.number++;
textNumber.setText(""+this.number);
}
}
}
import java.awt.Color;
import java.awt.GraphicsConfiguration;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
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;
public class UserJFrame extends JFrame implements ActionListener {
private JTextArea textUser;
private JTextField textName,textNumber;
private JRadioButton radioMan,radioFamale;
private JComboBox comCity,comProvice;
private JCheckBox check;
private JButton btnAdd;
private int number=1;
private Object provinces[] = {"江苏省","浙江省","湖南省"};
private Object cities[][] = { {"南京市","苏州市","无锡市"},
{"杭州市","宁波市","温州市","衢州市"},
{"益阳市","长沙市","岳阳市","湘潭市"}
};
public UserJFrame(){
super("输入用户信息");
this.setBounds(100, 200, 360, 200);
this.getContentPane().setBackground(Color.cyan);
this.setDefaultCloseOperation(HIDE_ON_CLOSE);
this.setResizable(false);
this.getContentPane().setLayout(new GridLayout(1,2));
textUser=new JTextArea();
this.getContentPane().add(new JScrollPane(textUser) );
JPanel p=new JPanel();
p.setLayout(new GridLayout(6,1));
textNumber=new JTextField("1");
textNumber.setEditable(false);
p.add(textNumber);
textName=new JTextField("姓名");
p.add(textName);
JPanel pSex=new JPanel();
radioMan=new JRadioButton("男");
radioFamale=new JRadioButton("女");
radioMan.setSelected(true);
pSex.add(radioMan);
pSex.add(radioFamale);
p.add(pSex);
ButtonGroup group=new ButtonGroup();
group.add(radioMan);
group.add(radioFamale);
comProvice=new JComboBox(provinces);
p.add(comProvice);
comProvice.addActionListener(this);
comCity=new JComboBox(cities[0]);
p.add(comCity);
btnAdd=new JButton("添加");
p.add(btnAdd);
btnAdd.setActionCommand("a");
btnAdd.addActionListener(this);
this.getContentPane().add(p);
this.setVisible(true);
}
public static void main(String[] args) {
new UserJFrame();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==comProvice){
int i=comProvice.getSelectedIndex();
comCity.removeAllItems();
for(int j=0;j<cities[i].length;j++){
comCity.addItem(cities[i][j]);
}
}
if(e.getActionCommand().equals("a")){
String str=""+this.number+"、"+textName.getText()+":";
if(radioMan.isSelected()){
str+=" "+radioMan.getText();
}
if(radioFamale.isSelected()){
str+=""+radioFamale.getText();
}
str+=" "+comProvice.getSelectedItem()+" "+comCity.getSelectedItem();
textUser.append(str+"\n");
this.number++;
textNumber.setText(""+this.number);
}
}
}