定位于java swing

时间:2009-05-21 21:01:13

标签: 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);
    }

}

2 个答案:

答案 0 :(得分:6)

要了解布局,我建议您阅读我的文章(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中有一个检查,它在内容窗格的自动间接方面有问题。
  • 我嵌套了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();
        }
    };
    

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

答案 1 :(得分:1)

我会推荐JGoodies FormLayout。一旦你学会了它,它就非常强大而且易于用手工编码。