2013-02-09 88 views
3

我在Java中製作這個程序,在這個程序中,我向那些通過套接字連接的人銷售東西。我正在通過JDBC加載產品和價格,但我想在可滾動的表格中以旁邊的價格顯示產品。現在我只有這個JList載入產品名稱:製作可滾動表格

請點擊下面的鏈接瞭解問題。

enter image description here

我應該使用什麼樣的元素,以及如何完成我需要?

感謝您的閱讀。

+3

a [JTable](http://docs.oracle.com/javase/6/docs/api/javax/swing/JTable.html)也許? – 2013-02-09 16:03:14

+1

查看[如何使用表格](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html),(我確定其他人已經鏈接了根目錄,我剛剛標記了它更多清楚;)),只是爲了比較,[如何使用列表](http://docs.oracle.com/javase/tutorial/uiswing/components/list.html) – MadProgrammer 2013-02-09 20:06:59

回答

4

您需要使用JTableJScrollPane

更新參考實例鏈接:

this reference exampleand this可能會幫助你

+0

謝謝...這個例子很不錯! – 2013-02-10 04:59:22

+1

@格蘭諾拉樂意幫你 – exexzian 2013-02-10 04:59:55

+1

不幸的是,鏈接不再可用。 – Iain 2017-04-06 07:47:29

3

我認爲你可以用JTable組件來完成這個任務。

3

使用帶有JScrollPane的JTable用於從上到下或從左到右滾動。

2

您需要使用JTableJScrollPane

示例代碼:

import javax.swing.JFrame; 
import javax.swing.JScrollPane; 
import javax.swing.JTable; 


public class ProductTableExample { 

    public static void main(String[] str) { 
     String[] colName = new String[] { "Product Name" ,"Price" }; 
     Object[][] products = new Object[][] { 
       { "Galleta" ,"$80" }, 
       { "Malta" ,"$40" }, 
       { "Nestea" ,"$120" }, 
       { "Tolta" ,"$140" } 
      }; 

     JTable table = new JTable(products, colName); 

     JFrame frame = new JFrame("Simple Table Example"); 

     // create scroll pane for wrapping the table and add 
     // it to the frame 
     frame.add(new JScrollPane(table)); 
     frame.pack(); 
     frame.setVisible(true);  
    } 

}