向我的应用程序添加滚动窗格时遇到问题。我的应用程序是这样工作的: 1. 在开始时 panel1(黄色一个)被初始化 2. 在我按下“potvrdit”后 panel2(红色一个被初始化) 3. 一组面板(绿色一个)被添加到 panel2
问题是当设置(绿色面板)的数量对于屏幕来说太大时,我无法添加滚动条。如您所见,它只是在右侧添加了细线。
这是我的源代码的一部分:
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();
}