Can I add a component to a specific grid cell when a GridLayout is used?

前端 未结 2 1144
一生所求
一生所求 2020-12-01 10:47

When I set the GridLayout to the JPanel and then add something, it is added subsequently in the \"text order\" (from left to right, from top to bottom). But I want to add an

相关标签:
2条回答
  • 2020-12-01 11:09

    Yes

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(2,2,1,1));
        JButton component= new JButton("Component");
        panel.add(component, 0,0 );
    

    Create your panel and set its layout.
    new GridLayout(numberOfRows,numberOfColums,HorizontalGap,VerticalGap)

    (new GridLayout(2,2,1,1))  => here i want 2 rows, 2 columns,
    - if any horizontal gaps (HGap), they should be 1px (1unit)
    - I also want the same for vertical gaps so i do same as vertical gaps(VGap). i.e 1 unit
    - In this case; gaps  => spacing/margins/padding --in that sense.

    Create your components and add them to the panel
    - (component, 0,0 )  => 0,0 is the row and column.. (like a 2d array). @row 0 & @column 0 or at intersection of row 0 and column 0
    specify where your component goes by putting the row and column where it should go.
    each cell has a location == [row][column]

    Or you can do it without hgaps and vgaps:

        JPanel panel = new JPanel();        
        panel.setLayout(new GridLayout(2,2));
        JButton component= new JButton("Component");
        panel.add(component, 0,0 );
    
    0 讨论(0)
  • 2020-12-01 11:22

    No, you can't add components at a specific cell. What you can do is add empty JPanel objects and hold on to references to them in an array, then add components to them in any order you want.

    Something like:

    int i = 3;
    int j = 4;
    JPanel[][] panelHolder = new JPanel[i][j];    
    setLayout(new GridLayout(i,j));
    
    for(int m = 0; m < i; m++) {
       for(int n = 0; n < j; n++) {
          panelHolder[m][n] = new JPanel();
          add(panelHolder[m][n]);
       }
    }
    

    Then later, you can add directly to one of the JPanel objects:

    panelHolder[2][3].add(new JButton("Foo"));
    
    0 讨论(0)
提交回复
热议问题