gpt4 book ai didi

java - 在Java中指定JLabels高度之间一定的间距

转载 作者:行者123 更新时间:2023-12-01 18:42:02 25 4
gpt4 key购买 nike

我尝试过寻找这个答案,并尝试了我在此处的帖子中找到的一些内容,但似乎没有任何效果。

我拥有的是这样的:

Screenshot of current dialog

这样:

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

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

最佳答案

试试这个:

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

另请参阅: How to Use BoxLayout: Using Invisible Components as Filler .

关于java - 在Java中指定JLabels高度之间一定的间距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19583883/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com