在网格布局中,Jbutton 大小不会随着 java 中 setBounds() 的使用而改变

In Grid Layout the Jbutton size is not changing with the use of setBounds() in java

在使用 GridLayout 制作框架时,使用 setBound() 不会更改按钮大小。框架的代码是:

f1=new JFrame("Select any one");
            ch=new JCheckBox("SUBSTITUTION CYPHER");
            ch1=new JCheckBox("two");
            ch2=new JCheckBox("three");
            ch3=new JCheckBox("four");
            ch4=new JCheckBox("five");

            f1.add(ch);
            f1.add(ch1);
            f1.add(ch2);
            f1.add(ch3);
            f1.add(ch4);
            f1.setLayout(new GridLayout(5, 1));
            b10.addActionListener(this);
            b10.setBounds(280,380,10,10);
            f1.add(b10);
            f1.setSize(300,400);
            f1.setVisible(true);

根据 setBoundsdocs

This method changes layout-related information, and therefore, invalidates the component hierarchy.

因此,setBounds 设置特定布局的大小和位置。在你的 JFrame 你设置 GridLayout,其中 setBounds 将不起作用。

GridLayout 不是按钮的最佳选择,因为 GridLayout 将占用其容器中的所有 space。所以没有 setBounds() 不会是您想要实现的目标的最佳选择。

我建议查看 LayoutManagers 的参考资料:Reference for LayoutManagers

我建议查看一些替代的 LayoutManager。你可以试试 GridBagLayout。

如果你真的想使用GridLayout,你也可以先在每个网格中添加一个JPanel,然后在JPanel中添加较小的JButton