2014-03-12 59 views
0

我有一個JPanel在JScrollPane中使用FormLayout。JScrollPane不能與FormLayout一起工作

由於某些原因,JScrollPane在內容變得太大時不顯示滾動條。

我是新來的Java,所以我很可能錯過了一些次要的事情:)

這裏是我到目前爲止的代碼。將窗口大小調整爲較小的高度應該會導致JScrollPane顯示滾動條,但不會。請幫忙!

我知道我應該在另一個線程等運行GUI,但這只是一個模型,迄今爲止,它不應該影響結果。

import javax.swing.JFrame; 
import javax.swing.JPanel; 

import java.awt.Color; 
import java.awt.Dimension; 

import javax.swing.BoxLayout; 
import javax.swing.JScrollPane; 
import javax.swing.UIManager; 

import javax.swing.JLabel; 
import javax.swing.border.LineBorder; 
import javax.swing.BorderFactory; 

import com.jgoodies.forms.builder.PanelBuilder; 
import com.jgoodies.forms.layout.CellConstraints; 
import com.jgoodies.forms.layout.FormLayout; 
import com.jgoodies.forms.layout.RowSpec; 

public class Test7 extends JFrame { 

@SuppressWarnings("deprecation") 
public Test7() { 

    UIManager.put("swing.boldMetal", Boolean.FALSE); 

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    this.setTitle("Test7"); 
    this.setSize(1000, 700); 
    this.setLocationRelativeTo(null); 

    JScrollPane scrollPane_4 = new JScrollPane(); 
    scrollPane_4.setBorder(BorderFactory.createEmptyBorder()); 
    this.add(scrollPane_4); 

    JPanel bottom = new JPanel(); 
    bottom.setLayout(new BoxLayout(bottom, BoxLayout.Y_AXIS)); 

    JPanel container = new JPanel(); 
    container.setPreferredSize(new Dimension(10, 10)); 

    scrollPane_4.setViewportView(container); 

    FormLayout layout = new FormLayout("default", "fill:default:grow"); 
    PanelBuilder builder = new PanelBuilder(layout, container); 
    CellConstraints cc = new CellConstraints(); 

    layout.appendRow(new RowSpec("pref")); 
    builder.add(bottom, cc.xy(1, layout.getRowCount()));   

    JPanel row1 = new JPanel(); 
    row1.setBackground(Color.decode("#fcfcfc")); 
    row1.setLayout(new BoxLayout(row1, BoxLayout.X_AXIS)); 
    row1.setAlignmentX(JPanel.LEFT_ALIGNMENT); 
    row1.setMaximumSize(new Dimension(700, Short.MAX_VALUE)); 
    bottom.add(row1); 

    JLabel lblAaaa = new JLabel("<html>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</html>"); 
    lblAaaa.setAlignmentY(JPanel.TOP_ALIGNMENT); 
    lblAaaa.setBackground(Color.decode("#f2f2f2")); 
    lblAaaa.setOpaque(true); 
    lblAaaa.setBorder(new LineBorder(Color.decode("#cccccc"),1)); 
    row1.add(lblAaaa); 

    layout.appendRow(new RowSpec("10px"));   

    JPanel bottom2 = new JPanel(); 
    bottom2.setOpaque(false); 
    bottom2.setLayout(new BoxLayout(bottom2, BoxLayout.Y_AXIS)); 

    layout.appendRow(new RowSpec("pref")); 
    builder.add(bottom2, cc.xy(1, layout.getRowCount()));   

    JPanel row2 = new JPanel(); 
    row2.setBackground(Color.decode("#fcfcfc")); 
    row2.setLayout(new BoxLayout(row2, BoxLayout.X_AXIS)); 
    row2.setAlignmentX(JPanel.RIGHT_ALIGNMENT); 
    row2.setMaximumSize(new Dimension(700, Short.MAX_VALUE)); 
    bottom2.add(row2); 

    JLabel lblAaaa2 = new JLabel("<html>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</html>"); 
    lblAaaa2.setAlignmentY(JPanel.TOP_ALIGNMENT); 
    lblAaaa2.setBackground(Color.decode("#a9dff5")); 
    lblAaaa2.setOpaque(true); 
    lblAaaa2.setBorder(new LineBorder(Color.decode("#8fb4c2"), 1)); 
    row2.add(lblAaaa2); 

    this.setVisible(true); 

} 

public static void main(String[] args) { 
    new Test7(); 
} 
} 

更新1:

繼從「氣墊船」的提示,滾動條似乎如果我註釋掉了setPreferredSize行工作,但帶來了另一個問題。佈局變得超寬(屏幕外)等等。這可能與row1和row2的setMaximumSize有關,但我想要這些組件的最大尺寸:)

回答

2

這與佈局和所有因爲你是限制的大小組件的JPanel這裏:

JPanel container = new JPanel(); 
container.setPreferredSize(new Dimension(10, 10)); // ***** 

scrollPane_4.setViewportView(container); 

通過調用setPreferredSize(...)您的容器的JPanel不會得到比大小大。解決方法:不要這樣做,不要撥打setPreferredSize(...)。要麼根據GUI的狀態,將組件大小設置爲自己的自然首選大小,這取決於其所擁有的組件,要麼覆蓋其方法以返回一個合理的大小,具體取決於GUI的狀態。

+0

但是,如果我註釋掉這條線,整個佈局變得越來越多。還有其他一些問題嗎? –

+0

@DanieleTesta:是的,但是那行肯定是你的主要問題的原因,並且絕對會阻止你的容器JPanel變得更大。你有更多的工作要做。 –