liudaimu
2015-03-25 12:46
采纳率: 50%
浏览 2.6k
已采纳

Java中swing编程文本面板的问题

想做一个记录的程序,将文本框中输入的内容显示在列表中,现在能够添加,但是在删除时,数据模型中的数据删除成功,但是在列表视图中仍然存在,仅仅是有显示,但点击后没反应。

 public class Address {
protected String name;
protected String email;
public Address(String _name,String _email){
    name=_name;
    email=_email;
}
public String toString(){
    return name;
}
}
 package higher;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class EmailListing extends JFrame implements ActionListener,
        ListSelectionListener {

    private JButton add=new JButton("Add");
    private JButton del=new JButton("Delete");

    private DefaultListModel data =new DefaultListModel();
    private JList list=new JList(data);//数据模型放在列表中
    private JTextField name= new JTextField();
    private JTextField email= new JTextField();

    public EmailListing(){
        super("Email List");
        JPanel buttons=new JPanel(new FlowLayout());
        buttons.add(add); add.addActionListener(this);
        buttons.add(del); del.addActionListener(this);

        JPanel input =new JPanel(new GridLayout(2,2));
        input.add(new JLabel("Name:"));  input.add(name);
        input.add(new JLabel("Email:"));  input.add(email);
        input.setBorder(new TitledBorder("address"));

        JScrollPane scrollList =new JScrollPane(list);//将列表放在带有滚动条的面板中
        scrollList.setBorder(new TitledBorder("List"));
        setLayout(new BorderLayout());

        getContentPane().add("North", input);
        getContentPane().add("South", buttons);
        getContentPane().add("Center", scrollList);
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        list.addListSelectionListener(this);
        email.addActionListener(this);
        validate();pack();setVisible(true);

    }

    public void delete(){
        if(list.getSelectedIndex()>=0){
            data.remove(list.getSelectedIndex());

            name.setText(" ");
            email.setText(" ");

        }
    }
    public void add(){
        if(!name.equals("")){
            data.addElement(new Address(name.getText(),email.getText()));
            name.setText(" ");
            email.setText(" ");
            name.requestFocus();
        }
    }


    @Override
    public void valueChanged(ListSelectionEvent arg0) {
        // TODO Auto-generated method stub
        if(!arg0.getValueIsAdjusting()){
            Address address = (Address)data.get(list.getSelectedIndex());
            name.setText(address.name);
            email.setText(address.email);
        }
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub
        if(arg0.getSource()==del){
            delete();
        }
        if(arg0.getSource()==add||arg0.getSource()==email){
            add();
        }
            }


    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
EmailListing el =new EmailListing();
    }

}

图片说明
以下是抛出的异常

 Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
    at java.util.Vector.elementData(Unknown Source)
    at java.util.Vector.elementAt(Unknown Source)
    at javax.swing.DefaultListModel.get(Unknown Source)
    at higher.EmailListing.valueChanged(EmailListing.java:81)
    at javax.swing.JList.fireSelectionValueChanged(Unknown Source)
    at javax.swing.JList$ListSelectionHandler.valueChanged(Unknown Source)
    at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
    at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
    at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
    at javax.swing.DefaultListSelectionModel.removeIndexInterval(Unknown Source)
    at javax.swing.plaf.basic.BasicListUI$Handler.intervalRemoved(Unknown Source)
    at javax.swing.AbstractListModel.fireIntervalRemoved(Unknown Source)
    at javax.swing.DefaultListModel.remove(Unknown Source)
    at higher.EmailListing.delete(EmailListing.java:60)
    at higher.EmailListing.actionPerformed(EmailListing.java:91)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • danielinbiti 2015-03-25 14:54
    已采纳
    
    

    public void valueChanged(ListSelectionEvent arg0) {
    // TODO Auto-generated method stub
    if(list.getSelectedIndex()>=0){//删除会调用changed,但被删了,index不存在了,要判断
    Address address = (Address)data.get(list.getSelectedIndex());
    name.setText(address.name);
    email.setText(address.email);
    }
    }

    
    
    打赏 评论
  • liudaimu 2015-03-26 05:48

    按照你的修改,程序实现了,但是我还是不明白为什么按照原来的程序,删除掉的数据还是会显示在List中呢

    打赏 评论

相关推荐 更多相似问题