获取秋千组件的位置 [英] Get location of a swing component

查看:126
本文介绍了获取秋千组件的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将一些JPanels放到另一个JPanel中,它的布局是Box Layout和Y轴. 在放置所有面板之后,我需要从JPanel容器面板中获取每个添加的JPanel的Y位置.但是,当我尝试获得该值时,每个JPanel的Y位置总会得到零.请告诉我如何从JPanel容器JPanel获取每个Jpanel的Y位置.

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 from the JPanel container panel. But, when I try to get that I always get zero for Y position of each JPanel. Please tell me how to get the Y position of each Jpanel from the JPanel container JPanel.

这就是我所做的,

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添加到JFrame contentPane 中,从而允许您使用get(),尽管我建议先添加所有组件,因为随着添加的组件的增加,点数可能会发生变化,然后如垃圾桶所说,只需在框架实例上调用pack().

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

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

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

0,0

但是,在JFrame实例上调用pack()后调用printPanelCompPoints(mainPanel);

createAndShowUI()中会产生正确的共配酸盐:

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

44,26

44, 26

0,0

0,52

99、26

0,26

这篇关于获取秋千组件的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆