programmingIsSilly
programmingIsSilly

Reputation: 3

Why are some properties of a component, in a constructed JPanel, expressed but not others?

I'm beginning a program where I construct a JFrame and add a JPanel to it using another class that I have created called StartPanel. When the JPanel constructs, it adds a JLabel as defined in the StartPanel class. This panel is then added to the JFrame. Some properties of the label are expressed in the GUI (it adds itself to the panel, can add a red border across it, can change the text) but some are not. For instance, I cannot change the location or size of the label.

Everything that I have read about the issue so far has (i think) has agreed with my approach or hasn't clarified much. Additionally, I have added borders to both the StartPanel and the label to visualize the issue and to confirm that the label is not changing it's size. I cannot adjust either the size or location properties of the label. How can I address this issue and control the properties of the label?

Additionally, I understand that I could circumvent the entire issue by not constructing the panel through a separate class and declaring the label in the same function. However, this project is going to be fairly large so I would prefer to have a system of classes where I can construct a panel as needed.

Code time:

Where I construct the frame and panel using the class:

public class StartFrame {

    public StartFrame() {
        JFrame frame = new JFrame("Constuctor tests");
        frame.setSize(800, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

        JPanel startPanel = new StartPanel();  //This is where I construct the panel
        startPanel.setVisible(true);
        startPanel.setBorder(BorderFactory.createLineBorder(Color.blue)); //Visualize the panel
        frame.add(startPanel);

    }

}

The StartPanel class:

public class StartPanel extends JPanel {

    public StartPanel() {
        JLabel label = new JLabel("This is a label");
        label.setLocation(100, 100); //It is not at 100,100
        label.setSize(200, 100); //It is not a box this large
        label.setBorder(BorderFactory.createLineBorder(Color.red)); //Done as to visualize it
        label.setVisible(true);
        add(label);

    }
}

Finally, a picture of the product: The final product

Thank you so much for your help!

Upvotes: 0

Views: 28

Answers (1)

Sijmen
Sijmen

Reputation: 506

You can control the size and layout of elements through layout managers. You can read all about it here.

To get you started, let's take a look why your screen looks like that.

The default layout of a JFrame is the BorderLayout. The BorderLayout takes it's first child (in this case the JPanel), places it in the center and stretches it to take up the full width of the container (in this case the JFrame). That's why your StartPanel takes up the full size of the JFrame. Here you can read more about how to use the border layout.

The default layout for a JPanel is the FlowLayout. In the flow-layout every child element (in this case the JLabel) is added to the center top. When more children are added they are added next to each other in a single row. The FlowLayout lets its children take up the size that they want. In this case the JLable only needs a small rectangle to show the text, so that is the size the JLabel will be. Go here to learn more about the FlowLayout.

Upvotes: 1

Related Questions