我有一些問題需要滾動JPanel組件......我花了3個多小時才完成這項工作,但沒有成功,這就是爲什麼我要求你的幫助!使用GridBagLayout製作可滾動的JPanel子節點
Window類:
public class Window extends JFrame {
public Window(String title) {
super();
Dimension dimension = IHM.getDimension();
this.setTitle(title);
this.setSize(dimension.width, dimension.height);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setMaximumSize(dimension);
this.setMinimumSize(dimension);
this.setContentPane(new Container());
this.setBackground(IHM.getBlue());
this.pack();
this.setVisible(true);
}
}
容器類:
public class Container extends JPanel {
public static Boolean debug = true;
private Dimension lastDimension = new Dimension(0, 0);
public Container() {
// Layout
GridBagLayout layout = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
// Panel configurations
this.setLayout(layout);
/**
* HEADER
*/
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.gridheight = 1;
this.add(new HeaderPanel(), gbc);
/**
* LEFT
*/
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.fill = GridBagConstraints.BOTH;
JPanel leftPanel = new LeftPanel();
JScrollPane leftPanelScrollable = new JScrollPane(leftPanel);
leftPanelScrollable.setViewportView(leftPanel);
leftPanelScrollable.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
leftPanelScrollable.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
leftPanelScrollable.setBounds(10, 10, leftPanel.getWidth() - 50, leftPanel.getHeight() - 50);
this.add(leftPanelScrollable, gbc);
// ...
}
}
而且leftPanel類:
public class LeftPanel extends JPanel {
private static Dimension dimension = new Dimension(IHM.getWidth()/2, IHM.getHeight() - 220);
public LeftPanel() {
this.setPreferredSize(dimension);
// Add the labels
this.addLabel();
// Add the radios
this.addRadios();
this.setAutoscrolls(true);
}
}
請看截圖,我沒有任何滾動條:(
的結果在圖片:
你有關於這個問題的任何想法?提前致謝!
請考慮告訴我們更多關於您可能遇到的問題。 –
請注意,我們更願意查看有關代碼和問題的詳細信息,然後查看您在此過程中花費了多少小時。 –
@HovercraftFullOfEels:我添加了一個截圖。關於細節,所有重要的代碼都是關於addLabels和addRadios方法的介紹,我不認爲這很重要,如果我錯了,請糾正。 – keupsonite