Java 从文件读取的JList scoll窗格程序

Java 从文件读取的JList scoll窗格程序,java,swing,file-io,jscrollpane,Java,Swing,File Io,Jscrollpane,到目前为止,我有下面的代码,它可以输出汽车的品牌和价格,但我似乎无法确定是要添加汽车的discritpion,以便它也可以显示。帮助 我尝试添加description[k]=st.nextToken(),但程序未运行 import java.util.*; import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public c

到目前为止,我有下面的代码,它可以输出汽车的品牌和价格,但我似乎无法确定是要添加汽车的discritpion,以便它也可以显示。帮助

我尝试添加
description[k]=st.nextToken(),但程序未运行

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

public class Lab22V3 extends JFrame implements ListSelectionListener {

    String[] products = new String[100];
    double[] prices = new double[100];
//string[]discription=new String[100]
    JLabel unitPrice;
    JList productList;

    public static void main(String[] args) {
        Lab22V3 x = new Lab22V3();
        x.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        x.setTitle("Lab#22 Version#3");
        x.setSize(350, 250);
        x.setVisible(true);
    } // main

    public Lab22V3() // constructor
    {
        String s;
        StringTokenizer st;
        int k = 0;
        try {
            BufferedReader inFile = new BufferedReader(new FileReader("Lab22data.txt"));
            while ((s = inFile.readLine()) != null) {
                st = new StringTokenizer(s);
                products[k] = st.nextToken();
                //  discription[k] = st.nextToken();
                prices[k] = Double.valueOf(st.nextToken());
                ++k;
            } // while not EOF
            inFile.close();
        } catch (Exception e) {
            System.err.println(e);
        }

        unitPrice = new JLabel();
        getContentPane().add(unitPrice, BorderLayout.NORTH)

        JPanel p = new JPanel();
        productList = new JList(products);  // create a JList object
        productList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        JScrollPane sp = new JScrollPane(productList);  // adding scrolling capability
        p.add(sp);
        getContentPane().add(p, BorderLayout.SOUTH);
        // Event registration
        productList.addListSelectionListener(this);
    } // constructor

    // Event handling
    public void valueChanged(ListSelectionEvent event) {
        unitPrice.setText(" " + prices[productList.getSelectedIndex()]);
        //proddiscrition.setText(""+ discription[productList.getselectedIndex()];
    } // valueChanged
} // Lab22V3>
这是文本文件

1000    100.00  
A dozen of jokes
2000    5.91    
Used BMW
3000    19.28   
Cookie Jar
4000    21.90   
Birthday Cake
5000    35.28   
Used keyboard
6000    50.00   
Love
7000    92.27   
Gone with Wind

Angel918已经有5个月没有出现了,所以我正在推广他的评论,以回答问题

我明白了。这是我一直搞砸的地方。谢谢


unitPrice.setText(“+prices[productList.getSelectedIndex()]”+“+productnames[productList.getSelectedIndex()]”)

您的代码提供NumberFormatException。。价格[k]=两倍的价值(st.nextToken());。我想首先修复你的代码。此外,我将只显示产品列表。。您需要先将其更正..+1,因为我看不到否决投票的原因、SSCE和关于问题的完整描述,请在开始时使用
getContentPane().add(unitPrice,BorderLayout.NORTH)//0
,但我认为这是Vector&JTable的工作,而不是JListI的工作。我更正了getContentPane。如果我一开始就正确了,我就不会寻求帮助:(。当我在eclipse上运行它时,它会给我Gui框架,包括选择列表和价格,但没有说明。我就是找不到缺少的内容!“但是程序没有运行。”一个程序必须先运行。但是在2个更严肃的注释中1)它没有做你期望它做的事情吗?你的问题是什么2)的“正确”部分并不意味着“工作”。请返回并再次阅读它-这次请仔细阅读。