程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> jtextfield-Java中swing編程文本面板的問題

jtextfield-Java中swing編程文本面板的問題

編輯:編程綜合問答
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)

最佳回答:



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);
}
}


danielinbiti
liudaimu
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved