4

我在另一个具有复杂 JPanels 结构和大量代码的项目中面临这个问题。

在那里我有一个以下片段

Point point = label.getLocation();
Point point2 = SwingUtilities.convertPoint(label.getParent(), point, panel);

其中标签和面板是动态选择的,JLabel 应该是 JPanel 的 grand-grand-...-child。

那么为什么问题是SwingUtilities.convertPoint()如果源和目标具有子父关系,在什么情况下可以返回负值?这可能吗?

此代码演示了负坐标问题,但 panel22 不是标签的父级。

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Point;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class NegativeLocation {
    public static void main(String[] args) {
        new NegativeLocation();
    }

    public NegativeLocation() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());

                JPanel panel1 = new JPanel() {
                     @Override
                     public Dimension getPreferredSize() {
                         return new Dimension(600, 400);
                     }
                };

                JPanel panel21 = new JPanel() {
                    @Override
                    public Dimension getPreferredSize() {
                        return new Dimension(300, 400);
                    }
                };
                panel1.add(panel21);

                JPanel panel22 = new JPanel() {
                    @Override
                    public Dimension getPreferredSize() {
                        return new Dimension(300, 400);
                    }
                };
                panel1.add(panel22);

                JPanel panel3 = new JPanel() {
                    @Override
                    public Dimension getPreferredSize() {
                        return new Dimension(300, 400);
                    }
                };
                panel21.add(panel3);

                JLabel label = new JLabel("Label");
                panel3.add(label);

                frame.getContentPane().add(panel1);
                frame.setPreferredSize(new Dimension(600, 400));

                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

                Point point = label.getLocation();
                Point point2 = SwingUtilities.convertPoint(label.getParent(), point, panel21);
                Point point3 = SwingUtilities.convertPoint(label.getParent(), point, panel22);

                System.out.println(point2);
                System.out.println(point3);
            }
        });
    }
}

所以输出是

java.awt.Point[x=134,y=10]
java.awt.Point[x=134,y=-395]

UPD:基本上,我需要获取label相对于panel21. 我怎样才能修改这条线来实现它?

Point point = label.getLocation();
Point point2 = SwingUtilities.convertPoint(label.getParent(), point, panel21);
4

2 回答 2

7

对我来说,这是正确的行为。想象一下。主面板包含一个位置 x=0 y=100 的子项。如果您将主面板的 0,0 点转换为子面板,它将返回 0, -100。事实上,哪个组件是哪个组件的父级并不重要。逻辑是添加(全部减去)父渲染子时应用于图形的所有 translate() 调用。

于 2012-12-07T08:14:45.720 回答
4

举个例子……

在此处输入图像描述

public class TestLocation01 {

    public static void main(String[] args) {
        new TestLocation01();
    }

    public TestLocation01() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JPanel outter = new JPanel(new BorderLayout());
                outter.setBorder(new CompoundBorder(new LineBorder(Color.RED), new EmptyBorder(10, 10, 10, 10)));
                JPanel inner = new JPanel(new GridBagLayout());
                inner.setBorder(new CompoundBorder(new LineBorder(Color.BLUE), new EmptyBorder(10, 10, 10, 10)));
                JLabel label = new JLabel("Testing");
                inner.add(label);
                outter.add(inner);

                JLabel below = new JLabel("Below");

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(outter);
                frame.add(below, BorderLayout.SOUTH);
                frame.setSize(200, 200);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

                System.out.println("lable.location: " + label.getLocation());
                System.out.println("label to inner: " + SwingUtilities.convertPoint(label, new Point(0, 0), inner));
                System.out.println("label to outter: " + SwingUtilities.convertPoint(label, new Point(0, 0), outter));
                System.out.println("label.parent to inner: " + SwingUtilities.convertPoint(label.getParent(), new Point(0, 0), inner));
                System.out.println("label.parent to outter: " + SwingUtilities.convertPoint(label.getParent(), new Point(0, 0), outter));
                System.out.println("label to frame: " + SwingUtilities.convertPoint(label, new Point(0, 0), frame));
                System.out.println("label.getParent to frame: " + SwingUtilities.convertPoint(label.getParent(), new Point(0, 0), frame));
                System.out.println("outter to label: " + SwingUtilities.convertPoint(outter, new Point(0, 0), label));
                System.out.println("label to below: " + SwingUtilities.convertPoint(label, new Point(0, 0), below));
                System.out.println("below to label: " + SwingUtilities.convertPoint(below, new Point(0, 0), label));
            }
        });
    }        
}

生成(在我的系统上)以下输出

lable.location: java.awt.Point[x=65,y=62]
label to inner: java.awt.Point[x=65,y=62]
label to outter: java.awt.Point[x=76,y=73]
label.parent to inner: java.awt.Point[x=0,y=0]
label.parent to outter: java.awt.Point[x=11,y=11]
label to frame: java.awt.Point[x=76,y=95]
label.getParent to frame: java.awt.Point[x=11,y=33]
outter to label: java.awt.Point[x=-76,y=-73]
label to below: java.awt.Point[x=76,y=-89]
below to label: java.awt.Point[x=-76,y=89]

如果我将作为源组件传递,我会根据和我试图转换到其坐标空间的组件label之间的关系来获得相对位置。label如果我传递label's 的父级,我使用的是父级坐标空间,这势必会产生不正确的结果

如果您查看 和 之间的关系labelbelow您会得到负面结果是有道理的,如label上方和右侧below

于 2012-12-07T08:45:32.333 回答