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

単に存在しないメソッドを呼び出そうとしていますが、コンパイラは当然のことながら文句を言っています。ペットリストはVector変数であり、Vector APIを見ると、このタイプで使用できるメソッドがわかります。この方法getSelectedValue()はそれらの1つではなく、Vectorにはほとんど意味がありません。

同様に、Vectorにはメソッドがありませんadd(...)。繰り返しになりますが、Java APIは、この種のエラーを防ぐのに役立つため、よく理解してください。

于 2013-02-22T17:15:15.737 に答える
0

私が同様のコードを書いていたら、おそらく Vector の代わりに ArrayLists を使用するでしょう。デバッガーを使用している場合は、コードをデバッグして、コードの各行が何を行っているかを確認し、どの時点で失敗したかを確認することをお勧めします。場合によっては、問題を特定する上でエラー コードが信頼できないことがあります。

于 2013-02-22T17:15:55.630 に答える