【菜鸟】Java awt组件重叠时如何置当前位置显示
【求助】【初学者】Java awt组件重叠时怎么置当前位置显示
给重叠组件 排上下顺序,求哪位大哥讲解一下!小弟先谢谢了!
------解决思路----------------------
可以通过设置容器的ZOrder来改变组件的叠放次序。
给重叠组件 排上下顺序,求哪位大哥讲解一下!小弟先谢谢了!
------解决思路----------------------
可以通过设置容器的ZOrder来改变组件的叠放次序。
public static void main(String[] args) {
JFrame fr = new JFrame();
JLabel l1 = new JLabel("Lable1"), l2 = new JLabel("Lable2");
l1.setForeground(Color.red);
l2.setForeground(Color.blue);
fr.getContentPane().setLayout(null);
l1.setSize(40, 30);
l2.setSize(40, 30);
l1.setLocation(10, 10);
l2.setLocation(11, 11);
fr.getContentPane().add(l2);
fr.getContentPane().add(l1);
// 设置组件的叠放顺序,数值越小越靠上,取值范围是0到n-1
fr.getContentPane().setComponentZOrder(l1, 1);
fr.getContentPane().setComponentZOrder(l2, 0);
fr.setSize(100, 100);
fr.setLocationRelativeTo(null);
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fr.setVisible(true);
}