简体   繁体   English

BorderLayout和JPanel与JLabel

[英]BorderLayout and JPanel with JLabel

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. 我的JButton会正常显示。 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. 我的黑色JLabel不会出现,我想要。 Why does this happen? 为什么会这样? Thanks a lot 非常感谢

JLabel c = new JLabel();

You have an empty label, so I'm guessing the size if (0, 0) and there is nothing to paint. 你有一个空标签,所以我猜测大小if(0,0)并且没有什么要画。 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. 阅读BorderLayout API或Using a Border Layout的Swing教程以获取适当的约束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM