如何在 JPanel 中将一个组件精确居中,同时保持其他组件按默认方式对齐?

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

How can I center exactly one component in a JPanel, while the others remain aligned as default?

问题

我使用一个主要的JPanel(Java Swing),我添加了一个JLabel,一个JTextField和一个JButton。我将主要的JPanel的布局设置为BoxLayout.Y_AXIS,使所有组件以从上到下的方式排列。我希望我的按钮是唯一居中的,但是"submitButton.setAlignmentX(CENTER_ALIGNMENT)"将除了按钮以外的所有东西都居中对齐。我尝试将按钮封装在一个JPanel中,并更改其布局,但没有成功。

英文:

So I'm using a main JPanel (Java Swing) and I add a JLabel, a JTextField and a JButton. I'm setting the layout of the main JPanel to BoxLayout.Y_AXIS, aligning all the components in a top to bottom manner. I want my button to be the only one centered, but "submitButton.setAlignmentX(CENTER_ALIGNMENT)" centers everything BUT the button. I tried to encapsulate the button in a JPanel, whose Layout I would change, but I did not succeed.

public NewProductPanel() {
            setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));

            CustomJLabel productNameLabel = new CustomJLabel("PRODUCT NAME:");
            productNameField = new CustomJTextField();

            submitButton = new SubmitButton("Submit");
            submitButton.setAlignmentX(CENTER_ALIGNMENT);

            add(productNameLabel);
            add(productNameField);
            add(submitButton);
        }

答案1

得分: 1

BoxLayout 在具有不同 x 对齐值的组件时不能正常工作。

一种解决方案是使用“包装器”面板,以使所有组件都具有相同的 x 对齐:

setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

JPanel wrapper = new JPanel();
wrapper.setLayout(new BoxLayout(wrapper, BoxLayout.Y_AXIS));
wrapper.setAlignmentX(CENTER_ALIGNMENT);

JLabel productNameLabel = new JLabel("PRODUCT NAME:");
productNameLabel.setAlignmentX(LEFT_ALIGNMENT);
JTextField productNameField = new JTextField(10);
productNameField.setAlignmentX(LEFT_ALIGNMENT);

wrapper.add(productNameLabel);
wrapper.add(productNameField);

JButton submitButton = new JButton("Submit");
submitButton.setAlignmentX(CENTER_ALIGNMENT);

add(wrapper);
add(submitButton);

现在主面板上的两个组件具有相同的“中心”对齐方式,包装器面板上的两个组件具有相同的“左”对齐方式。

另一种选择可能是使用 GridBagLayout

setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.LINE_START;

JLabel productNameLabel = new JLabel("PRODUCT NAME:");
add(productNameLabel, gbc);

JTextField productNameField = new JTextField(10);
gbc.gridy = 1;
add(productNameField, gbc);

JButton submitButton = new JButton("Submit");
gbc.gridy = 2;
gbc.anchor = GridBagConstraints.CENTER;
add(submitButton, gbc);

阅读 How to Use GridBagLayout 上的 Swing 教程,了解有关约束的更多信息。

或者您可以使用 Relative Layout,它允许您控制每个组件的对齐:

RelativeLayout rl = new RelativeLayout(RelativeLayout.Y_AXIS);
rl.setAlignment(RelativeLayout.COMPONENT);
setLayout(rl);

JLabel productNameLabel = new JLabel("PRODUCT NAME:");
add(productNameLabel);

JTextField productNameField = new JTextField(10);
productNameField.setAlignmentX(LEFT_ALIGNMENT);
add(productNameField);

JButton submitButton = new JButton("Submit");
submitButton.setAlignmentX(CENTER_ALIGNMENT);
add(submitButton);
英文:

A BoxLayout doesn't really work (the way you would expect) when you have components with different x alignment values.

One solution is to use a "wrapper" panel so that all components can have the same x alignment:

setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));

JPanel wrapper = new JPanel();
wrapper.setLayout( new BoxLayout(wrapper, BoxLayout.Y_AXIS) );
wrapper.setAlignmentX(CENTER_ALIGNMENT);

JLabel productNameLabel = new JLabel("PRODUCT NAME:");
productNameLabel.setAlignmentX(LEFT_ALIGNMENT);
JTextField productNameField = new JTextField(10);
productNameField.setAlignmentX(LEFT_ALIGNMENT);

wrapper.add(productNameLabel);
wrapper.add(productNameField);

JButton submitButton = new JButton("Submit");
submitButton.setAlignmentX(CENTER_ALIGNMENT);

add(wrapper);
add(submitButton);

Now the two components on the main panel have the same "center" alignment and the two components of the wrapper panel have the same "left" alignment.

Another option might be to use a GridBagLayout:

setLayout( new GridBagLayout() );
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.LINE_START;

JLabel productNameLabel = new JLabel("PRODUCT NAME:");
add(productNameLabel, gbc);

JTextField productNameField = new JTextField(10);
gbc.gridy = 1;
add(productNameField, gbc);

JButton submitButton = new JButton("Submit");
gbc.gridy = 2;
gbc.anchor = GridBagConstraints.CENTER;
add(submitButton, gbc);

Read the Swing tutorial on How to Use GridBagLayout for more information about the contraints.

Or you could use the Relative Layout, which does allow you control alignment of each individual component:

RelativeLayout rl = new RelativeLayout(RelativeLayout.Y_AXIS);
rl.setAlignment( RelativeLayout.COMPONENT );
setLayout( rl );

JLabel productNameLabel = new JLabel("PRODUCT NAME:");
add(productNameLabel);

JTextField productNameField = new JTextField(10);
productNameField.setAlignmentX( LEFT_ALIGNMENT );
add(productNameField);

JButton submitButton = new JButton("Submit");
submitButton.setAlignmentX( CENTER_ALIGNMENT );
add(submitButton);

huangapple
  • 本文由 发表于 2023年5月14日 20:39:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76247530.html
匿名

发表评论

匿名网友

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

确定