将组件放置在任意(x,y)坐标处
我想在JPanel
中的随机位置(x,y)放置一些按钮,这些布局类很烦人。
这在Swing中甚至可能吗?
如果使用null布局,则可以设置坐标:
panel.setLayout(null); Button b = new Button(....); panel.add(b); b.setSize(width, height); b.setLocation(x,y);
但强烈建议使用布局。 布局类不会“烦人”,如果您理解它们,它们就是您的朋友。
我建议阅读一篇关于GridBagLayout的教程 ,它很容易理解(有点html表)并且非常强大。
使用null
作为“布局管理器”:
http://docs.oracle.com/javase/tutorial/uiswing/layout/none.html