2013-10-25 26 views
0

我试过寻找这个答案,并尝试了一些我在这里的帖子中发现的东西,但似乎没有任何工作。在Java中JLabels高度之间指定一定的空间量

什么我已经是这样的:

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

我需要的标签与字段来排队,但不管我怎么努力标签总是落得正下方彼此。

+0

您是否尝试过使用Spring布局来获取您想要的标签?我只看到了一些例子,但在GUI上放置组件似乎很有效 – Levenal

回答

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

参见: How to Use BoxLayout: Using Invisible Components as Filler

+0

谢谢,添加了链接。 ! –

+0

我将会使用它,因为它与我已有的工作。感谢堆这个答案! – adamb53

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

这导致:

Sample image

有易于使用的基于网格布局管理器比网格包,而是一个具有作为优势在标准库中。如果你最终做了很多这样的布局,那么值得看第三方布局管理器,比如MigLayout或者DesignGridLayout。

+0

+1但是非常集中 – mKorbel

+0

@mKorbel哦,是啊!不知何故,当我专注于做网格时,我错过了这一点。我将添加锚定。感谢您的通知。 – kiheru

+0

我喜欢这种方法,但因为我已经在使用盒子布局,而其他答案只需要添加几行我将使用,但我会在下次需要这种布局时记住这一点。非常感谢! – adamb53

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 

它适用于我的标签。