I am kind of new to Java Programming and I am trying to make a window that contains two buttons and a text area, as seen in the image below.
The problem I encountered though was positioning the components. I tried using GridLayout
and separating the window into 9 rows and 16 cells, but then found I couldn't make components occupy more than a cell. I know I should be using GridBagLayout
but I don't know how exactly. Help would be appreciated. :)
The Java GridBagLayout class is used to align components vertically, horizontally or along their baseline. The components may not be of the same size. Each GridBagLayout object maintains a dynamic, rectangular grid of cells. Each component occupies one or more cells known as its display area.
A GridLayout puts all the components in a rectangular grid and is divided into equal-sized rectangles and each component is placed inside a rectangle whereas GridBagLayout is a flexible layout manager that aligns the components vertically and horizontally without requiring that the components be of the same size.
Try something like this: GridBagLayout gbl=new GridBagLayout(); setLayout(gbl); GridBagConstraints gbc=new GridBagConstraints(); gbc. insets = new Insets(10, 10, 10, 10); JLabel jl = new JLabel("This is a JLabel!", SwingConstants. CENTER); jl.
GridBagLayout is one of the most flexible — and complex — layout managers the Java platform provides. A GridBagLayout places components in a grid of rows and columns, allowing specified components to span multiple rows or columns. Not all rows necessarily have the same height.
You have a number of choices. Instead of trying to layout the whole component in one, try using a compound layout, where by you layout sections of the UI in separate panes and focus on the individual requirements of each section...
public class TestLayout11 {
public static void main(String[] args) {
new TestLayout11();
}
public TestLayout11() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new ExamplePane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
protected class ExamplePane extends JPanel {
public ExamplePane() {
setLayout(new GridBagLayout());
JPanel buttonPane = new JPanel(new GridBagLayout());
JButton btnOkay = new JButton("Ok");
JButton btnCancel = new JButton("Cancel");
JTextArea textArea = new JTextArea(5, 20);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.CENTER;
buttonPane.add(btnOkay, gbc);
gbc.gridy++;
gbc.insets = new Insets(50, 0, 0, 0);
buttonPane.add(btnCancel, gbc);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(100, 100, 100, 100);
add(buttonPane, gbc);
gbc.insets = new Insets(150, 100, 150, 100);
gbc.gridx++;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.BOTH;
add(new JScrollPane(textArea), gbc);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With