2011-11-07 21 views
-1

これまでのフォローコードでは、車のメイクと価格を出力していますが、車のディスパッションを追加してディプレイしてください。ファイルから読み込むJList scollペインプログラム

discription[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 
+0

あなたのコードはNumberFormatExceptionがを与える.. [K] =は、Double.valueOf(st.nextToken());.私はあなたのコードを最初に修正すると思います。また、私はプロダクトリストだけを表示します。最初に正しいものを取得する必要があります。 – harshit

+0

+1、なぜあなたは 'getContentPane()が必要になるのかについて、問題について – mKorbel

+0

についてdown-vote、SSCCEおよび完全な説明の理由を見ることができません。 (UnitPrice、BorderLayout.NORTH); // 0'を追加しますが、これはVectorとJTableの仕事であり、JListではないと思います。 – mKorbel

答えて

0

Angel918は5ヶ月の周りされていなかったので、私はMeta Suggestionあたりに答えるために彼のコメントを推進しています。

私はそれを得ました。これは私が台無しにしていたワットです。感謝します。価格で

unitPrice.setText(" " + prices[productList.getSelectedIndex()] +" "+productnames[productList.getSelectedIndex()]);

関連する問題