有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java GridLayout超出了它的大小

我创建了JPanel并将布局设置为GridLayout,我将其设置为3行5列。 但当我向这个面板添加11个JButton时,它显示了3行,每行4列。 我希望JButtons以3行5列的形式显示

public class Main {

public static void main(String[] args) {
        JFrame frame = new JFrame();
        JPanel panel = new JPanel(new GridLayout(3, 5));
        for (int i = 0; i < 11; i++) {
            JButton jButton = new JButton("Click");
            panel.add(jButton);
        }
        frame.add(panel);
        frame.setLocationByPlatform(false);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
}
}

共 (0) 个答案