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

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

Swing GridBagLayout: centering a button on the second row

问题

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

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Form extends JFrame implements ActionListener {
    private JLabel label = new JLabel("What's your name:");
    private JTextField field = new JTextField(15);
    private JButton button = new JButton("Send");

    public Form() {
        setTitle("Name Form");
        setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10, 10, 10, 1);
        gbc.anchor = GridBagConstraints.LINE_END;
        gbc.gridx = 0;
        gbc.gridy = 0;
        add(label, gbc);

        gbc = new GridBagConstraints();
        gbc.insets = new Insets(10, 10, 10, 10);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 1;
        gbc.gridy = 0;
        add(field, gbc);

        gbc = new GridBagConstraints();
        gbc.insets = new Insets(10, 10, 10, 10);
        gbc.anchor = GridBagConstraints.LINE_END;
        gbc.gridx = 0;
        gbc.gridy = 1;
        add(button, gbc);
    }

    @Override
    public void actionPerformed(ActionEvent event) {

    }

    public static void main(String[] args) {
        Form myFrame = new Form();

        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myFrame.pack();
        myFrame.setVisible(true);
    }
}

它显示如下:

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

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

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

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

编辑:

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

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

英文:

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

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Form extends JFrame implements ActionListener  {
private JLabel label = new JLabel("What's your name:");
private JTextField field = new JTextField(15);
private JButton button = new JButton("Send");
public Form()  {
setTitle("Name Form");
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(10, 10, 10, 1);
gbc.anchor = GridBagConstraints.LINE_END;
gbc.gridx = 0;
gbc.gridy = 0;
add(label, gbc);
gbc = new GridBagConstraints();
gbc.insets = new Insets(10, 10, 10, 10);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 1;
gbc.gridy = 0;
add(field, gbc);
gbc = new GridBagConstraints();
gbc.insets = new Insets(10, 10, 10, 10);
gbc.anchor = GridBagConstraints.LINE_END;
gbc.gridx = 0;
gbc.gridy = 1;
add(button, gbc);
}
@Override
public void actionPerformed(ActionEvent event) {
}
public static void main(String[] args) {
Form myFrame = new Form();
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.pack();
myFrame.setVisible(true);
}
}

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

gbc.gridwidth = 2;

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

英文:

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

    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:

确定