Question

I use:

BorderLayout a = new BorderLayout();
setLayout(a);
JPanel b = new JPanel();

now, if I use:

JButton c = new JButton("Press");
b.add(c);
add("East", b);

my JButton will appear normally. BUT if I say instead:

JLabel c = new JLabel();
c.setBackground(Color.BLACK);
c.setOpaque(true);
add("East", b);

my black JLabel won't appear, which I want to. Why does this happen? Thanks a lot

Was it helpful?

Solution

JLabel c = new JLabel();

You have an empty label, so I'm guessing the size if (0, 0) and there is nothing to paint. Try adding some text.

Also the following is incorrect:

add("East", b); 

That is the old way of adding a constraint. Don't use hardcoded values and the constraint is specified second:

add(b, BorderLayout.???);

Read the BorderLayout API or the Swing tutorial on Using a Border Layout for the appropriate constraint.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top