GridBagLayout和JButtons

huangapple go评论73阅读模式
英文:

GridBagLayout and JButtons

问题

我还没有找到答案,或者也许我不理解和回答。我尝试做的是在一行中显示两个按钮,它们之间以一个或两个空格分隔。当我使用标签和文本字段来做时,效果非常好,但是用按钮却没有这么幸运,

    gridBagConstraints.weighty = 0.1;
    gridBagConstraints.weightx = 1;

    gridBagConstraints.gridy = 0;
    gridBagConstraints.gridx = 0;
    gridBagConstraints.fill = GridBagConstraints.NONE;
    gridBagConstraints.anchor = GridBagConstraints.LINE_END;
    gridBagConstraints.insets = new Insets(0, 0, 0, 5);
    add(streetLabel, gridBagConstraints);

    gridBagConstraints.gridy = 0;
    gridBagConstraints.gridx = 1;
    gridBagConstraints.anchor = GridBagConstraints.LINE_START;
    gridBagConstraints.insets = new Insets(0, 0, 0, 0);
    add(streetField, gridBagConstraints);

效果很好!

    gridBagConstraints.weighty = 2.0;
    gridBagConstraints.weightx = 1;
    
    gridBagConstraints.gridy = 4;
    gridBagConstraints.gridx = 1;
    gridBagConstraints.anchor = GridBagConstraints.FIRST_LINE_START;
    
    gridBagConstraints.insets = new Insets(0 , 0, 0, 5);
    add(addBtn, gridBagConstraints, 0);

    gridBagConstraints.weighty = 2.0;
    gridBagConstraints.weightx = 0.1;
    
    gridBagConstraints.gridy = 4;
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridwidth = 0;
    gridBagConstraints.insets = new Insets(0, 0, 0, 0);
    gridBagConstraints.anchor = GridBagConstraints.FIRST_LINE_END;
    add(cancelBtn, gridBagConstraints);

效果不太好。

GridBagLayout和JButtons

英文:

I haven't found an answer or maybe I don't understand and answer. What I'm trying to do is display 2 buttons ion one row spaced separated by one or two spaces. When I do this with labels and text fields it works perfectly but with buttons no such luck,

gridBagConstraints.weighty = 0.1;
gridBagConstraints.weightx = 1;

gridBagConstraints.gridy = 0;
gridBagConstraints.gridx = 0;
gridBagConstraints.fill = GridBagConstraints.NONE;
gridBagConstraints.anchor = GridBagConstraints.LINE_END;
gridBagConstraints.insets = new Insets(0, 0, 0, 5);
add(streetLabel, gridBagConstraints);

gridBagConstraints.gridy = 0;
gridBagConstraints.gridx = 1;
gridBagConstraints.anchor = GridBagConstraints.LINE_START;
gridBagConstraints.insets = new Insets(0, 0, 0, 0);
add(streetField, gridBagConstraints);

Works great!

gridBagConstraints.weighty = 2.0;
gridBagConstraints.weightx = 1;

gridBagConstraints.gridy = 4;
gridBagConstraints.gridx = 1;
gridBagConstraints.anchor = GridBagConstraints.FIRST_LINE_START;

gridBagConstraints.insets = new Insets(0 , 0, 0, 5);
add(addBtn, gridBagConstraints, 0);

gridBagConstraints.weighty = 2.0;
gridBagConstraints.weightx = 0.1;

gridBagConstraints.gridy = 4;
gridBagConstraints.gridx = 1;
gridBagConstraints.gridwidth = 0;
gridBagConstraints.insets = new Insets(0, 0, 0, 0);
gridBagConstraints.anchor = GridBagConstraints.FIRST_LINE_END;
add(cancelBtn, gridBagConstraints);

Not so Great.

GridBagLayout和JButtons

答案1

得分: 2

我建议您移除所有对 weightxweighty 的使用。它们并不起到您认为的作用。

这些权重仅在网格中存在额外空间时才适用。它们不适用于您的表单。

另外,gridBagConstraints.gridwidth = 0; 没有意义;如果某物在网格中,它无法跨越零个单元格。gridwidth 和 gridheight 必须是正数(或者是像 REMAINDERRELATIVE 这样的特殊值)。

实现您目标的一个好方法是在 GridBagLayout 中嵌套一个具有不同布局的 JPanel,该 JPanel 仅包含您的按钮:

JPanel buttonPanel = new JPanel();
buttonPanel.add(addBtn);
buttonPanel.add(cancelBtn);

gridBagConstraints.gridy = 4;
gridBagConstraints.gridx = 0;
gridBagConstraints.gridwidth = GridBagConstraints.REMAINDER;
gridBagConstraints.anchor = GridBagConstraints.FIRST_LINE_START;

gridBagConstraints.insets = new Insets(0, 0, 0, 5);
add(buttonPanel, gridBagConstraints);

请注意使用 GridBagConstraints.REMAINDER 来使面板在水平方向上跨足所有单元格。

英文:

I recommend that you remove all uses of weightx and weighty. They are not doing what you think.

The weights only apply when there is extra space in your grid. They are not applicable to your form.

Also, gridBagConstraints.gridwidth = 0; does not make sense; if something is in the grid, it can’t span zero cells. gridwidth and gridheight must be positive (or a special value like REMAINDER or RELATIVE).

A good way to accomplish your goal is by nesting a JPanel with a different layout, which contains only your buttons, inside your GridBagLayout:

JPanel buttonPanel = new JPanel();
buttonPanel.add(addBtn);
buttonPanel.add(cancelBtn);

gridBagConstraints.gridy = 4;
gridBagConstraints.gridx = 0;
gridBagConstraints.gridwidth = GridBagConstraints.REMAINDER;
gridBagConstraints.anchor = GridBagConstraints.FIRST_LINE_START;

gridBagConstraints.insets = new Insets(0, 0, 0, 5);
add(buttonPanel, gridBagConstraints);

Notice the use of GridBagConstraints.REMAINDER to make the panel span all cells horizontally.

答案2

得分: 1

尽管可能可以使用单个GridBagLayout来创建整个视图,但我会结合布局使工作变得简单。

  • 红色区域将是一个带有BorderLayout的面板。绿色和蓝色区域将分别位于CENTERPAGE_END中。
  • 带有绿色边框的面板将使用GridBagLayout
  • 带有蓝色边框的面板将使用FlowLayout
英文:

Although it's probably possible to do this entire view with a single GridBagLayout, I'd combine layouts to make the job easy.

GridBagLayout和JButtons

  • The RED area would be a panel with a BorderLayout. GREEN & BLUE would be panels in the CENTER & PAGE_END respectively.
  • The GREEN bordered panel would have a GridBagLayout.
  • The BLUE bordered panel would have a FlowLayout.

答案3

得分: -1

GridBagConstraints.weighty 的最大值为 1。如果值大于1,程序将会将其重置为0。如果你希望该值大于0且小于1,可以使用小数(例如 0.5)。

英文:

The highest value for GridBagConstraints.weighty is 1. If the value is higher than one, the program will reset it to 0. If you want the value to be greater than 0 and less than 1, then you can use a decimal (ie. 0.5)

huangapple
  • 本文由 发表于 2020年10月15日 06:50:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/64362536.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定