在使用JLayeredPane按z顺序添加组件时,我注意到了一些问题:
JLayeredPane lp = getLayeredPane();
JButton top = new JButton(); ...
JButton middle = new JButton(); ...
JButton bottom = new JButton(); ...效果不佳:
lp.add(top,2);
lp.add(middle,1);
lp.add(bottom,3);效果很好:
lp.add(top,new Integer(2));
lp.add(middle,new Integer(1));
lp.add(bottom,new Integer(3));你可以在这里看到它的样子:http://i.imgur.com/eqH2El8.png
为什么文字常量不能转换成Integer对象,而且不能正常工作?
发布于 2016-08-08 23:43:30
本质上,因为它从(容器)继承的类有一个在它的组件列表中的给定位置添加组件的函数(add(Component comp, int layer) ),以及一个添加具有任何给定参数的组件的函数(传递给LayoutManager) (add(Component comp, Object constraint)。
为了调用正确的函数(以及接收约束的JLayeredPane的LayoutManager ),参数必须是对象Integer而不是原始int。
发布于 2016-08-08 23:43:30
为什么文字常量不能转换成整型对象,而且不能正常工作?
您需要查看add(...)方法的API。
Container类有一个接受"int“作为参数的方法。
JLayeredPane类有一个方法,它接受一个"Integer“值来指定组件的分层。
因此,您不能依赖自动装箱将int转换为Integer。
https://stackoverflow.com/questions/38832661
复制相似问题