JButton in JPanel with gridlayout

*爱你&永不变心* 提交于 2021-02-08 11:34:36

问题


Is it possible to bring it into this shape using the gridlayout?

I use empty label to add this empty grid

This is my code:

import javax.swing.*;
import java.awt.*;
public class test {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setTitle("Buttons");
        frame.setSize(800, 800);
        int size=12;
        int flag1=0,count=0,count2=0;
        frame.setLayout(new GridLayout(1,2,0,0));
        JPanel panel = new JPanel();
        GridLayout layout = new GridLayout(size,(size*2),0,0);
           panel.setLayout(layout);
       

        for(int i=1; size>=i; i++){
            
            for(int k=0; i-1>k; k++){ 
                panel.add(new JLabel());      
                count2++;               
            }
            
            for(int k=0; size>k; k++){
                JButton button = new JButton(""+i);
                button.setBackground(Color.WHITE);
                panel.add(button);
                count2++;
            }
            
            for(int k=0; (size*2)-(count2)>k; k++){   
                     
                panel.add(new JLabel());  
                flag1=1; 
                                      
               }
            if(flag1==1){
                    count2=0;
                    flag1=0;
            }

        }

        frame.add(panel);
        frame.setVisible(true);
    }   

}

actually i don't know much about this , i'm trying to learn

来源:https://stackoverflow.com/questions/65965772/jbutton-in-jpanel-with-gridlayout

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