我有问题。当我getLocation
按钮b2
时,它返回0-0。那么我可以获得b2
的位置吗?
public class TestF {
public static void main(String[] args) {
JFrame frame = new JFrame("GroupLayout");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = frame.getContentPane();
GroupLayout groupLayout = new GroupLayout(contentPane);
contentPane.setLayout(groupLayout);
JLabel label = new JLabel("Label");
JButton b2 = new JButton("Second Button");
groupLayout.setHorizontalGroup(groupLayout.createSequentialGroup()
.addComponent(label).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(b2));
groupLayout.setVerticalGroup(groupLayout
.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(label)
.addComponent(b2));
System.out.println(b2.getLocation());
frame.pack();
frame.setVisible(true);
}
}