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