0

我已尝试搜索此答案并尝试了一些我在此处的帖子中找到的东西,但似乎没有任何效果。

我所拥有的是:

当前对话框的屏幕截图

有了这个:

private class InputGUI extends JFrame implements ActionListener
{

    private JTextField urlIn = new JTextField(colWidth);
    private JLabel urlLabel = new JLabel("DB URL: ");

    private JTextField protocolIn = new JTextField(colWidth);
    private JLabel protocolLabel = new JLabel("Protocol: ");

    private JTextField portIn = new JTextField(colWidth);
    private JLabel portLabel = new JLabel("Port: ");

    private JTextField userIn = new JTextField(colWidth);
    private JLabel userLabel = new JLabel("Username: ");

    private JPasswordField passIn = new JPasswordField(colWidth);
    private JLabel passLabel = new JLabel("Password: ");

    private JTextField tableIn = new JTextField(colWidth);
    private JLabel tableLabel = new JLabel("Table: ");


    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public void inputFields()
    {
        addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent e)
            {
                inputExit.doClick();
            }
        });

        // Sets the size, what to do when closed and the title
        this.setSize(350,400);
        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        this.setTitle("Brewberry Pi!");

        // Set the location off the frame
        this.setLocationRelativeTo(null);

        button1 = new JButton("Submit");
        button1.addActionListener(this);

        button2 = new JButton("Cancel");
        button2.addActionListener(this);

        inputExit = new JButton("Exit");
        inputExit.addActionListener(this);

        // Panel for content to be added to
        JPanel labelPanel = new JPanel();
        JPanel fieldPanel = new JPanel();
        JPanel buttonPanel = new JPanel();

        Box box1 = Box.createVerticalBox();
        Box box2 = Box.createVerticalBox();

        box1.add(urlLabel);
        box1.add(protocolLabel);
        box1.add(portLabel);
        box1.add(userLabel);
        box1.add(passLabel);
        box1.add(tableLabel);

        box2.add(urlIn);
        box2.add(protocolIn);
        box2.add(portIn);
        box2.add(userIn);
        box2.add(passIn);
        box2.add(tableIn);

        labelPanel.add(box1);
        fieldPanel.add(box2);
        buttonPanel.add(button1);
        buttonPanel.add(button2);   

        // Add Panel to frame
        this.add(labelPanel, BorderLayout.WEST);
        this.add(fieldPanel, BorderLayout.CENTER);
        this.add(buttonPanel, BorderLayout.SOUTH);

        this.pack();

        // Don't allow the user to change the size of our window
        this.setResizable(false);

        // Set to visible
        this.setVisible(true);
    }

我需要标签与字段对齐,但无论我尝试什么,标签总是在彼此下方结束。

4

3 回答 3

2

试试这个:

box1.add(urlLabel);
box1.add(Box.createRigidArea(new Dimension(5,5))); // change these dimension to achieve the correct spacing
box1.add(protocolLabel);
box1.add(Box.createRigidArea(new Dimension(5,5))); 
box1.add(portLabel);
box1.add(Box.createRigidArea(new Dimension(5,5))); 
box1.add(userLabel);
box1.add(Box.createRigidArea(new Dimension(5,5))); 
box1.add(passLabel);
box1.add(Box.createRigidArea(new Dimension(5,5))); 
box1.add(tableLabel);

另请参阅:如何使用 BoxLayout:使用不可见组件作为填充物

于 2013-10-25T07:53:32.980 回答
2

您应该使用基于网格的布局管理器。这是使用GridBagLayout的网格部分的示例:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class GridExample extends JPanel {
    GridExample() {
        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();

        gbc.anchor = GridBagConstraints.WEST;
        gbc.gridy = 0;
        add(new JLabel("DB URL: "), gbc);
        gbc.gridy++;
        add(new JLabel("Protocol: "), gbc);
        gbc.gridy++;
        add(new JLabel("Port: "), gbc);
        gbc.gridy++;
        add(new JLabel("Username: "), gbc);
        gbc.gridy++;
        add(new JLabel("Password: "), gbc);
        gbc.gridy++;
        add(new JLabel("Table: "), gbc);
        gbc.gridy = 0;
        gbc.gridx = 1;
        add(new JTextField(16), gbc);
        gbc.gridy++;
        add(new JTextField(16), gbc);
        gbc.gridy++;
        add(new JTextField(16), gbc);
        gbc.gridy++;
        add(new JTextField(16), gbc);
        gbc.gridy++;
        add(new JPasswordField(16), gbc);
        gbc.gridy++;
        add(new JTextField(16), gbc);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame("sample");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLocationByPlatform(true);
                frame.add(new GridExample());
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}

这导致:

示例图像

有比 GridBag 更容易使用的基于网格的布局管理器,但它的优点是位于标准库中。如果你最终做了很多这样的布局,那么看看第三方布局管理器是值得的,比如 MigLayout 或 DesignGridLayout。

于 2013-10-25T08:08:15.800 回答
0
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(20, 20, 20, 20); //here you specify the space between
panel0.add(label0, c);                //top, right, bottom and left

它适用于我的标签。

于 2015-04-04T16:27:07.673 回答