1

I've created a jFrame form in a package in netbeans. The project is without a main class. I've placed a button from the palette. The following is the code for the button:

 int x = jButton1.getHorizontalAlignment();
 int y = jButton1.getVerticalAlignment();
     JButton button2=new JButton();              
     button2.setBounds(200, 200, 100, 100);    
     button2.setVisible(true);

The second button will not show. Why? the x and y are to be used later for relative positioning. I would also like to know how to do that besides x+something and y+something in the coordinate parameters of the .setBounds().

4

1 回答 1

4

第二个按钮不会显示。为什么?

因为,您还没有将按钮添加到JPanel.

除了 .setBounds() 的坐标参数中的 x+something 和 y+something 之外,我还想知道如何做到这一点。

要使 setBounds 起作用,您需要将容器的布局设置为 null,这是非常非常糟糕的做法。因为,它降低了应用程序跨平台的可移植性,并且非常必须使用 setBounds 来维护代码。你应该让秋千内置布局来完成它的工作。看看这里:布局管理器的视觉指南

于 2013-06-30T11:24:56.963 回答