2013-04-28 122 views
1

我有這個JPanel叫做CatalogPane,它的大小是600,它是JTabbedPane裏面一個名爲BookFrame的JFrame。因此,在CatalogPane內部,我創建了一個名爲bookDisplay的JPanel,它顯示書籍列表及其詳細信息。我希望它的大小爲780×900,爲滾動條留下20px,比框架高,以便它可以滾動。然後我創建了一個尺寸爲800×400的面板,因爲我需要在其他領域的底部留出一些額外的空間。我試圖爲bookDisplay創建一個JScrollPane,然後將它放在另一個面板中,但不知何故滾動條出現,但不能用於滾動。我已經嘗試過更改大小和滾動窗格,但仍然無法使其工作。JTabbedPane中的JPanel內部的JScrollPane不滾動

是什麼樣子:http://prntscr.com/12j0d9

滾動條是有,但不能工作。我試圖讓我的滾動條工作之前,我格式正確的佈局。

CatalogPane:

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

public class CatalogPane extends JPanel{ 
//private Order currOrder = new Order(); 
//ArrayList<Book> bookCatalog = new ArrayList(); 
GridBagConstraints gbc = new GridBagConstraints(); 
GridBagLayout gbl = new GridBagLayout(); 
JPanel bookDisplay = new JPanel(); 

public CatalogPane() 
{ 
    //loadBookCatalog(); 

    this.setPreferredSize(new Dimension(800, 600)); 
    bookDisplay.setPreferredSize(new Dimension(780, 900)); 

    bookDisplay.setLayout(new GridLayout(6, 5)); 

    //bookDisplay.setLayout(gbl); 
    //gbc.fill = GridBagConstraints.NONE; 
    //gbc.weightx = 1; 
    //gbc.weighty = 1; 

    JLabel bookL = new JLabel("Books"); 
    JLabel hardL = new JLabel("Hardcopy"); 
    JLabel hardQuantL = new JLabel("Quantity"); 
    JLabel eL = new JLabel("EBook"); 
    JLabel eQuantL = new JLabel("Quantity"); 

    bookDisplay.add(bookL); 
    bookDisplay.add(hardL); 
    bookDisplay.add(hardQuantL); 
    bookDisplay.add(eL); 
    bookDisplay.add(eQuantL); 

    /* 
    addComponent(bookL, 0, 0, 1, 1); 
    addComponent(hardL, 0, 1, 1, 1); 
    addComponent(hardQuantL, 0, 2, 1, 1); 
    addComponent(eL, 0, 3, 1, 1); 
    addComponent(eQuantL, 0, 4, 1, 1); 
    */ 

    Iterator<Book> bci = bookCatalog.iterator(); 
    int row = 1; 
    /* 
    while(bci.hasNext()) 
    { 
     Book temp = bci.next(); 
     ImageIcon book1 = new ImageIcon(temp.getImage()); 
     JLabel image = new JLabel(temp.getTitle(), book1, JLabel.CENTER); 
     image.setVerticalTextPosition(JLabel.TOP); 
     image.setHorizontalTextPosition(JLabel.CENTER); 
     String[] quant = {"1", "2", "3", "4", "5"}; 
     JLabel hardP = new JLabel("$" + temp.getHardPrice()); 
     JLabel eP = new JLabel("$" + temp.getEPrice()); 
     JComboBox jbc1 = new JComboBox(quant); 
     JComboBox jbc2 = new JComboBox(quant); 
     jbc1.setSelectedIndex(0); 
     jbc2.setSelectedIndex(0); 

     /* 
     addComponent(b1temp, row, 0, 1, 1); 
     addComponent(hardP, row, 1, 1, 1); 
     addComponent(jbc1, row, 2, 1, 1); 
     addComponent(eP, row, 3, 1, 1); 
     addComponent(jbc2, row, 4, 1, 1); 
     row++; 

     bookDisplay.add(image); 
     bookDisplay.add(new JLabel("$" + temp.getHardPrice())); 
     bookDisplay.add(jbc1); 
     bookDisplay.add(new JLabel("$" + temp.getEPrice())); 
     bookDisplay.add(jbc2); 
    */ 


    for(int i=0;i<5;i++) 
    { 
     String[] quant = {"1", "2", "3", "4", "5"}; 
     JComboBox jbc1 = new JComboBox(quant); 
     JComboBox jbc2 = new JComboBox(quant); 
     jbc1.setSelectedIndex(0); 
     jbc2.setSelectedIndex(0); 
     JLabel image = new JLabel("image"); 
     bookDisplay.add(image); 
     bookDisplay.add(new JLabel("$" + 20)); 
     bookDisplay.add(jbc1); 
     bookDisplay.add(new JLabel("$" + 15)); 
     bookDisplay.add(jbc2); 
    } 

    JScrollPane vertical = new JScrollPane(bookDisplay); 
    //JPanel testP = new JPanel(); 
    //testP.setPreferredSize(new Dimension(800, 400)); 
    //JScrollPane vertical = new JScrollPane(testP); 
    //testP.add(bookDisplay); 
    vertical.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 

    JPanel testP = new JPanel(); 
    testP.setPreferredSize(new Dimension(800, 400)); 
    testP.add(vertical); 
    add(testP); 
} 

public void addComponent(Component c, int row, int col, int hei, int wid) 
{ 
    gbc.gridx = col; 
    gbc.gridy = row; 
    gbc.gridwidth = wid; 
    gbc.gridheight = hei; 
    gbl.setConstraints(c, gbc); 
    bookDisplay.add(c); 
} 

public Order getCurrOrder() 
{ 
    return currOrder; 
} 

private void loadBookCatalog() 
{ 
    try 
    { 
     String[] str = new String[8]; 
     Scanner sc = new Scanner(new File("bookcat.txt")); 
     double temp1, temp2; 

     while(sc.hasNextLine()) 
     { 
      str = sc.nextLine().split(";"); 
      temp1 = Double.parseDouble(str[3]); 
      temp2 = Double.parseDouble(str[4]); 
      Book temp = new Book(temp1, temp2, str[0], str[1], str[2], str[5]); 
      bookCatalog.add(temp); 
     } 
    } 
    catch(IOException e) 
    { 
     System.out.println("File not found!"); 
    } 

} 
} 

BookFrame:

public class BookFrame extends JFrame{ 
JButton closeButton; 
CatalogPane cp; 
//IntroPane ip; 

public BookFrame(String name) 
{ 
    super(name); 
    this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); 
    this.addWindowListener(new WindowAdapter(){ 
     public void windowClosing(WindowEvent e) 
     { 
      JOptionPane.showMessageDialog(JOptionPane.getFrameForComponent(new IntroPane()), 
       "Thank you for visiting Groovy Book Company.", "Message", 
       JOptionPane.INFORMATION_MESSAGE, new ImageIcon("coffee.jpg")); 
      System.exit(0); 
     } 
    }); 

    //ip = new IntroPane(); 
    cp = new CatalogPane(); 
    JTabbedPane jtp = new JTabbedPane(); 
    jtp.setPreferredSize(new Dimension(800, 600)); 

    //jtp.addTab("Intro", ip); 
    jtp.addTab("Catalog", cp); 
    add(jtp); 
    pack(); 
    setVisible(true); 

} 
} 
+1

爲了更好地幫助越早,張貼[SSCCE(http://sscce.org/)。 – 2013-04-28 14:12:42

+0

這似乎是一個佈局問題。嘗試使用'gbl'作爲'bookDisplay'的佈局。然後在'addComponent'中,移除對'setConstraints'的調用,並用'bookDisplay.add(c,gbc);'替換下面的行,看看是否有幫助。 – kuporific 2013-04-28 14:42:33

+0

其實,因爲'bookDisplay'使用了一個'GridBagLayout',所以在你調用'bookDisplay.add'的地方,你需要使用'GridBagConstraints'' – kuporific 2013-04-28 14:45:01

回答

3

我想看看JTable,它處理滾動和繪製如圖所示here及以下。這個例子展示瞭如何渲染圖像和貨幣。首先爲Integer類型的數量添加第三列。這個相關的example說明了使用JComboBox編輯器。

test image

import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.text.NumberFormat; 
import javax.swing.Icon; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTabbedPane; 
import javax.swing.JTable; 
import javax.swing.UIManager; 
import javax.swing.table.DefaultTableCellRenderer; 
import javax.swing.table.DefaultTableModel; 

/** 
* @see https://stackoverflow.com/a/16264880/230513 
*/ 
public class Test { 

    public static final Icon ICON = UIManager.getIcon("html.pendingImage"); 

    private JPanel createPanel() { 
     JPanel panel = new JPanel(); 
     DefaultTableModel model = new DefaultTableModel() { 
      @Override 
      public Class<?> getColumnClass(int col) { 
       if (col == 0) { 
        return Icon.class; 
       } else { 
        return Double.class; 
       } 
      } 
     }; 
     model.setColumnIdentifiers(new Object[]{"Book", "Cost"}); 
     for (int i = 0; i < 42; i++) { 
      model.addRow(new Object[]{ICON, Double.valueOf(i)}); 
     } 
     JTable table = new JTable(model); 
     table.setDefaultRenderer(Double.class, new DefaultTableCellRenderer() { 
      @Override 
      protected void setValue(Object value) { 
       NumberFormat format = NumberFormat.getCurrencyInstance(); 
       setText((value == null) ? "" : format.format(value)); 
      } 
     }); 
     table.setRowHeight(ICON.getIconHeight()); 
     panel.add(new JScrollPane(table) { 
      @Override 
      public Dimension getPreferredSize() { 
       return new Dimension(320, 240); 
      } 
     }); 
     return panel; 
    } 

    private void display() { 
     JFrame f = new JFrame("Test"); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     JTabbedPane jtp = new JTabbedPane(); 
     jtp.addTab("Test1", createPanel()); 
     jtp.addTab("Test2", createPanel()); 
     f.add(jtp); 
     f.pack(); 
     f.setLocationRelativeTo(null); 
     f.setVisible(true); 
    } 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       new Test().display(); 
      } 
     }); 
    } 
} 
+0

嘿,免費的書! – trashgod 2013-04-28 16:14:46

+0

該表的工作原理,但我試圖讓第一列加載圖像,但我似乎無法讓它出現。我該如何讓表格加載帶有ImageIcon的JLabel? – 2013-04-29 00:54:15

+0

在'addRow()'中使用'ImageIcon'並相應地更新列類;正如本教程中指出的那樣,它將「以中心標籤呈現」。 – trashgod 2013-04-29 03:45:54