Get location of a swing component

前端 未结 6 1234
野的像风
野的像风 2021-01-18 03:19

I have put some JPanels into another JPanel which its\' layout is Box Layout and Y-Axis. After I have put all the panels I need to get the Y position of each added JPanel in

6条回答
  •  执笔经年
    2021-01-18 04:17

    Add your JPanel to the JFrames contentPane thus allowing you to get the X and Y co ordinates using getX() and get() though I'd suggest adding all components first, as the points may change as more components are added and then as trashgod said simply call pack() on the frame instance.

    I did a short sample to demonstrate:

    import java.awt.BorderLayout;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class Test {
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new Test().createAndShowUI();
                }
            });
        }
    
        private void createAndShowUI() {
            JFrame frame = new JFrame("Test");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setResizable(false);
            initComponents(frame);
            
            frame.pack();//call pack 
            
            printPanelCompPoints(mainPanel);//produces correct coords
            
            frame.setVisible(true);
        }
        
        private JPanel mainPanel;
    
        private void initComponents(JFrame frame) {
            mainPanel = new JPanel(new BorderLayout());
            JPanel centerPanel = new JPanel();
            JPanel northPanel = new JPanel();
            JPanel southPanel = new JPanel();
            JPanel westPanel = new JPanel();
            JPanel eastPanel = new JPanel();
    
            centerPanel.add(new JLabel("CENTER"));
            northPanel.add(new JLabel("NORTH"));
            eastPanel.add(new JLabel("EAST"));
            southPanel.add(new JLabel("SOUTH"));
            westPanel.add(new JLabel("WEST"));
    
            mainPanel.add(centerPanel, BorderLayout.CENTER);
            mainPanel.add(northPanel, BorderLayout.NORTH);
            mainPanel.add(southPanel, BorderLayout.SOUTH);
            mainPanel.add(eastPanel, BorderLayout.EAST);
            mainPanel.add(westPanel, BorderLayout.WEST);
    
           frame.getContentPane().add(mainPanel);
        
           printPanelCompPoints(mainPanel);//produces all 0's
        }
    
        private void printPanelCompPoints(JPanel mainPanel) {
            for (int i = 0; i < mainPanel.getComponentCount(); i++) {
                System.out.println(mainPanel.getComponent(i).getX() + ", " + mainPanel.getComponent(i).getY());
            }
        }
    }
    

    As you can see calling printPanelCompPoints(mainPanel); in initComponents(JFrame frame) produces all 0's, (as they have been added to the frame but `pack() has not been called).

    0, 0

    0, 0

    0, 0

    0, 0

    0, 0

    however in the createAndShowUI() after calling pack() on the JFrames instance calling printPanelCompPoints(mainPanel); produces the correct co-odrinates:

    44, 26

    0, 0

    0, 52

    99, 26

    0, 26

提交回复
热议问题