修复以下警告的任何方法:javax.swing.JList 是原始类型。对泛型类型 <E> 的引用应该被参数化

Any way to fix thefollowing warning: javax.swing.JList is a raw type. References to generic type <E> should be parameterized

我有这个购物车系统的代码,使用 javax.swing.JList 总是会导致问题。有什么办法可以通过改变某些东西来修复它吗?我应该用不同的方式写吗?

这是代码。 (我正在使用 Dr.Java,我还应该使用任何其他编译器吗?)

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

public class ShoppingCartSystem extends JFrame 
{

 private JPanel sourceListPanel;
 private JPanel shoppingCartPanel;
 private JPanel buttonsPanel;

 private JList sourceList;
 private JList shoppingCart;
 private JScrollPane scrollPane;
 private JScrollPane scrollPane2;

 private JButton addButton;
 private JButton removeButton;
 private JButton checkListButton;

 private String []books;
 private double []price;
 private String []cart;
 private int cartSize;
 private double subTotal = 0.0,tax,total;

ShoppingCartSystem()
{
     setTitle("Shopping Cart System");
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     setLayout(new BorderLayout());

     books = new String[25];
     price = new double[25];
     cart = new String[25];
     cartSize =0;

     readDataFromFile();
     buildSourceListPanel();
     buildShoppingCartPanel();
     buildButtonsPanel();

    add(sourceListPanel,BorderLayout.WEST);
     add(shoppingCartPanel,BorderLayout.EAST);
     add(buttonsPanel,BorderLayout.SOUTH);
     pack();
     setVisible(true);
 }

private void readDataFromFile()
{
     try{
         DataInputStream dis = new DataInputStream(new FileInputStream("BookPrices.txt"));
         BufferedReader br = new BufferedReader(new InputStreamReader(dis));
         int i=0;
         String line;
         while((line = br.readLine()) != null){
             String []arr = line.split(", ");
             books[i] = arr[0];
             price[i++] = Double.parseDouble(arr[1]);
         }
         br.close();
         dis.close();
     }
     catch(Exception exp)
{
         System.out.println(exp.toString());
     }

 }

private void buildSourceListPanel()
{
     sourceListPanel = new JPanel();
     sourceList = new JList(books);
     sourceList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
     sourceList.setVisibleRowCount(6);       
     scrollPane = new JScrollPane(sourceList);
     sourceListPanel.add(scrollPane);
 }

private void buildShoppingCartPanel()
{
     shoppingCartPanel = new JPanel();
     shoppingCart = new JList();       
     shoppingCart.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
     shoppingCart.setVisibleRowCount(6);       
     scrollPane2 = new JScrollPane(shoppingCart);
     shoppingCartPanel.add(scrollPane2);
 }

private void buildButtonsPanel()
{
     buttonsPanel =new JPanel();
     addButton = new JButton("Add to shopping cart");
     removeButton = new JButton("Remove from shopping cart");
     checkListButton =new JButton("Check List");
     buttonsPanel.setLayout(new FlowLayout(10));
     buttonsPanel.add(addButton);
     buttonsPanel.add(removeButton);
     buttonsPanel.add(checkListButton);
     addButton.addActionListener(new ButtonListener());
     removeButton.addActionListener(new ButtonListener());
     checkListButton.addActionListener(new ButtonListener());
 }

private class ButtonListener implements ActionListener
{

    public void actionPerformed(ActionEvent ae)
{

        if(ae.getSource() == addButton)
         {               
             cart[cartSize++] = (String) sourceList.getSelectedValue();
             shoppingCart.setListData(cart);
             subTotal += price[sourceList.getSelectedIndex()];
         }
         else if(ae.getSource() == removeButton)
         {
             for(int i=0;i<books.length;i++)
                 if(shoppingCart.getSelectedValue().equals(books[i]))
                 {
                    subTotal -= price[i];
                    break;
                 }

             int selection = shoppingCart.getSelectedIndex();

            for(int i=selection; i< cart.length-1;i++)
                 cart[i] = cart[i+1];
             shoppingCart.setListData(cart);               
         }
         else
         {
             java.text.DecimalFormat df = new java.text.DecimalFormat("#.##");
             tax = subTotal * 0.06;
             total = subTotal + tax;
             StringBuffer sb = new StringBuffer();
             sb.append("Sub Total: "+df.format(subTotal)+"\n");
             sb.append("Tax: "+df.format(tax)+"\n");
             sb.append("Total: "+df.format(total)+"\n");
             JOptionPane.showMessageDialog(null, sb);
         }
     }
 }

 public static void main(String []args)
{
     new ShoppingCartSystem();
 }

}

JList 更改为 JList<String> 或您在列表中输入的任何类型。

例如

sourceList = new JList<String>(books)

是的。您似乎想要包含 String(s),因此您可以 声明 您的 JListString Generic Type。像

private JList<String> sourceList;
private JList<String> shoppingCart;

然后初始化他们喜欢

sourceList = new JList<>();
// ...
shoppingCart = new JList<>();

,你可以同时声明初始化

private JList<String> sourceList = new JList<>();
private JList<String> shoppingCart = new JList<>();