Java 使用JLists和vectors进行未经检查或不安全操作的两个案例

Java 使用JLists和vectors进行未经检查或不安全操作的两个案例,java,vector,nullpointerexception,awt,actionlistener,Java,Vector,Nullpointerexception,Awt,Actionlistener,我有两个程序,每个程序在小程序中创建一个不同的选项卡,用户可以在一个选项卡中输入宠物类型,然后选择并将宠物类型添加或删除到另一个选项卡的列表中。我原以为我已经解决了所有问题,但现在这两个问题都给了我java使用未经检查或不安全的操作的机会。 注意:使用-Xlint重新编译:未选中以获取详细信息。 我似乎不明白为什么我会收到这些错误。这是两个代码 import java.awt.*; import java.awt.event.*; import javax.swing.*; import jav

我有两个程序,每个程序在小程序中创建一个不同的选项卡,用户可以在一个选项卡中输入宠物类型,然后选择并将宠物类型添加或删除到另一个选项卡的列表中。我原以为我已经解决了所有问题,但现在这两个问题都给了我
java使用未经检查或不安全的操作的机会。
注意:使用-Xlint重新编译:未选中以获取详细信息。
我似乎不明白为什么我会收到这些错误。这是两个代码

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

public class CreatePanel extends JPanel
 {
   private Vector petList; //Instantiation of variables.  To save space, multiple variables
   private JButton button1; //were instantiated at a time when possible, such as the panels.
   private SelectPanel sPanel;
   private JLabel text1, redText;
   private JTextField petInput;
   private JTextArea petsIn;
   private Panel wPanel, alertPanel, petPanel, createPanel;



 public CreatePanel(Vector petList, SelectPanel sPanel)
  {
    this.petList = petList;
    this.sPanel = sPanel;


    // orgranize components here
    // here is an example
    setLayout(new GridLayout(1,2));

    text1 = new JLabel("Enter a Pet Type"); //Creation of the visible portions of the applet
    petInput = new JTextField();
    button1 = new JButton("Create a Pet");
    petsIn = new JTextArea("No Pet");
    wPanel = new Panel(); //Panels have to be used to ensure the porper layout of the
    petPanel = new Panel();  //applet so it conforms to assignment specifications
    createPanel = new Panel();  //making for a pretty panel, but some very ugly code
    alertPanel = new Panel();
    alertPanel.setLayout(new GridLayout(1,2));
    createPanel.setLayout(new GridLayout(2,3));
    petPanel.setLayout(new GridLayout(1,2));  //Each panel has its own layout, making for layouts
    wPanel.setLayout(new GridLayout(3,1));  //in layouts to get everything looking correct.
    button1.addActionListener(new ButtonListener()); //adds the listener to the button

    add(wPanel);
    wPanel.add(alertPanel);
    alertPanel.add(redText);
    wPanel.add(petPanel);
    petPanel.add(text1);
    petPanel.add(petInput);
    wPanel.add(createPanel);
    createPanel.add(new JLabel());
    createPanel.add(button1);
    createPanel.add(new JLabel());  //Have to create several blank labels to get position of
    createPanel.add(new JLabel());  //the create pet button to be positioned correctly.
    createPanel.add(new JLabel());
    createPanel.add(new JLabel());
    add(petsIn);


  }

  private class ButtonListener implements ActionListener
   {
    public void actionPerformed(ActionEvent event)
     {
         String inputPet = petInput.getText(); //Gets the user input pet
         boolean checker = false; // A boolean check type so pets can't be added repeatedly

         for (int p = 0; p < petList.size(); p++){ //Runs a check through the petList vector
             if (inputPet.equalsIgnoreCase((String) petList.get(p))){ //If a pet already exists (ignoring case) then the check is set to true
                 checker = true; //for a later if loop to inform the user.
                 break;
             }
         }


         if(inputPet.equals("")){
             redText.setText("Please enter a pet type.");
             redText.setForeground(Color.red);
         } else if (checker == true){
             redText.setText("The pet type already exists.");
             redText.setForeground(Color.red);
         } else {
             petList.add(inputPet);
             String addedPets = (String) petList.get(0);
             for(int i = 1; i < petList.size(); i++){
                 addedPets += "\n" + (String) petList.get(i);
             }
             redText.setText("Pet type added.");
             redText.setForeground(Color.red);
             petsIn.setText(addedPets);
             sPanel.updateUI();
         }
         redText.setText("");

     } //end of actionPerformed method
  } //end of ButtonListener class

} //end of CreatePanel class
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
导入java.util.*;
公共类CreatePanel扩展了JPanel
{
私有向量petList;//变量的实例化。为了节省空间,可以使用多个变量
私有JButton button1;//在可能的时候实例化,例如面板。
专用面板板;
私有JLabel text1,redText;
私人JTextField小输入;
私人住宅区;
专用面板wPanel、alertPanel、petPanel、createPanel;
public CreatePanel(向量petList,选择Panel sPanel)
{
this.petList=petList;
this.sPanel=sPanel;
//在这里组织组件
//这里有一个例子
setLayout(新网格布局(1,2));
text1=new JLabel(“输入宠物类型”);//创建小程序的可见部分
小输入=新的JTextField();
button1=新的JButton(“创建宠物”);
petsIn=新的JTextArea(“无Pet”);
wPanel=new Panel();//必须使用面板来确保
petPanel=new Panel();//小程序,使其符合分配规范
createPanel=new Panel();//制作一个漂亮的面板,但是一些非常难看的代码
alertPanel=新面板();
alertPanel.setLayout(新网格布局(1,2));
setLayout(新的GridLayout(2,3));
setLayout(新的GridLayout(1,2));//每个面板都有自己的布局,用于布局
setLayout(新的GridLayout(3,1));//在布局中,使所有内容看起来都正确。
button1.addActionListener(new ButtonListener());//将侦听器添加到按钮
添加(wPanel);
wPanel.add(警报面板);
alertPanel.add(红色文本);
wPanel.add(petPanel);
petPanel.add(text1);
petPanel.add(小输入);
wPanel.add(createPanel);
添加(新的JLabel());
createPanel.add(按钮1);
createPanel.add(new JLabel());//必须创建几个空白标签才能获得
createPanel.add(new JLabel());//要正确定位的创建宠物按钮。
添加(新的JLabel());
添加(新的JLabel());
添加(petsIn);
}
私有类ButtonListener实现ActionListener
{
已执行的公共无效操作(操作事件)
{
字符串inputPet=petInput.getText();//获取用户输入pet
boolean checker=false;//布尔检查类型,因此不能重复添加宠物
对于(int p=0;p

import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
导入javax.swing.event.*;
导入java.util.*;
公共类SelectPanel扩展了JPanel
{
私有向量列表,选择列表;
私人小组bPanel,nPanel;
私人JLabel sPets、APET、NPET;
私有整数numPets=0;
私人JButton addPet,remove;
私人JList petsAvail,petTypes;
私人JScrollPane sPane,sPane2;
公共选择面板(向量列表)
{
this.petList=petList;
此.setLayout(新的BorderLayout());
bPanel=新面板();
nPanel=新面板();
nPanel.setLayout(新网格布局(1,2));
b面板设置布局(新网格布局(2,1));
petTypes=新的JList(petList);
petTypes.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
sPane=新的JScrollPane(petTypes);
宠物列表。添加(0,“可用宠物”);
选择列表=新向量();
petsAvail=新建JList(选择列表);
petsAvail.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
sPane2=新的JScrollPane(petsAvail);
选择列表。添加(0,“选定宠物”);
aPets=新的JLabel(“可用宠物”);
nPets=新JLabel(“选定宠物”);
nPets=新的JLabel(“选定宠物的数量:“+numPets”);
addPet=新的JButton(“添加”);
移除=新的按钮(“移除”);
添加(petsAvail,BorderLayout.EAST);
添加(petTypes,BorderLayout.WEST);
添加(nPanel,BorderLayout.SOUTH);
nPanel.add(nPets);
添加(bPanel,BorderLayout.CENTER);
b面板添加(添加PET);
b面板添加(删除);
addPet.addActionListener(新按钮Listener());
remove.addActionListener(新按钮Listener());
//组织面板的部件
}
public void updatePetList()
{
petTypes.updateUI();
petsAvail.updateUI();
//此方法可以刷新的外观
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);
      petList.add(0, "Available pet(s)");

      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();
        petsAvail.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;

                    if(selectList.contains(petTypes.getSelectedValue())){
                        check = true;
                        break;
                    } else if(check == false){
                        selectList.addElement(petTypes.getSelectedValue());
                        petsAvail.updateUI();
                        numPets++;
                    }
                }
            } else if (which == remove){
                selectList.removeElement(petsAvail.getSelectedValue());
                updatePetList();
                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
private Vector<String> petList
petList = new Vector<String>()