定位于java swing

0 java layout swing

我在定位标签/密码字段时遇到了一些麻烦.
使用这个代码,他们都被定位在彼此相邻的中心,而我实际上希望它们在我的面板中间彼此叠加.

有谁知道我应该怎么做?

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

}
Run Code Online (Sandbox Code Playgroud)

Sco*_*eld 6

为了掌握布局,我建议你阅读我的文章(http://java.sun.com/developer/onlineTraining/GUI/AWTLayoutMgr/).这是旧的,但概述和FlowLayout如何工作是详细的.

你在"彼此之上"是什么意思?

如果你的意思是

   Password
   <field>
Run Code Online (Sandbox Code Playgroud)

编辑:我记得更容易做到这一点(完全在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);
Run Code Online (Sandbox Code Playgroud)

以前的答案供参考......

我不推荐以下内容,但可以帮助其他有意思的读者

你可以做点什么

setLayout(FlowLayout());
JPanel group = new JPanel(new BorderLayout());
group.add(new JLabel("Password"), BorderLayout.NORTH);
group.add(passwordField, BorderLayout.SOUTH);
add(group);
Run Code Online (Sandbox Code Playgroud)

这将在包含密码和字段的整个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));
Run Code Online (Sandbox Code Playgroud)

我讨厌一般使用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);
Run Code Online (Sandbox Code Playgroud)

关于此的几点说明:

  • 我正在使用我的BoxBeans助手类 - 请参阅http://javadude.com/tools/boxbeans.此页面基于VisualAge for Java,但页面底部的jar可以在VAJ之外使用.我刚刚在eclipse中试过它.
  • AFAICS,您无法将Jframe的布局直接设置为BoxLayout,因此我在其间添加了一个额外的面板.BoxLayout中有一个检查,内容窗格的自动间接有问题.
  • 我嵌套了BoxLayouts,因此水平居中(stuffH面板)包含一个垂直居中(stuffV面板).它们以"Glue"组件为中心,这些组件只是允许自身扩展的组件.
  • 我不得不将标签和字段放在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();
        }
    };
    
    Run Code Online (Sandbox Code Playgroud)

希望这证明对您和其他任何人都有帮助! - 斯科特