Reputation: 3429
I am wondering why I am not able to see the topPanel
in my controlPanel
Here is my code:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class GUI {
private JFrame frame;
private JTextArea textArea;
private JPanel topPanel;
private JPanel controlPanel;
private JLabel topLabel;
void createScreen() {
frame = new JFrame("Hello");
frame.setSize(600,600);
frame.setLayout(new GridLayout(3,1));
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
controlPanel.setBackground(Color.GREEN);
topLabel = new JLabel("WELCOME TO MY TRAINING", JLabel.CENTER);
frame.add(topLabel);
frame.add(controlPanel);
topPanel = new JPanel();
BorderLayout borderLayout = new BorderLayout();
borderLayout.setHgap(10);
borderLayout.setVgap(10);
topPanel.setLayout(borderLayout);
topPanel.setBackground(Color.BLUE);
topPanel.setSize(75,300);
textArea = new JTextArea();
textArea.setSize(25, 25);
topPanel.add(textArea, BorderLayout.CENTER);
controlPanel.add(topPanel);
frame.setVisible(true);
}
public static void main(String[] args) {
GUI gui = new GUI();
gui.createScreen();
}
}
Upvotes: 0
Views: 97
Reputation: 5948
FlowLayout uses components preferred size and not the actual size set to it.
To fix your issue set preferred size to topPanel
instead of its size
.
topPanel.setPreferredSize( new Dimension(75,300) );
But my advice is to avoid setting size like this but instead let the TextArea
determine the size by specifying its number of rows and columns like this:
topPanel.setLayout(borderLayout);
topPanel.setBackground(Color.BLUE);
textArea = new JTextArea(10, 15);
Upvotes: 1