本文介绍了将组件放置在任意(x,y)坐标上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在JPanel中的任意位置(x,y)放置一些按钮,这些布局类很烦人.

I want to place some buttons in a JPanel at random positions (x,y), and these layout classes are annoying.

在Swing中甚至有可能吗?

Is this even possible in Swing?

推荐答案

如果使用空布局,则可以设置坐标:

You can set the coordinates if you use a null layout:

panel.setLayout(null);
Button b = new Button(....);
panel.add(b);
b.setSize(width, height);
b.setLocation(x,y);

但是强烈建议使用布局.布局类不是烦人的",如果您正确理解它们,它们就是您的朋友.

But it is strongly recommended to use layouts. Layout classes are not "annoying", they are your friend if you understand them properly.

我建议阅读有关GridBagLayout的教程,它很容易理解(有点html表格),功能非常强大.

I propose reading a tutorial about GridBagLayout, it is easy to understand (kinda html tables) and very powerful.

这篇关于将组件放置在任意(x,y)坐标上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 19:04