gpt4 book ai didi

java - Java setAlignment 函数可以有哪些参数?

转载 作者:行者123 更新时间:2023-11-29 08:17:27 24 4
gpt4 key购买 nike

setAlignment函数中可以传入哪些参数。还有这个 button1.setAlignment(1f) 是什么意思。

例如

public class TwoButtons extends JFrame {

public TwoButtons() {

setTitle("Two Buttons");

JPanel basic = new JPanel();
basic.setLayout(new BoxLayout(basic, BoxLayout.Y_AXIS));
add(basic);

basic.add(Box.createVerticalGlue());

JPanel bottom = new JPanel();
bottom.setAlignmentX(1f);
bottom.setLayout(new BoxLayout(bottom, BoxLayout.X_AXIS));

JButton ok = new JButton("OK");
JButton close = new JButton("Close");

bottom.add(ok);
bottom.add(Box.createRigidArea(new Dimension(5, 0)));
bottom.add(close);
bottom.add(Box.createRigidArea(new Dimension(15, 0)));

basic.add(bottom);
basic.add(Box.createRigidArea(new Dimension(0, 15)));

setSize(300, 250);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);

}

最佳答案

我认为您是在谈论 setAlignmentX(1f) 方法。此方法在 JComponent 类中定义。它设置垂直对齐方式。

http://download.oracle.com/javase/1.5.0/docs/api/javax/swing/JComponent.html#setAlignmentX(float)

Possible values for setAlignmentX are 

Component.CENTER_ALIGNMENT 0.5f
Component.LEFT_ALIGNMENT 0.0f
Component.RIGHT_ALIGNMENT 1.0f

在你的情况下它是"1f" 所以这个组件有(Component.RIGHT_ALIGNMENT) 右垂直对齐

Box.createRigidArea创建一个始终具有指定大小的不可见组件。

关于java - Java setAlignment 函数可以有哪些参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3504910/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com