Java setAlignment函数有哪些参数?

sub*_*nki 1 java

可以在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);

    }
Run Code Online (Sandbox Code Playgroud)

YoK*_*YoK 5

我想你在谈论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
Run Code Online (Sandbox Code Playgroud)

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

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