简体   繁体   English

在网格布局中,通过使用Java中的setBounds(),Jbutton的大小不会改变

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

While making a frame using GridLayout the button size is not changing using setBound(). 使用GridLayout制作框架时,使用setBound()不会更改按钮的大小。 The code for the frame is : 该框架的代码是:

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);

According to docs of setBounds 根据setBounds 文档

This method changes layout-related information, and therefore, invalidates the component hierarchy. 此方法更改与布局有关的信息,因此使组件层次结构无效。

So, setBounds sets the size and position with layout specific. 因此, setBounds设置特定于布局的大小和位置。 At your JFrame you set GridLayout , where setBounds will not work. JFrame设置GridLayout ,其中setBounds将不起作用。

GridLayout isn't the best choice for buttons as GridLayout will take up all space in its container. GridLayout不是按钮的最佳选择,因为GridLayout会占用其容器中的所有空间。 So no setBounds() won't be the best choice for what you're trying to achieve. 因此,没有setBounds()并不是您要实现的最佳选择。

I would suggest having a look at this reference for LayoutManagers: Reference for LayoutManagers 我建议看看LayoutManagers的此参考:LayoutManagers的参考

I would suggest looking at some alternative LayoutManagers. 我建议看一些替代的LayoutManager。 You could try GridBagLayout. 您可以尝试GridBagLayout。

如果您真的想使用GridLayout还可以先将JPanel添加到每个网格,然后将较小的JButton添加到JPanel

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM