我有一個面板佈局問題,我似乎無法解決。我有以下代碼:如何在JScrollPane中獲得水平滾動條
public class Test extends ApplicationFrame {
public Test(String title) {
super(title);
JPanel jpanel = new JPanel();
jpanel.setPreferredSize(new Dimension(100 * 2, 300));
jpanel.setBackground(new Color(0xFF0000));
JScrollPane scrollPane = new JScrollPane(jpanel);
scrollPane.setBackground(new Color(0xFF0000));
scrollPane.getViewport().setPreferredSize(new Dimension(100 * 2, 300));
this.add(scrollPane, BorderLayout.WEST);
this.setPreferredSize(new Dimension(500, 500));
}
public static void main(String args[]) {
Test test = new Test("Layout Test");
test.pack();
RefineryUtilities.centerFrameOnScreen(test);
test.setVisible(true);
}
}
創建以下佈局:
當我拖動窗口的右側並將其移動到紅色JPanel
和JScrollPane
,我'd想水平滾動條出現在JScrollPane
上。但是目前窗口縮小而沒有顯示水平滾動條。這可能嗎?
我想保留BorderLayout.WEST
,因爲我的使用案例是爲了防止JFreeChart在我沒有足夠大的圖表填充整個窗口時伸展。
的'WEST'組件沒有真正開始 「壓縮」 水平。在這種情況下,我不'BorderLayout'是你的朋友 – MadProgrammer
@MadProgrammer你有建議哪個佈局管理器會是更好的選擇嗎? –
我的直覺是'GridBagLayout',但我還不確定它會服務於你的目的 – MadProgrammer