程序如下,可以通过编译,按添加的时候就报错,无法添加
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Javax_Swing extends JFrame implements ActionListener, ItemListener
{
private int number = 1;
private JTextArea textAll;
private JTextField textNum,textName;
private JRadioButton male,female;
private JComboBox province,city;
private JButton button_add;
private JLabel label_name;
public Javax_Swing()
{
super("用户信息");
this.setLocation(300,200);
this.setSize(300,200);
this.setLayout(new GridLayout(1,2));
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
textAll = new JTextArea();
this.add(textAll);
JPanel panel = new JPanel(new GridLayout(6,1));
this.add(panel); textNum = new JTextField(""+number);
textNum.setEditable(false);
panel.add(textNum);

JPanel panel1 = new JPanel(new FlowLayout());
panel.add(panel1);
label_name = new  JLabel("姓名");
panel1.add(label_name);
textName = new JTextField(9);
textName.setVisible(true);
panel1.add(textName); JPanel sex = new JPanel(new GridLayout(1,2));
panel.add(sex);
male = new JRadioButton("男",true);
female = new JRadioButton("女",true);
ButtonGroup bg = new ButtonGroup();
bg.add(male);
bg.add(female);
sex.add(male);
sex.add(female);

Object p[] = {"广东省","北京市"};
JComboBox province = new JComboBox(p);
province.addItemListener(this);
panel.add(province); Object c[] = {"佛山市","东莞市","珠海市","中山市"};
JComboBox city = new JComboBox(c);
panel.add(city); JButton button_add = new JButton("添加");
button_add.addActionListener(this);
panel.add(button_add);
this.setVisible(true);
}
public void itemStateChanged(ItemEvent e)
{
if( province.getSelectedIndex()==0 )
{
city.removeAllItems();
city.addItem("佛山市");
city.addItem("东莞市");
city.addItem("珠海市");
city.addItem("中山市");
}
if( province.getSelectedIndex()==1 )
{
city.removeAllItems();
city.addItem("北京市");
}
} public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button_add)
{
String out="";
out += number + "."+ textName.getText() + ",";
if(male.isSelected())
out += male.getText();
else
out += female.getText();
out += "," + province.getSelectedItem() + "," + city.getSelectedItem() + "\n";
textAll.append(out); this.number++;
textNum.setText(""+this.number);
textName.setText("");
}
} public static void main(String argv[])
{
new Javax_Swing();
}
}

解决方案 »

  1.   


     province = new JComboBox(p);
            province.addItemListener(this);
            panel.add(province);        Object c[] = {"佛山市","东莞市","珠海市","中山市"};
            city = new JComboBox(c);
            panel.add(city);        button_add = new JButton("添加");
            button_add.addActionListener(this);
      

  2.   

    private JComboBox province,city;
        private JButton button_add;已经定义过了,为什么要在程序里重新new呢?
      

  3.   

    你试试System.out.println(e.getSource()==button_add);,这个输出的是false 所以点按钮就不会有反应,
      

  4.   

    你试试这个String name = ((JButton)e.getSource()).getText();获取的是“添加”,你知道该怎么办了!!
      

  5.   

    各位抱歉。。
    错误之一:
    我点添加是没反应,我按5楼的方法试过了,在研究为什么是false;
    错误之二:
    province我一切换,就提示如下错误:
    ---------- Java ----------
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Javax_Swing.itemStateChanged(Javax_Swing.java:67)
    at javax.swing.JComboBox.fireItemStateChanged(JComboBox.java:1205)
    at javax.swing.JComboBox.selectedItemChanged(JComboBox.java:1253)
    at javax.swing.JComboBox.contentsChanged(JComboBox.java:1309)
    at javax.swing.AbstractListModel.fireContentsChanged(AbstractListModel.java:100)
    at javax.swing.DefaultComboBoxModel.setSelectedItem(DefaultComboBoxModel.java:88)
    at javax.swing.JComboBox.setSelectedItem(JComboBox.java:557)
    at javax.swing.JComboBox.setSelectedIndex(JComboBox.java:603)
    at javax.swing.plaf.basic.BasicComboPopup$Handler.mouseReleased(BasicComboPopup.java:816)
    at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:273)
    at java.awt.Component.processMouseEvent(Component.java:6263)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3255)
    at javax.swing.plaf.basic.BasicComboPopup$1.processMouseEvent(BasicComboPopup.java:480)
    at java.awt.Component.processEvent(Component.java:6028)
    at java.awt.Container.processEvent(Container.java:2041)
    at java.awt.Component.dispatchEventImpl(Component.java:4630)
    at java.awt.Container.dispatchEventImpl(Container.java:2099)
    at java.awt.Component.dispatchEvent(Component.java:4460)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
    at java.awt.Container.dispatchEventImpl(Container.java:2085)
    at java.awt.Window.dispatchEventImpl(Window.java:2475)
    at java.awt.Component.dispatchEvent(Component.java:4460)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
      

  6.   


    JAVA里面的类对象不是都要new来分配空间的么