3

Good day, please would like to know how to reduce the spaces between the labels and the Textboxes in this picture below and also how to create some spaces between the labels and the frame.Thank you.

login page

My code for this:

private void initUI(JFrame parent) {
    // private void initUI() {
   myPanel = new JPanel(new GridLayout(3,2,1,1));
   buttons_panel = new JPanel(new FlowLayout());

   username_label = new JLabel("Username: ");
   password_label = new JLabel("Password: ");
   username = new JTextField(20);
   password = new JPasswordField(20);

   ok = new JButton("Ok");
   ok.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {

        }
    });

    cancel = new JButton("Cancel");
    cancel.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
           dispose();
        }
    });

    myPanel.add(username_label);
    myPanel.add(username);
    myPanel.add(password_label);
    myPanel.add(password);

    buttons_panel.add(ok);
    buttons_panel.add(cancel);

    getContentPane().add(myPanel, BorderLayout.CENTER);
    getContentPane().add(buttons_panel, BorderLayout.PAGE_END);

    pack();
    setResizable(false);
    setLocationRelativeTo(parent);
}

Should i be using GridBagLayout for this instead?..

irobotxx
  • 5,963
  • 11
  • 62
  • 91

3 Answers3

5

All cells in GridLayout have equal size. You have to use GridBagLayout or SpringLayout or BoxLayout.

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
3

or use proper

1) Borders

2) set for Alignment (left - right, top - bottom) for

mKorbel
  • 109,525
  • 20
  • 134
  • 319
2

You can also use the GridLayout hgap and hgap parameters and add a matching, empty Border, as shown here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045