按钮侦听器在列表中添加和删除对象

时间:2013-02-22 17:00:39

标签: java swing compiler-errors awt actionlistener

我正在尝试为可以使用其他标签中的宠物类型的小程序创建标签。此选项卡左侧有可用的宠物类型,右侧有添加的宠物类型。用户可以通过在左栏中选择宠物然后单击添加按钮将宠物添加到右栏,或者通过选择它并单击删除按钮从右栏中删除宠物。我无法弄清楚如何让程序执行此操作,并不断收到错误。这是我的代码。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;

public class SelectPanel extends JPanel
 {
   private Vector petList, selectList;
   private Panel bPanel, nPanel;
   private JLabel sPets, aPets, nPets;
   private int numPets = 0;
   private JButton addPet, remove;
   private JList petsAvail, petTypes;
   private JScrollPane sPane, sPane2;

   public SelectPanel(Vector petList)
     {
      this.petList = petList;
  this.setLayout(new BorderLayout());

  bPanel = new Panel();
  nPanel = new Panel();
  nPanel.setLayout(new GridLayout(1,2));
  bPanel.setLayout(new GridLayout(2,1));

  petTypes = new JList(petList);
  petTypes.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  sPane = new JScrollPane(petTypes);

  selectList = new Vector();
  petsAvail = new JList(selectList);
  petsAvail.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  sPane2 = new JScrollPane(petsAvail);
  selectList.add(0, "Selected pet(s)");

  aPets = new JLabel("Available pet(s)");
  nPets = new JLabel("Selected pet(s)");
  nPets = new JLabel("The number of selected pets:" + numPets);
  addPet = new JButton("Add");
  remove = new JButton("Remove");

  add(petsAvail, BorderLayout.EAST);
  add(petTypes, BorderLayout.WEST);
  add(nPanel, BorderLayout.SOUTH);
  nPanel.add(nPets);
  add(bPanel, BorderLayout.CENTER);
  bPanel.add(addPet);
  bPanel.add(remove);

  addPet.addActionListener(new ButtonListener());
  remove.addActionListener(new ButtonListener());


 // orgranize components for the panel


 }

 public void updatePetList()
  {
        petTypes.updateUI();
        //This method can refresh the appearance of the list of pets
        //by calling updateUI() method for the JList.
        //It can be called from the CreatePanel class whenever a new pet type
        //is added to the vector and the JList appearence needs to be refreshed.
  }

 private class ButtonListener implements ActionListener
  {
       public void actionPerformed(ActionEvent event)
        {
            Object which = event.getSource();

        if(which == addPet){
            for (int p = 1; p < selectList.size(); p++){
                boolean check = false;
                String selectedPet =(String) petList.getSelectedValue();
                String petCheck =(String) selectList.get(p);
                if(selectedPet.equals(petCheck)){
                    check = true;
                    break;
                } else if(added == false){
                    petsAvail.add(selectedPet);
                    petsAvail.updateUI();
                    numPets++;
                }
            }
        } else if (which == remove){

            numPets--;
        }



        //When the added button is pushed, the selected pet
        //should be added to the right list and the number of
        //selected pets is incremented by 1.
        //When the remove button is pushed, the selected pet
        //should be removed from the right list and the number of
        //selected pets is decremented by 1.
        //
        //Be careful for the cases when no item has been selected.
    }
  } //end of ButtonListener class

} //end of SelectPanel class

非常感谢任何协助。

错误是:

C:\Users\[Redacted]\Desktop\Java\SelectPanel.java:83: error: cannot find symbol
                String selectedPet =(String) petList.getSelectedValue();
                                                    ^


 symbol:   method getSelectedValue()
  location: variable petList of type Vector
C:\Users\[Redacted]\Desktop\Java\SelectPanel.java:89: error: no suitable method found for add(String)
                        petsAvail.add(selectedPet);
                                 ^

method Container.add(Component,Object,int) is not applicable
      (actual and formal argument lists differ in length)
    method Container.add(Component,Object) is not applicable
      (actual and formal argument lists differ in length)
    method Container.add(Component,int) is not applicable
      (actual and formal argument lists differ in length)
    method Container.add(String,Component) is not applicable
      (actual and formal argument lists differ in length)
    method Container.add(Component) is not applicable
      (actual argument String cannot be converted to Component by method invocation conversion)
    method Component.add(PopupMenu) is not applicable
      (actual argument String cannot be converted to PopupMenu by method invocation conversion)
Note: C:\Users\[Redacted]\Desktop\Java\SelectPanel.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
2 errors

Tool completed with exit code 1

2 个答案:

答案 0 :(得分:2)

您正在尝试调用根本不存在的方法,编译器正在抱怨。您的petlist是Vector变量,如果您查看Vector API,您将看到此类型可用的方法。方法getSelectedValue()不是其中之一,对于Vector来说真的没什么意义。

同样,Vector没有add(...)方法。请再次熟悉Java API,因为这有助于防止您犯这些错误。

答案 1 :(得分:0)

如果我正在编写类似的代码,我可能会使用ArrayLists而不是Vectors。如果你有一个调试器,最好调试代码,这样你就可以看到每行代码在做什么,并找出它失败的地方。有时,错误代码在确定问题时不可靠。