如何获取动态添加的JLabel的JPanel中的getLocation()?

发布于 2024-11-30 11:56:16 字数 1055 浏览 1 评论 0原文

我动态创建了许多 JLabel,并使用以下代码将它们添加到 JPanel(代码应该足以理解我希望的问题!)。

    JPanel textPanel = new JPanel();
    map = new HashMap<Integer,JLabel>();
    vec = new Vector<JLabel>();
    for(int i = 0; i<getCount();i++){ // getCount() returns int
               JLabel label = new JLabel(getItemText(i)); // getItemText() returns String
               map.put(i, label);
               vec.add(label);
               textPanel.add(map.get(i));
    }

现在,我尝试访问这些标签的位置,但在尝试通过以下代码访问它们时,除了 java.awt.Point[x=296,y=63] 之外什么也得不到。

System.out.println("Component position [1]: " +
textPanel.getComponent(1).getLocationOnScreen());

我对所有组件都获得相同的位置,而不仅仅是一个组件。

另外(更重要的是)我得到了以下代码的位置 java.awt.Point[x=0,y=0]

System.out.println("Position of Component 1: " + map.get(1).getLocation());

我猜这与 JLabels 是动态创建的事实有关。然而,我确实需要动态创建它们,而且确实需要能够通过getLocation()获取它们的位置。

请帮忙!也许有另一种方法来创建它们或以不同的方式访问它们的位置?

I have created a number of JLabels dynamically and have added them to a JPanel with the following code (code should be enough to understand the problem I hope!).

    JPanel textPanel = new JPanel();
    map = new HashMap<Integer,JLabel>();
    vec = new Vector<JLabel>();
    for(int i = 0; i<getCount();i++){ // getCount() returns int
               JLabel label = new JLabel(getItemText(i)); // getItemText() returns String
               map.put(i, label);
               vec.add(label);
               textPanel.add(map.get(i));
    }

Now I am trying to access the Location of these labels but get nothing but java.awt.Point[x=296,y=63] for them when trying to access them via the following code.

System.out.println("Component position [1]: " +
textPanel.getComponent(1).getLocationOnScreen());

I get the same position for all Components, not just for the one.

Also (more importantly) I get the position java.awt.Point[x=0,y=0] for the following code.

System.out.println("Position of Component 1: " + map.get(1).getLocation());

I'm guessing this is to do with the fact that the JLabels are created dynamically. I really need to create them dynamically, however, and also really need to be able to get their Location via getLocation().

Please help! Perhaps there is another way to create them or a way to access their Location(s) differently?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

流心雨 2024-12-07 11:56:17

当你创建一个组件时,它的默认位置是 (0, 0);

将组件添加到面板不会更改此位置。

将所有标签添加到面板后,您需要执行以下操作:

panel.revalidate();

这将调用面板使用的布局管理器,然后根据布局管理器的规则为每个标签分配适当的位置。

When you create a component it has a default location of (0, 0);

Adding a component to a panel does NOT change this location.

After adding all the labels to the panel you need to do:

panel.revalidate();

This will invoke the layout manager used by the panel and each label will then be assigned a proper location based on the rules of the layout manager.

若水微香 2024-12-07 11:56:17

这是一个 SSCCE。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class WhereIsMyComponent {

    public static void showComponentLocations(Container parent) {
        Component[] all = parent.getComponents();
        System.out.println("Show locations of children..");
        for (Component c : all) {
            System.out.println(c.getLocation());
        }
    }

    public static void main(String[] args) {
        String msg = "Hello World!";
        final JPanel p = new JPanel(new FlowLayout());
        for (int ii=0; ii<6; ii++) {
            p.add(new JLabel(msg));
        }

        ComponentListener cl = new ComponentAdapter() {
            @Override
            public void componentResized(ComponentEvent ce) {
                showComponentLocations(p);
            }
        };

        p.addComponentListener(cl);

        JFrame f = new JFrame("Where Is My Component?");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(p);
        f.pack();
        f.setSize(400,300);
        f.setLocationByPlatform(true);
        f.setVisible(true);
    }
}

典型输出

Show locations of children..
java.awt.Point[x=16,y=5]
java.awt.Point[x=89,y=5]
java.awt.Point[x=162,y=5]
java.awt.Point[x=235,y=5]
java.awt.Point[x=308,y=5]
java.awt.Point[x=162,y=26]
Show locations of children..
java.awt.Point[x=16,y=5]
java.awt.Point[x=89,y=5]
java.awt.Point[x=162,y=5]
java.awt.Point[x=235,y=5]
java.awt.Point[x=308,y=5]
java.awt.Point[x=162,y=26]
Show locations of children..
java.awt.Point[x=26,y=5]
java.awt.Point[x=99,y=5]
java.awt.Point[x=172,y=5]
java.awt.Point[x=26,y=26]
java.awt.Point[x=99,y=26]
java.awt.Point[x=172,y=26]
Press any key to continue . . .

Here is an SSCCE.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class WhereIsMyComponent {

    public static void showComponentLocations(Container parent) {
        Component[] all = parent.getComponents();
        System.out.println("Show locations of children..");
        for (Component c : all) {
            System.out.println(c.getLocation());
        }
    }

    public static void main(String[] args) {
        String msg = "Hello World!";
        final JPanel p = new JPanel(new FlowLayout());
        for (int ii=0; ii<6; ii++) {
            p.add(new JLabel(msg));
        }

        ComponentListener cl = new ComponentAdapter() {
            @Override
            public void componentResized(ComponentEvent ce) {
                showComponentLocations(p);
            }
        };

        p.addComponentListener(cl);

        JFrame f = new JFrame("Where Is My Component?");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(p);
        f.pack();
        f.setSize(400,300);
        f.setLocationByPlatform(true);
        f.setVisible(true);
    }
}

Typical output

Show locations of children..
java.awt.Point[x=16,y=5]
java.awt.Point[x=89,y=5]
java.awt.Point[x=162,y=5]
java.awt.Point[x=235,y=5]
java.awt.Point[x=308,y=5]
java.awt.Point[x=162,y=26]
Show locations of children..
java.awt.Point[x=16,y=5]
java.awt.Point[x=89,y=5]
java.awt.Point[x=162,y=5]
java.awt.Point[x=235,y=5]
java.awt.Point[x=308,y=5]
java.awt.Point[x=162,y=26]
Show locations of children..
java.awt.Point[x=26,y=5]
java.awt.Point[x=99,y=5]
java.awt.Point[x=172,y=5]
java.awt.Point[x=26,y=26]
java.awt.Point[x=99,y=26]
java.awt.Point[x=172,y=26]
Press any key to continue . . .
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文