Georgi Koemdzhiev
Georgi Koemdzhiev

Reputation: 11931

Border layout in java

I am following a java programming book and I just found something that I did not get. In the following piece of code, more specifically in the constructor of the class "MainFrame" we add a label and setting the layout of the frame at the same time, if I understand it correctly. I thought we need to set out the type of the layout first and than to add elements to the frame.

package fontframe;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

public class MainFrame extends JFrame{
private static final int FRAME_WIDTH = 300;
private static final int FRAME_HEIGHT = 300;

private JLabel label;
private JCheckBox italicCheckBox;
private JCheckBox boldCheckBox;
private JRadioButton smallButton;
private JRadioButton mediumButton;
private JRadioButton largeButton;
private JComboBox facenameCombo;
private ActionListener listener;

public MainFrame(){
    label = new JLabel("Big Java");
    add(label,BorderLayout.CENTER);
.......
}

Upvotes: 0

Views: 83

Answers (1)

Rod_Algonquin
Rod_Algonquin

Reputation: 26198

I thought we need to set out the type of the layout first and than to add elements to the frame.

Yes you are right, but the default layout of the JFrame is BorderLayout and for label is FlowLayout. So there is no need to specify the layout for the JFrame that it is already a BorderLayout.

From documentation:

And the child will be added to the contentPane. The content pane will always be non-null. Attempting to set it to null will cause the JFrame to throw an exception. The default content pane will have a BorderLayout manager set on it.

Upvotes: 2

Related Questions