java - java swing中的定位

标签 java swing layout

我在定位标签/密码字段时遇到一些麻烦。
通过这段代码,它们都被定位在彼此相邻的中心,而我实际上希望它们位于面板的中间,彼此重叠。

有人知道我该怎么做吗?

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;

public class Paneel_Pincode extends JPanel {


    Paneel_Pincode() {
        setLayout(new FlowLayout());

        JPasswordField pincode = new JPasswordField(15);
        pincode.setLocation(500, 500);
        JLabel pinInvoer = new JLabel();

        ImageIcon pin1 = new ImageIcon("images/voerPincodeIn.jpg");

        pinInvoer.setIcon(pin1);
        pinInvoer.setLocation(500,700);

        add(pincode);
        add(pinInvoer);
    }

    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(1000,1000);
        f.setLocationRelativeTo(null);

        f.add(new Paneel_Pincode());
        f.setVisible(true);
    }

}

最佳答案

要掌握布局的窍门,我建议阅读我关于布局的文章 ( http://java.sun.com/developer/onlineTraining/GUI/AWTLayoutMgr/ )。它很旧,但 FlowLayout 的概念和工作原理很详细。

“彼此之上”是什么意思?

如果你的意思是像

   Password
   <field>

编辑:我记得有一种更简单的方法(完全在 JDK/JRE 中)... (这与我对下面的 BoxBeans 所做的类似,但您不需要 BoxBeans。很久以前我创建了 BoxBeans 以便能够在 UI 构建器中使用 BoxLayout...)

JLabel label = new JLabel("Password") {
    @Override public Dimension getMaximumSize() {
        return super.getPreferredSize();
    }
};
JPasswordField field = new JPasswordField() {
    @Override public Dimension getMaximumSize() {
        return super.getPreferredSize();
    }
};
field.setColumns(10);
Box verticalBox = Box.createVerticalBox();
verticalBox.add(Box.createVerticalGlue());
verticalBox.add(label);
verticalBox.add(field);
verticalBox.add(Box.createVerticalGlue());
//
Box horizontalBox = Box.createHorizontalBox();
horizontalBox.add(Box.createHorizontalGlue());
horizontalBox.add(verticalBox);
horizontalBox.add(Box.createHorizontalGlue());
add(horizontalBox);

之前的答案供引用...

我不推荐以下内容,但它可能会对其他读者有帮助

你可以做类似的事情

setLayout(FlowLayout());
JPanel group = new JPanel(new BorderLayout());
group.add(new JLabel("Password"), BorderLayout.NORTH);
group.add(passwordField, BorderLayout.SOUTH);
add(group);

这将在整个 UI 的顶部中心创建一个小面板,其中包含密码和字段。

请注意,嵌套的 BorderLayout 可确保标签和字段各自获得其首选大小。您需要在字段上调用 ​​setColumns 来设置您想要显示的字符数。

如果您也想将标签/字段垂直居中,您可以执行以下操作

setLayout(new GridBagLayout());
//
add(new JLabel("Password"), 
    new GridBagConstraints(0,0,1,1,1,1,
        GridBagConstraints.SOUTH,GridBagConstraints.NONE, 
        new Insets(3,3,3,3), 0,0));
field.setColumns(10);
add(field, new GridBagConstraints(0,1,1,1,1,1,
    GridBagConstraints.NORTH,GridBagConstraints.NONE, 
    new Insets(3,3,3,3), 0,0));

我通常讨厌使用 GridBagLayout,因此我将添加一个使用 BoxLayout 的版本(但由于首选大小设置,这有点棘手)

    JFrame f = new JFrame();
    f.setLayout(new BorderLayout());
    //
    JPanel stuffH = new JPanel();
    f.add(stuffH, BorderLayout.CENTER);
    stuffH.setLayout(new BoxLayout(stuffH, BoxLayout.X_AXIS));
    //
    JPanel stuffV = new JPanel();
    stuffV.setLayout(new BoxLayout(stuffV, BoxLayout.Y_AXIS));
    //
    JLabel label = new JLabel("Password");
    BoxAdapter labelAdapter = new BoxAdapter();
    labelAdapter.add(label);
    JPasswordField field = new JPasswordField();
    field.setColumns(10);
    BoxAdapter fieldAdapter = new BoxAdapter();
    fieldAdapter.add(field);
    //
    stuffV.add(new VerticalGlue()); // for vertical spacing
    stuffV.add(labelAdapter);
    stuffV.add(fieldAdapter);
    stuffV.add(new VerticalGlue()); // for vertical spacing
    //
    stuffH.add(new HorizontalGlue()); // for horizontal spacing
    stuffH.add(stuffV);
    stuffH.add(new HorizontalGlue()); // for horizontal spacing
    //
    f.setVisible(true);
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

对此的一些说明:

  • 我正在使用我的 BoxBeans 辅助类 - 请参阅 http://javadude.com/tools/boxbeans 。该页面基于 VisualAge for Java,但页面底部的 jar 可以在 VAJ 之外使用。例如,我刚刚在 eclipse 中尝试过。
  • AFAICS,您无法将 jframe 的布局直接设置为 BoxLayout,因此我在中间添加了一个额外的面板。 BoxLayout 中的一项检查在内容 Pane 的自动间接寻址方面存在问题。
  • 我嵌套了 BoxLayouts,因此水平居中(stuffH 面板)包含垂直居中(stuffV 面板)。它们通过“粘合”组件围绕它们而居中,这些组件只是允许自身扩展的组件。
  • 我必须将标签和字段放入 BoxAdapter 中,这将它们的最大尺寸限制为首选尺寸。如果您不想使用 BoxAdapter,可以通过对字段和标签使用以下内容来达到相同的效果:

    JLabel label = new JLabel("Password") {
        @Override public Dimension getMaximumSize() {
            return super.getPreferredSize();
        }
    };
    JPasswordField field = new JPasswordField() {
        @Override public Dimension getMaximumSize() {
            return super.getPreferredSize();
        }
    };
    

希望这对您和其他人有帮助! ——斯科特

关于java - java swing中的定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/895139/

相关文章:

java - 如何在项目中独立共享资源文件夹?

java - 无法启动组件 [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/JDBC_DBO]]

flutter - 如何在 Flutter 中创建一个可以从屏幕底部滑动的页面?

php - 如何正确清理用户输出

java - 有没有办法使用 Eclipse 找出表单的类?

html - 我希望我的 div 的边界完全重叠,但是当我将它们与 flex 对齐时会变得奇怪

java - 从 Tomcat 资源访问 oracle,为什么会出现 NamingException?

java - 源兼容性 目标兼容性 使用原因

java - 如何在 Swing 中对列表进行平滑滚动

Java Swing JTree : how to change the focus on the last created node