Swing的GridBagLayout:将按钮居中放置在第二行。

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

Swing GridBagLayout: centering a button on the second row

问题

我有这段显示标签、文本框和按钮的代码:

  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. public class Form extends JFrame implements ActionListener {
  6. private JLabel label = new JLabel("What's your name:");
  7. private JTextField field = new JTextField(15);
  8. private JButton button = new JButton("Send");
  9. public Form() {
  10. setTitle("Name Form");
  11. setLayout(new GridBagLayout());
  12. GridBagConstraints gbc = new GridBagConstraints();
  13. gbc.insets = new Insets(10, 10, 10, 1);
  14. gbc.anchor = GridBagConstraints.LINE_END;
  15. gbc.gridx = 0;
  16. gbc.gridy = 0;
  17. add(label, gbc);
  18. gbc = new GridBagConstraints();
  19. gbc.insets = new Insets(10, 10, 10, 10);
  20. gbc.fill = GridBagConstraints.HORIZONTAL;
  21. gbc.gridx = 1;
  22. gbc.gridy = 0;
  23. add(field, gbc);
  24. gbc = new GridBagConstraints();
  25. gbc.insets = new Insets(10, 10, 10, 10);
  26. gbc.anchor = GridBagConstraints.LINE_END;
  27. gbc.gridx = 0;
  28. gbc.gridy = 1;
  29. add(button, gbc);
  30. }
  31. @Override
  32. public void actionPerformed(ActionEvent event) {
  33. }
  34. public static void main(String[] args) {
  35. Form myFrame = new Form();
  36. myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  37. myFrame.pack();
  38. myFrame.setVisible(true);
  39. }
  40. }

它显示如下:

Swing的GridBagLayout:将按钮居中放置在第二行。

我希望按钮水平居中,效果如下:

Swing的GridBagLayout:将按钮居中放置在第二行。

如何使用 GridBagLayout 实现这一点?我尝试了不同的 anchor 值,但没有效果。

编辑:

添加 gbc.gridwidth = 2 后效果如下:

Swing的GridBagLayout:将按钮居中放置在第二行。

英文:

I have this code that displays a label, a textfield and a button:

  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. public class Form extends JFrame implements ActionListener {
  6. private JLabel label = new JLabel("What's your name:");
  7. private JTextField field = new JTextField(15);
  8. private JButton button = new JButton("Send");
  9. public Form() {
  10. setTitle("Name Form");
  11. setLayout(new GridBagLayout());
  12. GridBagConstraints gbc = new GridBagConstraints();
  13. gbc.insets = new Insets(10, 10, 10, 1);
  14. gbc.anchor = GridBagConstraints.LINE_END;
  15. gbc.gridx = 0;
  16. gbc.gridy = 0;
  17. add(label, gbc);
  18. gbc = new GridBagConstraints();
  19. gbc.insets = new Insets(10, 10, 10, 10);
  20. gbc.fill = GridBagConstraints.HORIZONTAL;
  21. gbc.gridx = 1;
  22. gbc.gridy = 0;
  23. add(field, gbc);
  24. gbc = new GridBagConstraints();
  25. gbc.insets = new Insets(10, 10, 10, 10);
  26. gbc.anchor = GridBagConstraints.LINE_END;
  27. gbc.gridx = 0;
  28. gbc.gridy = 1;
  29. add(button, gbc);
  30. }
  31. @Override
  32. public void actionPerformed(ActionEvent event) {
  33. }
  34. public static void main(String[] args) {
  35. Form myFrame = new Form();
  36. myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  37. myFrame.pack();
  38. myFrame.setVisible(true);
  39. }
  40. }

It shows this:

Swing的GridBagLayout:将按钮居中放置在第二行。

I need the button to be horizontally centered like this:

Swing的GridBagLayout:将按钮居中放置在第二行。

How do I do this with GridBagLayout? I tried different values for anchor but nothing worked.

EDIT:

Adding gbc.gridwidth = 2 showed this:

Swing的GridBagLayout:将按钮居中放置在第二行。

答案1

得分: 2

按钮需要跨越两列,因此设置 gridwidth

  1. gbc.gridwidth = 2;

(顺便说一下,你不需要为每个组件创建一个新的 GridBagConstraints。只需重用同一个,然后只更改不同的属性。)

英文:

The button needs to stretch over the two columns, so set the gridwidth.

  1. gbc.gridwidth = 2;

(Btw, you don't need to create a new GridBagConstraints for each component. Just reuse the say one and only change the properties that are different.)

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

发表评论

匿名网友

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

确定