Second row of gridbaglayout scrolling out of container

限于喜欢 提交于 2019-12-20 05:43:20

问题


I am trying to achieve a layout similar to that of a carousel. It needs to have images added horizontally with a checkbox field in the second row. I have a panel within a jscrollpane and individual images are added to the panel as labels. Please see screen shot.

screenshot

When I scroll the pane , the first row containing the images stays well within the panel..but if you notice the second row of checkboxes , it scrolls out of the panel. Here is the code ...

JLabel lab1=new JLabel();
for (int ii=0; ii<imageFiles.length; ii++) {
       GridBagConstraints constraint = new GridBagConstraints();  
        lab1 = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);

        constraint.gridx = ii;
        constraint.gridy =0;  
        jPanel9.add(lab1,constraint);
    }
    for (int ii=0; ii<imageFiles.length; ii++) {
        GridBagConstraints constraint1 = new GridBagConstraints();         
        constraint1.anchor = GridBagConstraints.SOUTH;           
        chkbox = new Checkbox("asdasdada");
        constraint1.gridx = ii;
        constraint1.gridy =1;

        jPanel9.add(chkbox, constraint1);
      } 

Not sure what is wrong..Any help is much appreciated..Thanks..


回答1:


The problem is that you are mixing AWT components (heavyweight) with Swing components (lightweight). I have 2 recommendations:

  • Don't mix heavyweight and lightweight components
  • Try to use lightweight components as much as possible

So in your code, replace Checkbox by JCheckbox and it should work just fine.



来源:https://stackoverflow.com/questions/15524754/second-row-of-gridbaglayout-scrolling-out-of-container

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!