我已尝试搜索此答案并尝试了一些我在此处的帖子中找到的东西,但似乎没有任何效果。
我所拥有的是:
有了这个:
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);
}
我需要标签与字段对齐,但无论我尝试什么,标签总是在彼此下方结束。