2014-06-27 28 views
0

我有這個問題,JPanel(contentPane)重新調整(testLabel)GUI組件的大小,只要它們太大而不適合面板,就會變得很小。我已經將滾動條添加到JPanel,但組件仍然重新調整大小而不是使用滾動條。這裏是我的課,我使用帶滾動條的JPanel。如何讓JPanel使用滾動條來適應大J組件?

package marsPackage; 

import java.awt.*; 
import javax.swing.*; 

public class DashTab{ 

    private JLabel testLabel; //test label 
    private JLabel testLabel2; 
    private JLabel testLabel3; 
    private JLabel testLabel4; 
    private JPanel dashPanel; //Panel that holds the JTabbedPane tab 
    private JPanel contentPane; // Panel that holds all GUI components 
    private JScrollPane scrollPane; // Scrollpane used on contentPane 

    /* 
    * Constructor 
    * All of your GUI components should be added to 
    * contentPane using the gridBagLayout. 
    */ 

    public DashTab(){ 
    //Creating the dashpanel that holds everything 
    dashPanel = new JPanel(); 
    dashPanel.setBackground(Color.WHITE); 

    //Creating the contentPane that holds all GUI components and 
    //uses vertical/horizontal sidebards as needed 
    contentPane = new JPanel(); 
    contentPane.setBackground(Color.WHITE); 

    //Giving the contentPane the GridBagLayout 
    contentPane.setLayout(new GridBagLayout()); 
    GridBagConstraints g = new GridBagConstraints(); 
    contentPane.setPreferredSize(new Dimension(857, 725)); 

    //Adding scrollPane to Content Pane and adding those two to dashPanel 
    scrollPane = new JScrollPane(contentPane); 
    scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 
    scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
    scrollPane.setBackground(Color.WHITE); 
    dashPanel.add(scrollPane); 

    /* 
    * You may begin adding your GUI components from this point forward. 
    * Remember to only use GridBagLayout with GridBagConstraints using the 
    * g variable. 
    */ 

    testLabel = new JLabel("Testing Here 1"); 
    testLabel.setPreferredSize(new Dimension(500, 500)); 
    testLabel.setBorder(BorderFactory.createMatteBorder(2, 2, 2, 2, Color.gray)); 
    g.gridx = 0; 
    g.gridy = 0; 
    contentPane.add(testLabel, g); 

    testLabel2 = new JLabel("Testing Here 2"); 
    testLabel2.setPreferredSize(new Dimension(250, 200)); 
    testLabel2.setBorder(BorderFactory.createMatteBorder(2, 2, 2, 2, Color.gray)); 
    g.gridx = 1; 
    g.gridy = 0; 
    contentPane.add(testLabel2, g); 

    testLabel3 = new JLabel("Testing Here 3"); 
    testLabel3.setPreferredSize(new Dimension(200, 200)); 
    testLabel3.setBorder(BorderFactory.createMatteBorder(2, 2, 2, 2, Color.gray)); 
    g.gridx = 1; 
    g.gridy = 1; 
    contentPane.add(testLabel3, g); 

    testLabel4 = new JLabel("Testing Here 4"); 
    testLabel4.setPreferredSize(new Dimension(200, 200)); 
    testLabel4.setBorder(BorderFactory.createMatteBorder(2, 2, 2, 2, Color.gray)); 
    g.gridx = 2; 
    g.gridy = 2; 
    contentPane.add(testLabel4, g); 
    } 

    /** 
    * The getDashTab method returns a DashTab object. 
    * @return a DashTab panel object. 
    */ 

    public JPanel getDashTab(){ 
      return dashPanel; 
    } 
} 

這是上面的代碼看起來像: http://i.imgur.com/lBSTfrt.jpg

每當我刪除contentPane.setPreferredSize(new Dimension(857, 725));面板剛剛伸出和完全忽略滾動條使它看起來像:

http://i.imgur.com/sMusbTm.jpg

回答

1

contentPane.setPreferredSize(new Dimension(857,725));

請勿使用setPreferredSize()方法來設置大小。這是佈局管理器的工作,在這種情況下,GridBagLayout可以確定面板的大小。

當面板的首選大小大於滾動窗格的大小時,滾動條會自動出現。

當GUI元件太大而無法放入面板時,它們會變得非常小。

GridBagLayout將嘗試尊重組件的首選大小。如果首選尺寸大於面板尺寸,則將使用「最小尺寸」組件。在JLabel的情況下,最小尺寸是完全顯示文本所需的空間。

再次,不要嘗試使用setPreferedSize()方法。

+0

謝謝!我刪除了:'contentPane.setPreferredSize(new Dimension(857,725));'then added:'scrollPane.setPreferredSize(new Dimension(857,725));'結果就是我想要的:http:// i.imgur.com/KpW2A4G.jpg – Zero

+0

不要在滾動窗格上使用setPreferredSize()。可用空間將根據添加到選項卡窗格的最大選項卡大小的大小自動計算。如果您在所有選項卡上正確使用佈局管理器,則無需手動設置首選大小。 – camickr

相關問題