java – 获取swing组件的位置

前端之家收集整理的这篇文章主要介绍了java – 获取swing组件的位置前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我把一些JPanel放到另一个JPanel中,它的布局是Box Layout和Y-Axis.
在我放置所有面板后,我需要从JPanel容器面板中获取每个添加的JPanel的Y位置.但是,当我试图得到它时,每个JPanel的Y位置总是为零.请告诉我如何从JPanel容器JPanel获取每个Jpanel的Y位置.

这就是我所做的,

JPanel panelHolder = new JPanel(new BoxLayout(this,BoxLayout.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
}

解决方法

将你的JPanel添加到JFrames contentPane,这样你就可以使用getX()和get()获得X和Y坐标,虽然我建议先添加所有组件,因为这些点可能随着更多组件的添加而变化,然后变为trashgod说只需在框架实例上调用pack().

我做了一个简短的样本来证明:

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());
        }
    }
}

正如你所看到的,调用printPanelCompPoints(mainPanel);在initComponents(JFrame框架)中产生全0,(因为它们已添加到框架中,但尚未调用`pack()).

0,0

0,0

但是在调用printPanelCompPoints(mainPanel)的JFrames实例上调用pack()之后的createAndShowUI()中;产生正确的共同作用:

44,26

0,52

99,26

原文链接:https://www.f2er.com/java/121624.html

猜你在找的Java相关文章