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



我有一个购物车系统的代码,使用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();
 }

}

是。您似乎想要包含String,因此可以String泛型类型声明您的JList。类似的东西

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

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

例如

sourceList = new JList<String>(books)
<gt;是参数。

类,该类使用<gt;是参数类。

诸如Map<字符串,字符串>,JList等

如果您在不使用<gt;则称之为";原始用途";。例如,正确使用JList是-

JList<String> stringList = new JList<>();

但是如果你使用它没有像这样-

JList stringList = new JList();

这被称为参数类JList的原始使用。

最新更新