Java Swing GUI:专门使用布局来移动组件

时间:2012-09-30 03:09:27

标签: java swing user-interface flowlayout

我正在为我正在制作的东西做一点测试GUI。 但是,面板的定位会出现问题。

public winInit() {
    super("Chatterbox - Login");

    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch(ClassNotFoundException e) {
    } catch (InstantiationException e) {
    } catch (IllegalAccessException e) {
    } catch (UnsupportedLookAndFeelException e) {
    }

    setSize(300,135);

    pn1 = new JPanel();
    pn2 = new JPanel();
    pn3 = new JPanel();

    l1 = new JLabel("Username");
    l2 = new JLabel("Password");
    l3 = new JLabel("Random text here");
    l4 = new JLabel("Server Address");
    l5 = new JLabel("No address set.");

    i1 = new JTextField(10);

    p1 = new JPasswordField(10);

    b1 = new JButton("Connect");
    b2 = new JButton("Register");
    b3 = new JButton("Set IP");

    l4.setBounds(10, 12, getDim(l4).width, getDim(l4).height);
    l1.setBounds(10, 35, getDim(l1).width, getDim(l1).height);
    l2.setBounds(10, 60, getDim(l2).width, getDim(l2).height);
    l3.setBounds(10, 85, getDim(l3).width, getDim(l3).height);
    l5.setBounds(l4.getBounds().width + 14, 12, l5.getPreferredSize().width, l5.getPreferredSize().height);

    l5.setForeground(Color.gray);

    i1.setBounds(getDim(l1).width + 15, 35, getDim(i1).width, getDim(i1).height);
    p1.setBounds(getDim(l1).width + 15, 60, getDim(p1).width, getDim(p1).height);

    b1.setBounds(getDim(l1).width + getDim(i1).width + 23, 34, getDim(b2).width, getDim(b1).height - 5);
    b2.setBounds(getDim(l1).width + getDim(i1).width + 23, 60, getDim(b2).width, getDim(b2).height - 5);
    b3.setBounds(getDim(l1).width + getDim(i1).width + 23, 10, etDim(b2).width, getDim(b3).height - 5);

    b1.addActionListener(clickButton);
    b2.addActionListener(clickButton);
    b3.addActionListener(clickButton);

    pn1.setLayout(new FlowLayout(FlowLayout.RIGHT));
    pn2.setLayout(new FlowLayout(FlowLayout.RIGHT));

    pn1.add(l1);
    pn1.add(i1);
    pn1.add(b1);

    pn2.add(l2);
    pn2.add(p1);
    pn2.add(b2);

    add(pn1);
    add(pn2);

}

我正在尝试使用FlowLayout以所需的方式定位面板。我在添加时使用BorderLayout,但当我只使用彼此最接近的方向时,垂直间距太远了。

此代码的输出是创建一个窗口300,150,将两个面板放在完全相同的空格中。

是的,我意识到那里有无用的代码和setBounds(),但那只是我搞砸了绝对定位,这对我来说也没有用。

编辑:

问题解决了......我想。 我现在很高兴使用MiGLayout,但我也看到GridBagLayout也提供了什么。从你的一些人看到'例子,它似乎与我试图达到的非常相似。

感谢您的回答。

3 个答案:

答案 0 :(得分:3)

如果你必须进行绝对定位(除动画外我不建议这样做),那么布局需要设置为null,而不是FlowLayout

对于数据采集面板,我建议使用智能的布局管理器组合,包括考虑一些非标准布局,例如MiGLayout。

编辑澄清:MiGLayout可能比GridBagLayout更容易使用,但是如果你熟悉GridBagLayout,它并不难以使用,并且它不需要下载,因为它是核心Java库的一部分。我自己,我可能会使用嵌套的JPanels,它们使用布局的组合,可能是BorderLayout用于整个GUI,GridBagLayout用于数据采集面板,一个是JLabel / JTextField组合的行。

答案 1 :(得分:3)

通过阅读代码,看起来你似乎是在一个看起来像这样的表格之后...

Like this

我使用GridBagLayout

将其结合在了一起
l1 = new javax.swing.JLabel();
l2 = new javax.swing.JLabel();
l3 = new javax.swing.JLabel();
l4 = new javax.swing.JLabel();
l5 = new javax.swing.JLabel();
i1 = new javax.swing.JTextField();
p1 = new javax.swing.JPasswordField();
b1 = new javax.swing.JButton();
b2 = new javax.swing.JButton();
b3 = new javax.swing.JButton();

setLayout(new java.awt.GridBagLayout());

l4.setText("Server Address");
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.LINE_START;
gbc.insets = new java.awt.Insets(2, 2, 2, 2);
add(l4, gbc);

l1.setText("Username");
gbc.gridy = 1;
add(l1, gbc);

l2.setText("Password");
gbc.gridy = 2;
add(l2, gbc);

l3.setText("Random text here");
gbc.gridy = 3;
add(l3, gbc);

l5.setText("No address set");
gbc.gridx = 1;
gbc.gridy = 0;
add(l5, gbc);

i1.setColumns(12);
gbc.gridy = 1;
add(i1, gbc);

p1.setColumns(12);
gbc.gridy = 2;
add(p1, gbc);

b3.setText("Set IP");
gbc.gridx = 2;
gbc.gridy = 0;
add(b3, gbc);

b1.setText("Connect");
gbc.gridx = 2;
gbc.gridy = 1;
add(b1, gbc);

b2.setText("Register");
gbc.gridy = 2;
add(b2, gbc);

答案 2 :(得分:-1)

我不知道你到底想要什么,但这应该符合你的目的。

import java.awt.BorderLayout;
public class loginBox extends JDialog {

    private final JPanel contentPanel = new JPanel();
    private JTextField txtUserName;
    private JPasswordField passwordField;
    private JTextField textField;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        try {
            loginBox dialog = new loginBox();
            dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            dialog.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Create the dialog.
     */
    public loginBox() {
        setBounds(100, 100, 450, 300);
        getContentPane().setLayout(new BorderLayout());
        contentPanel.setBorder(new TitledBorder(null, "Log In", TitledBorder.LEADING, TitledBorder.TOP, null, null));
        getContentPane().add(contentPanel, BorderLayout.CENTER);
        contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));
        {
            JPanel panel = new JPanel();
            contentPanel.add(panel);
            panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
            {
                JLabel lblChatterBoxlogin = new JLabel("Chatter Box-Login");
                panel.add(lblChatterBoxlogin);
            }
        }
        {
            JPanel formPanel = new JPanel();
            contentPanel.add(formPanel);
            GridBagLayout gbl_formPanel = new GridBagLayout();
            gbl_formPanel.columnWidths = new int[]{52, 0, 0, 0};
            gbl_formPanel.rowHeights = new int[]{14, 0, 0, 0, 0, 0};
            gbl_formPanel.columnWeights = new double[]{0.0, 1.0, 1.0, Double.MIN_VALUE};
            gbl_formPanel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
            formPanel.setLayout(gbl_formPanel);
            {
                JLabel lblUserName = new JLabel("User Name");
                GridBagConstraints gbc_lblUserName = new GridBagConstraints();
                gbc_lblUserName.anchor = GridBagConstraints.WEST;
                gbc_lblUserName.insets = new Insets(0, 0, 5, 5);
                gbc_lblUserName.gridx = 0;
                gbc_lblUserName.gridy = 0;
                formPanel.add(lblUserName, gbc_lblUserName);
            }
            {
                txtUserName = new JTextField();
                GridBagConstraints gbc_txtUserName = new GridBagConstraints();
                gbc_txtUserName.insets = new Insets(0, 0, 5, 5);
                gbc_txtUserName.fill = GridBagConstraints.HORIZONTAL;
                gbc_txtUserName.gridx = 1;
                gbc_txtUserName.gridy = 0;
                formPanel.add(txtUserName, gbc_txtUserName);
                txtUserName.setColumns(10);
            }
            {
                JLabel lblPassword = new JLabel("Password");
                GridBagConstraints gbc_lblPassword = new GridBagConstraints();
                gbc_lblPassword.anchor = GridBagConstraints.WEST;
                gbc_lblPassword.insets = new Insets(0, 0, 5, 5);
                gbc_lblPassword.gridx = 0;
                gbc_lblPassword.gridy = 1;
                formPanel.add(lblPassword, gbc_lblPassword);
            }
            {
                passwordField = new JPasswordField();
                GridBagConstraints gbc_passwordField = new GridBagConstraints();
                gbc_passwordField.insets = new Insets(0, 0, 5, 5);
                gbc_passwordField.fill = GridBagConstraints.HORIZONTAL;
                gbc_passwordField.gridx = 1;
                gbc_passwordField.gridy = 1;
                formPanel.add(passwordField, gbc_passwordField);
            }
            {
                JLabel lblServer = new JLabel("Server Address");
                GridBagConstraints gbc_lblServer = new GridBagConstraints();
                gbc_lblServer.anchor = GridBagConstraints.ABOVE_BASELINE_TRAILING;
                gbc_lblServer.insets = new Insets(0, 0, 5, 5);
                gbc_lblServer.gridx = 0;
                gbc_lblServer.gridy = 2;
                formPanel.add(lblServer, gbc_lblServer);
            }
            {
                textField = new JTextField();
                GridBagConstraints gbc_textField = new GridBagConstraints();
                gbc_textField.insets = new Insets(0, 0, 5, 5);
                gbc_textField.fill = GridBagConstraints.HORIZONTAL;
                gbc_textField.gridx = 1;
                gbc_textField.gridy = 2;
                formPanel.add(textField, gbc_textField);
                textField.setColumns(10);
            }
            {
                JLabel lblNoAddressSet = new JLabel("No Address Set");
                GridBagConstraints gbc_lblNoAddressSet = new GridBagConstraints();
                gbc_lblNoAddressSet.insets = new Insets(0, 0, 5, 0);
                gbc_lblNoAddressSet.gridx = 2;
                gbc_lblNoAddressSet.gridy = 2;
                formPanel.add(lblNoAddressSet, gbc_lblNoAddressSet);
            }
            {
                JLabel lblRandomTextHere = new JLabel("Random Text Here");
                GridBagConstraints gbc_lblRandomTextHere = new GridBagConstraints();
                gbc_lblRandomTextHere.insets = new Insets(0, 0, 5, 5);
                gbc_lblRandomTextHere.gridx = 1;
                gbc_lblRandomTextHere.gridy = 3;
                formPanel.add(lblRandomTextHere, gbc_lblRandomTextHere);
            }
        }
        {
            JPanel buttonPane = new JPanel();
            buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
            getContentPane().add(buttonPane, BorderLayout.SOUTH);
            {
                JButton okButton = new JButton("Connect");
                okButton.setActionCommand("OK");
                buttonPane.add(okButton);
                getRootPane().setDefaultButton(okButton);
            }
            {
                JButton btnNewButton = new JButton("Register");
                buttonPane.add(btnNewButton);
            }
            {
                JButton btnNewButton_1 = new JButton("Set IP");
                buttonPane.add(btnNewButton_1);
            }
            {
                JButton cancelButton = new JButton("Cancel");
                cancelButton.setActionCommand("Cancel");
                buttonPane.add(cancelButton);
            }
        }
    }

}