Java – get the location of the swing component

I put some jpanels into another JPanel, and its layout is @ R_ 987_ 2419 @ layout and y-axis

That's what I did,

JPanel panelHolder = new JPanel(new @R_987_2419@Layout(this,@R_987_2419@Layout.Y_AXIS));
for(int i = 0; i< 10; i++){
   JPanel p = new JPanel(); 
   panelHolder.add(p);
}

int componentCount = panelHolder.getComponentCount();

for (int j = 0; i < componentCount ; j++) {

  Component c = pnlRowHolder.getComponent(i);
  JPanel p = (JPanel)c;
  System.out.println(p.getY());//I get zero for all
}

Solution

Add your JPanel to jframes contentpane so that you can use geTx () and get () to obtain X and Y coordinates. Although I suggest adding all components first, because these points may change with the addition of more components, and then trashgod says that you only need to call pack () in the framework instance

I made a short sample to prove:

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, call printpanelcomppoints (mainpanel); Generate all zeros in initcomponents (JFrame framework) (because they have been added to the framework, but ` pack() has not been called yet)

However, in createandshowui() after the jframes instance calling printpanelcomppoints (mainpanel) calls pack(); Produce the right interaction:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>