How can I make the component range of multiple cells in a GridBagLayout

I need to do this for school:

GUI

This is the code that I still have:

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

This is the result

(I still need to deal with additions and inserts. I got them to work in a much simpler program, so I think I have a handle to this material)

I tried the Oracle GridBagLayout tutorial and I'm not sure what I'm doing wrong. Can someone help me make him look more like what he should? In particular, so that the text fields and the text area cover more than 2 cells.

+6
source share
2 answers

A few things I noticed about your code.

  • Do not use setSize() for a JFrame. This will cause abnormal behavior. Instead, let's frame the size according to the size of its components. If you want the frame to be larger, adjust the frame size, but the components inside it. You can also setpreferredSize or override the getpreferredsize of the component if you really want to adjust the size, since GridBagLayout is one of those layout managers that respects the preferred size of the component. Use pack() to remove unnecessary space.

  • Do not extend the JFrame, create a UI class for the main panel and add all the components there. provide a getter for this panel (e.g. getUI() ) to retrieve the UI of this class.

  • Always restore the GridBagConstraints object whenever it is applied to another component. Therefore, it is more readable.

  • Use inserts to add add-ons to a component.

  • Do not re-link to different components;

  • Use Start Stream

  • This is not standard, but I think it is really useful when working with GridBagLayout , when setting gbc restrictions, do it in alphabetical order.

To solve your problem, here is a modified code with the instructions that I pointed out applicable.

 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); } }); } } 
+8
source

gbc.gridwidth is a parameter that allows a component to span more than one column. For example, if you have 3 columns and 4 rows, and you want the label to occupy the full top row, you need to assign the first cell to the label. and set gbc.gridwidth = 3;

+3
source

Source: https://habr.com/ru/post/947704/


All Articles