Note that there are some explanatory texts on larger screens.

plurals
  1. POProblems implementing GridBagLayout in Java
    primarykey
    data
    text
    <p>I am trying to implement GridBagLayout in Java for a Sign in dialog that I have to make for a program that I am building. I am going for the clean Google sign in. The main issue that I am having is that the constraints that I have set using <code>GridBagConstraints</code> is not working. Here is what I would like the dialog to look like. </p> <p><img src="https://i.stack.imgur.com/fNyBj.png" alt="A simple sign in dialog using java"></p> <p>And here is the code for the dialog I am trying to implement.</p> <pre><code>import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Login_Dialog extends JDialog{ // SEtting up the required components for the sign in /** * */ private static final long serialVersionUID = 1L; protected JLabel username_Label = new JLabel("Username"); protected JLabel password_Label = new JLabel("Username"); protected JTextField username_Field = new JTextField(30); protected JTextField password_Field = new JTextField(30); protected JButton sign_In = new JButton("Sign in!"); public Login_Dialog() { setSize(600,400); setTitle("Sign in"); setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 0; c.gridy = 0; add(username_Label); c.fill = GridBagConstraints.HORIZONTAL; // c.weightx = 0.5; c.gridx = 1; c.gridy = 0; add(username_Field); c.fill = GridBagConstraints.HORIZONTAL; c.weightx = 0.5; c.gridx = 0; c.gridy = 2; add(password_Label); c.fill = GridBagConstraints.HORIZONTAL; c.weightx = 0.5; c.gridx = 0; c.gridy = 4; add(password_Field); c.fill = GridBagConstraints.HORIZONTAL; c.weightx = 0.5; c.gridx = 0; c.gridy = 5; add(sign_In); setVisible(true); } } </code></pre> <p>UPDATE: </p> <p>I have made some changes and it seems that I am reaching my desired result. Now the issue is that everything is centred and the length of the button is too wide. Also, I would like the textfield and label to much bigger.</p> <p>Here is the updated for the <code>GridBagLayout</code></p> <pre><code>//cusotmization of buttons Dimension signD = sign_In.getPreferredSize(); signD.height = 50; signD.width = 30; sign_In.setPreferredSize(signD); GridBagConstraints c = new GridBagConstraints(); c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 0; c.gridy = 0; add(username_Label,c); c.fill = GridBagConstraints.HORIZONTAL; // c.weightx = 0.5; c.gridx = 0; c.gridy = 1; add(username_Field,c); c.fill = GridBagConstraints.HORIZONTAL; // c.weightx = 0.5; c.gridx = 0; c.gridy = 2; add(password_Label,c); c.fill = GridBagConstraints.HORIZONTAL; // c.weightx = 0.5; c.gridx = 0; c.gridy = 3; add(password_Field,c); // // c.fill = GridBagConstraints.HORIZONTAL; // c.weightx = 0.5; c.gridx = 0; c.gridy = 5; // add(sign_In,c); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload