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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3504910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:09:11  来源:igfitidea点击:

What parameters can Java setAlignment Function have?

java

提问by subanki

What are the parameters that can be passed in setAlignment Function. Also what does this button1.setAlignment(1f)mean .

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

For Example

例如

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);

    }

回答by YoK

I think you are talking about setAlignmentX(1f) method. This method is defined in JComponent class. It sets the the vertical alignment.

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

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

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

In your case it is "1f"so this component has (Component.RIGHT_ALIGNMENT) Right vertical alignment

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

Box.createRigidAreaCreates an invisible component that's always the specified size.

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

回答by Andrzej Doyle

All methods in Java belong to a class, and as it is (without the class name) I have no idea which method you're talking about.

Java 中的所有方法都属于一个类,因此(没有类名)我不知道您在谈论哪种方法。

If you doknow the class name, and it's a core Java class, then check out the official APIfor a list of all of the classes and methods involved in the J2SE API. Just find the class you're interested in on the left hand side, click on it and you'll see a list of all its methods, together with documentation and parameter lists.

如果你知道类名,这是一个核心的Java类,然后检查出官方的API为所有参与的J2SE API的类和方法的列表。只需在左侧找到您感兴趣的类,单击它,您就会看到其所有方法的列表,以及文档和参数列表。

If it's not a core Java class then you'll need to find the API for the library that defines it. Almost all commonly-downloadable libraries will have an "API" or "Javadocs" link on their homepage that you can follow to achieve the same thing as above.

如果它不是核心 Java 类,那么您需要找到定义它的库的 API。几乎所有通常可下载的库在其主页上都有一个“API”或“Javadocs”链接,您可以点击这些链接来实现与上述相同的功能。

And if you don'tknow the class name, that's the first thing to find out. (Without it your question is like asking "Who lives at house number 34?" You'll need the street name and quite possibly town name to get an answer without the specific context.)

如果您知道班级名称,那么首先要找出来。(没有它,您的问题就像问“谁住在 34 号门牌号?”您需要街道名称和很可能的城镇名称才能在没有特定上下文的情况下获得答案。)