0

我在互联网上遇到了一个使用这两种说法的例子:

dp.add( bg , new Integer( 50 ) );

(dp 是 JDesktopPane 对象, bg 是 JLabel)

setLayeredPane( dp );

如果您想知道它们是如何使用的,那么这就是我所看到的: http: //www.coderanch.com/t/329874/GUI/java/put-background-image-swing

当谈到 Java 时,我是一个初学者,我理解示例的其余部分,只是不理解这两个语句 - 我不知道他们在做什么,这让我很烦恼!最让我困惑的是“new Integer(50))”,但请你对这两者给出一个彻底的、对初学者友好的解释吗?我会很感激的。

提前致谢,

亚历克斯。

4

1 回答 1

2

See the documentation on JLayeredPane.

Each layer is a distinct integer number. The layer attribute can be set on a Component by passing an Integer object during the add call. For example:

layeredPane.add(child, JLayeredPane.DEFAULT_LAYER);

or

layeredPane.add(child, new Integer(10));

You can find the integer values of the default layer values here.


dp.add(lbl,new Integer(50));

The above adds the JLabel component lbl to the JDesktopPane (which is a JLayeredPane) with the specified layer of 50. Components added to dp with layers that are less than 50 will be rendered before, while components with layers greater than 50 will be rendered after -- i.e. a simple depth order, where greater layers refer to nearer components.

setLayeredPane( dp );

This sets the JFrame represented by the ImagePaneTest object (which probably shouldn't be a subclass) to use dp as its layered pane. You can see how Swing top-level containers work in the relevant Java Tutorial.

于 2012-08-08T18:06:40.850 回答