I have to make this for school:
This is the code I have so far:
import javax.swing.*;
import java.awt.*;
public class AddressBookGui1 extends JFrame {
public AddressBookGui1(){
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
setLayout(gbl);
JLabel label;
JButton button;
JTextField textField;
JTextArea textArea = new JTextArea(10, 20);
gbc.weightx = 1;
label = new JLabel("text");
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 0;
add(label ,gbc);
textField = new JTextField();
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 1;
gbc.gridy = 0;
add(textField ,gbc);
label = new JLabel("text");
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 1;
add(label ,gbc);
textField = new JTextField();
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 1;
gbc.gridy = 1;
gbc.gridwidth = 2;
add(textField, gbc);
label = new JLabel("text");
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 2;
gbc.gridwidth = 1;
add(label ,gbc);
textField = new JTextField();
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 1;
gbc.gridy = 2;
gbc.gridwidth = 2;
add(textField, gbc);
label = new JLabel("text");
gbc.weightx = 1;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 3;
gbc.gridwidth = 1;
add(label ,gbc);
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.CENTER;
gbc.gridwidth = 2;
gbc.gridx = 1;
gbc.gridy = 3;
add(textArea, gbc);
gbc.weightx = 1;
button = new JButton("text");
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 1;
gbc.gridx = 0;
gbc.gridy = 4;
add(button ,gbc);
gbc.weightx = 1;
button = new JButton("text");
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 3;
gbc.gridy = 4;
add(button ,gbc);
}
public static void main(String[] args){
AddressBookGui1 frame = new AddressBookGui1();
frame.setTitle("Address Book");
frame.setSize(400, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
(I still need to deal with padding and insets. I've gotten those to work in a much simpler program so I think I have a handle on that stuff)
I've tried the GridBagLayout Oracle tutorial and I'm not sure what I'm doing wrong. Can someone help me make it look more like it is supposed to? Specifically to make the text fields and text area span over 2 cells.
Few things I noticed about your code.
Do not use setSize()
of JFrame. This will cause abnormal behavior.
Instead, let the frame size itself according to the size of its
components. If you want the frame to be bigger, adjust not the size
of the frame but the components inside it. You can either
setpreferredSize or override the getpreferredsize of the component if
you really want to adjust is size since GridBagLayout is one of those layout managers that respects the
preferredSize of the component. Use pack()
to remove the unecessary
space.
Do not extend a JFrame, make your UI class to have a main panel and
add all the components there. provide a getter for that panel (e.g.
getUI()
) for extracting the UI of that class.
Always reinstantiate the GridBagConstraints object whenever it is going to be applied to another component. This way it is more readable.
Use insets to put padding around the component.
Do not reuse same reference to different components;
Use Initial Thread
This is not standard but it I find it really helpful when working
with GridBagLayout
, in setting the constraints of gbc, make it in
alphabetical order.
To solve your problem, here is the modified code with the things I pointed out about applied.
public class AddressBook {
private JPanel pnlMain;
public AddressBook() {
pnlMain = new JPanel();
pnlMain.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
JLabel lblName = new JLabel("Name");
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(0, 10, 0, 0);
gbc.weightx = 1;
pnlMain.add(lblName, gbc);
JTextField txtName = new JTextField();
gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 3;
gbc.gridx = 1;
gbc.gridy = 0;
gbc.insets = new Insets(5, 0, 0, 10);
gbc.weightx = 1;
pnlMain.add(txtName, gbc);
JLabel lblPhone = new JLabel("Phone");
gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 1;
gbc.gridx = 0;
gbc.gridy = 1;
gbc.insets = new Insets(0, 10, 0, 0);
gbc.weightx = 1;
pnlMain.add(lblPhone, gbc);
JTextField txtPhone = new JTextField();
gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 3;
gbc.gridx = 1;
gbc.gridy = 1;
gbc.insets = new Insets(5, 0, 0, 10);
gbc.weightx = 1;
pnlMain.add(txtPhone, gbc);
JLabel lblEmail = new JLabel("Email");
gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 1;
gbc.gridx = 0;
gbc.gridy = 2;
gbc.insets = new Insets(0, 10, 0, 0);
gbc.weightx = 1;
pnlMain.add(lblEmail, gbc);
JTextField txtEmail = new JTextField();
gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 3;
gbc.gridx = 1;
gbc.gridy = 2;
gbc.weightx = 1;
gbc.insets = new Insets(5, 0, 0, 10);
pnlMain.add(txtEmail, gbc);
JLabel lblAddress = new JLabel("Address");
gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 1;
gbc.gridx = 0;
gbc.gridy = 3;
gbc.insets = new Insets(0, 10, 0, 0);
gbc.weightx = 1;
pnlMain.add(lblAddress, gbc);
JTextArea txtAreaAddress = new JTextArea(10, 20);
JScrollPane pane = new JScrollPane(txtAreaAddress);
gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.NORTH;
gbc.fill = GridBagConstraints.BOTH;
gbc.gridwidth = 3;
gbc.gridx = 1;
gbc.gridy = 3;
gbc.insets = new Insets(5, 0, 0, 10);
gbc.weightx = 1;
pnlMain.add(pane, gbc);
JButton btnSave = new JButton("Save");
gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.NONE;
gbc.gridwidth = 1;
gbc.gridx = 0;
gbc.gridy = 4;
gbc.insets = new Insets(10, 10, 10, 0);
gbc.weightx = 1;
pnlMain.add(btnSave, gbc);
JButton btnCancel = new JButton("Cancel");
gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.EAST;
gbc.gridwidth = 1;
gbc.gridx = 3;
gbc.gridy = 4;
gbc.insets = new Insets(10, 0, 10, 10);
gbc.weightx = 1;
pnlMain.add(btnCancel, gbc);
}
public JPanel getUI(){
return pnlMain;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("Address Book");
frame.getContentPane().add(new AddressBook().getUI());
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
});
}
}