向我的应用添加滚动窗格时遇到问题。我的应用程序是这样的: 1.在开始面板1(黄色一个)初始化 2.按“potvrdit”panel2后(红色的一个初始化) 3.将一组面板(绿色面板)添加到面板2
问题是当设置(绿色面板)的数量对于屏幕而言太大时,我无法添加滚动条。如你所见,它只是在右侧添加了细线。
这是我的源代码的一部分:
JFrame jframe = new JFrame("Etiket print.");
JPanel panel0 = new JPanel();
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JScrollPane scrollFrame = new JScrollPane(panel2);
jframe.setMinimumSize(new Dimension(1280, 1000));
GridBagConstraints c = new GridBagConstraints();
panel0.setLayout(new GridBagLayout());
panel1.setLayout(new GridBagLayout());
panel2.setLayout(new GridBagLayout());
panel2.setAutoscrolls(true);
panel3.setLayout(new GridBagLayout());
panel4.setLayout(new GridBagLayout());
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel container = new JPanel();
JPanel container1 = new JPanel();
JPanel container2 = new JPanel();
container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
container1.setLayout(new BoxLayout(container1, BoxLayout.Y_AXIS));
container2.setLayout(new BoxLayout(container2, BoxLayout.X_AXIS));
container1.add(panel0);
container1.add(panel1);
container2.add(container1);
container2.add(panel3);
container2.add(panel4);
container.add(container2);
container.add(panel2);
jframe.add(scrollFrame);
jframe.add(container, BorderLayout.NORTH);
jframe.pack();
jframe.setVisible(true);
if (e.getActionCommand().equals("Potvrd")||e.getActionCommand().equals("myTextField")){
temporaryBlockedSubharnesses.clear();
panel2.removeAll();
harnessPanelList.clear();
GridBagConstraints c = new GridBagConstraints();
for (int i=0; i<20; i++){
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 0;
c.gridy = i;
JPanel hp= new JPanel();
hp.setMinimumSize(new Dimension(80, 70));
hp.setMaximumSize(new Dimension(80, 70));
hp.setPreferredSize(new Dimension(80, 70));
if(i%2==0) {
hp.setBackground(myBlue1Color);
}
else {
hp.setBackground(myBlue2Color);
}
panel2.add(hp, c);
hp.repaint();
hp.validate();
}
panel2.repaint();
panel2.validate();
jframe.repaint();
}
答案 0 :(得分:1)
而不是来自swingx库的JPanel
尝试JXPanel
。它实现了Scrollable
,因此它支持滚动。