设置组合框可输入,并将输入字符串添加到组合框的下拉列表中 10
publicclassUserJFrameextendsJFrameimplementsActionListener{publicUserJFrame(Objectpro...
public class UserJFrame extends JFrame implements ActionListener
{
public UserJFrame(Object provinces[], Object cities[][])
{super("输入用户信息");
this.setBounds(300,240,360,200);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.getContentPane().setLayout(new GridLayout(1,2));
text_user = new JTextArea();
text_user.setEditable(false);
this.getContentPane().add(text_user);
JPanel panel = new JPanel(new GridLayout(6,1));
this.getContentPane().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_rb=new JPanel(new GridLayout(1,2));
panel.add(panel_rb);
ButtonGroup bgroup = new ButtonGroup();
radiob_male = new JRadioButton("男",true);
bgroup.add(radiob_male);
panel_rb.add(radiob_male);
radiob_female = new JRadioButton("女");
bgroup.add(radiob_female);
panel_rb.add(radiob_female);
this.cities = cities;
combox_province = new JComboBox(provinces);
combox_province.setEditable(true);
combox_province.addActionListener(this);
panel.add(combox_province);
combox_city = new JComboBox(cities[0]);
panel.add(combox_city);
button_add = new JButton("添加");
button_add.addActionListener(this);
panel.add(button_add);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{ if (e.getSource()==combox_province)
{ int i=combox_province.getSelectedIndex();
combox_city.removeAllItems();
for (int j=0; j<this.cities[i].length; j++)
combox_city.addItem(this.cities[i][j]);
} if (e.getSource() == button_add)
{ String aline=number+", "+text_name.getText();
if (radiob_male.isSelected())
aline += ", "+radiob_male.getText();
if (radiob_female.isSelected())
aline += ", "+radiob_female.getText();
aline += ", "+combox_province.getSelectedItem();
aline += ", "+combox_city.getSelectedItem();
text_user.append(aline+"\n");
this.number++;
text_number.setText(""+this.number);
text_name.setText("姓名");
}
}
public static void main(String arg[]) 展开
{
public UserJFrame(Object provinces[], Object cities[][])
{super("输入用户信息");
this.setBounds(300,240,360,200);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.getContentPane().setLayout(new GridLayout(1,2));
text_user = new JTextArea();
text_user.setEditable(false);
this.getContentPane().add(text_user);
JPanel panel = new JPanel(new GridLayout(6,1));
this.getContentPane().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_rb=new JPanel(new GridLayout(1,2));
panel.add(panel_rb);
ButtonGroup bgroup = new ButtonGroup();
radiob_male = new JRadioButton("男",true);
bgroup.add(radiob_male);
panel_rb.add(radiob_male);
radiob_female = new JRadioButton("女");
bgroup.add(radiob_female);
panel_rb.add(radiob_female);
this.cities = cities;
combox_province = new JComboBox(provinces);
combox_province.setEditable(true);
combox_province.addActionListener(this);
panel.add(combox_province);
combox_city = new JComboBox(cities[0]);
panel.add(combox_city);
button_add = new JButton("添加");
button_add.addActionListener(this);
panel.add(button_add);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{ if (e.getSource()==combox_province)
{ int i=combox_province.getSelectedIndex();
combox_city.removeAllItems();
for (int j=0; j<this.cities[i].length; j++)
combox_city.addItem(this.cities[i][j]);
} if (e.getSource() == button_add)
{ String aline=number+", "+text_name.getText();
if (radiob_male.isSelected())
aline += ", "+radiob_male.getText();
if (radiob_female.isSelected())
aline += ", "+radiob_female.getText();
aline += ", "+combox_province.getSelectedItem();
aline += ", "+combox_city.getSelectedItem();
text_user.append(aline+"\n");
this.number++;
text_number.setText(""+this.number);
text_name.setText("姓名");
}
}
public static void main(String arg[]) 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询