如何在GroupLayout中获取组件的位置?

时间:2017-03-27 14:06:07

标签: java swing jbutton grouplayout

我有问题。当我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);
   }
 }

0 个答案:

没有答案