简体   繁体   English

如何使用GridBagLayout设置JButton大小

[英]How to set JButton size with GridBagLayout

I want to set the size of my JButtons that are at the center of my screen to become larger but I can't seem to find how to do that using GridBagLayouts. 我想将位于屏幕中心的JButton的大小设置为更大,但是我似乎找不到使用GridBagLayouts进行操作的方法。

Here is how it looks like : 看起来是这样的: 这就是它的样子

Here is my code : 这是我的代码:

    //      Client
    c.fill = GridBagConstraints.BOTH;
    c.anchor = GridBagConstraints.CENTER;
    c.gridy = 5;
    c.gridx = 5;
    c.gridwidth = 1;
    c.gridheight = 1;
    c.insets = new Insets(10, 1, 1, 10);
    p.add(b[0], c);

    //      Server
    c.fill = GridBagConstraints.BOTH;
    c.anchor = GridBagConstraints.CENTER;
    c.gridy = 10;
    c.gridx = 5;
    c.gridwidth = 1;
    c.gridheight = 1;
    c.insets = new Insets(10, 1, 1, 10);
    p.add(b[1], c);

I want the buttons to take up a larger portion of the empty space around them. 我希望按钮占用其周围空白区域的较大部分。

I can't imagine what do you want, but if you want your button to fill around, you can add 我无法想象您想要什么,但是如果您想让按钮变满,您可以添加

    c.weightx = ...; //Specifies how to distribute extra horizontal space. 
    or c.weighty = ...; //Specifies how to distribute extra vertical space. 

More information was added: Buttons have 50% of the width and [about] 20% of the height of parent [together 50% height including the space in between]. 添加了更多信息:按钮的宽度为父级的50%,高度为父级的20%(高度之间为50%,中间包括空格)。 (Slightly rewritten to match the suggestion.) (根据建议略微改写。)

Solution

Combination of simple Layouts Layouts. 简单布局的组合布局。 Although if you do it like this you will have 3 columns or 3 rows which can't be joined, the rest can easily be changed later: 尽管如果这样做,您将有3列或3行无法连接,其余的可以在以后轻松更改:

// row variation

JPanel parent = new JPanel();
parent.setLayout(new GridLayout(3, 1));

parent.add(new JPanel()); // placeholder for 1st row

JPanel row = new JPanel(); // 2nd row
row.setLayout(new GridLayout(1, 3)); // create 3 cells of equal size

row.add(new JPanel()); // 2nd row, 1st cell placeholder

// now you have a 33% x 33% (oops) rectangle in the middle
JPanel controls = new JPanel();
controls.setLayout(new GridLayout(2, 1, 10, 10));
controls.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10);
controls.add(new JButton("Client"));
controls.add(new JButton("Server"));

row.add(controls); // add 2nd row, 2nd cell

row.add(new JPanel()); // 2nd row, 3rd cell placeholder

parent.add(row); // add 2nd row

parent.add(new JPanel()); // placeholder for 3rd row

Easy, but you won't be able to join the cells later: 容易,但以后将无法加入单元:

JPanel parent = new JPanel(); JPanel父=新的JPanel(); parent.setLayout(newGridLayout(9, 9)); parent.setLayout(newGridLayout(9,9));

Bottom line: combine different layout managers, put your 2 buttons inside a panel and put some placeholders inside, then it should also work fine with GridBagLayout. 底线:合并不同的布局管理器,将2个按钮放在面板中,并在其中放置一些占位符,然后它也可以与GridBagLayout一起正常工作。 That said, I would try to stay flexible by writing reusable components which can easily be combined with any layout manager. 也就是说,我将尝试通过编写可重复使用的组件来保持灵活性,这些组件可以轻松地与任何布局管理器结合使用。 Then you don't have to use placeholders superfluous code in order to display the components correctly. 然后,您不必使用占位符多余的代码即可正确显示组件。

Old Answer 旧答案

Alternative Solution: Use BoxLayout 替代解决方案:使用BoxLayout

BoxLayout is more intuitive and easier to understand when looking at code (of course this is only an opinion). 在查看代码时,BoxLayout更加直观且易于理解(当然,这只是一个意见)。

  1. Decide how your window is structered (is it more like big horizontal components on top of each other PAGE_AXIS or big vertical components next to each other LINE_AXIS ) and use this as the outer BoxLayout: 确定窗口的结构方式(它更像是彼此顶部的大水平组件PAGE_AXIS还是彼此相邻的大垂直组件LINE_AXIS ),并将其用作外部BoxLayout:

     JPanel content = new JPanel(); // or frame content.setLayout(new BoxLayout(content, BoxLayout.LINE_AXIS)); 
  2. Add the components along the axis, where you have more than one component along the other axis use a 2nd BoxLayout. 沿着轴添加组件,沿着另一个轴添加多个组件时,请使用2nd BoxLayout。 You can space components by creating rigid areas (empty rectangles always having the same size) or by adding glue (expanding like gum together with the components). 您可以通过创建刚性区域(始终具有相同大小的空矩形)或添加胶水(将胶与部件一起像胶一样展开)来对部件进行间隔。

     content.add(BoxLayout.createHorizntalGlue()); JPanel col = new JPanel(); col.setLayout(new BoxLayout(col, BoxLayout.PAGE_AXIS)); JButton clientBtn = new JButton("Client"); JButton serverBtn = new JButton("Server"); col.add(BoxLayout.createVerticalGlue()); col.add(clientBtn); col.add(BoxLayout.createRigidArea(new Dimension(1, 10))); col.add(serverBtn); col.add(BoxLayout.createVerticalGlue()); content.add(col); content.add(BoxLayout.createHorizontalGlue()); 
button.setMargin( new Insets(50, 50, 50, 50) );

这将为按钮添加额外的空间,并允许布局管理器根据按钮的首选大小来完成其工作。

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

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