1

我正在为我正在制作的东西制作一个小测试 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 所提供的功能。从你们的一些例子来看,这似乎与我想要达到的目标非常相似。

谢谢您的回答。

4

3 回答 3

3

如果你必须做绝对定位(除了动画我不推荐这个),那么布局需要设置为null,而不是FlowLayout.

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

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

于 2012-09-30T03:12:35.903 回答
3

通过阅读代码,您似乎正在寻找一个看起来像这样的表单......

像这样

我通过使用得到了这个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);
于 2012-09-30T06:22:48.527 回答
-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);
            }
        }
    }

}
于 2012-09-30T04:12:08.367 回答