1

我正在尝试为可以使用另一个选项卡中的宠物类型的小程序创建一个选项卡。此选项卡左侧有可用的宠物类型,右侧有添加的宠物类型。用户可以通过在左栏中选择宠物然后单击添加按钮将宠物添加到右栏中,或者通过选择宠物并单击删除按钮从右栏中删除宠物。我不知道如何让程序做到这一点,并不断出错。这是我的代码。

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
4

2 回答 2

2

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

同样,Vector 也没有add(...)方法。同样,请非常熟悉Java API,因为它有助于防止您犯此类错误。

于 2013-02-22T17:15:15.737 回答
0

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

于 2013-02-22T17:15:55.630 回答