我在 netbeans 的包中创建了一个 jFrame 表单。该项目没有主类。我从调色板中放置了一个按钮。以下是按钮的代码:
int x = jButton1.getHorizontalAlignment();
int y = jButton1.getVerticalAlignment();
JButton button2=new JButton();
button2.setBounds(200, 200, 100, 100);
button2.setVisible(true);
第二个按钮不会显示。为什么? x 和 y 稍后将用于相对定位。 我还想知道除了 .setBounds() 的坐标参数中的 x+something 和 y+something 之外该怎么做。
最佳答案
The second button will not show. Why?
因为,您还没有将按钮添加到 JPanel
。
I would also like to know how to do that besides x+something and y+something in the coordinate parameters of the .setBounds().
要使 setBounds 起作用,您需要将容器的布局设置为 null,这是非常非常糟糕的做法。因为,它降低了应用程序跨平台的可移植性,而且非常必须使用 setBounds 来维护代码。您应该让 swing 内置布局来完成它的工作。看看这里:A Visual Guide to Layout Managers
关于java - 以 JFrame 形式创建对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17389748/