1

我用的是Jave Swing,简化的层次结构可以理解为JFrame -> JPanel -> JLabel。

现在我可以选择将 JLabel 添加到 JPanel 中:

  1. 面板。添加(标签);
  2. panel.setLayout(new BorderLayout()); panel.add(label, BorderLayout.NORTH);

这是我在两种情况下都在做的常见配置

panel.setOpaque(false);
panel.setBorder(BorderFactory.createLineBorder(Color.yellow));

即使我保持所有其他代码不变,以下方法调用也会给出非常不同的结果:

    Point location = getLocationOnFrame(label);

我调用这个方法来识别 JLabel 相对于 JFrame 的位置:

    public Point getLocationOnFrame (JLabel label) {
        if (label == null) return null;

        try {
            Point location = label.getLocationOnScreen();
            System.out.println(location);
            Point frameOffset = mainFrame.getLocationOnScreen();
            System.out.println(frameOffset);
            location.translate(-frameOffset.x, -frameOffset.y);
            return location;
        } catch (IllegalComponentStateException e) {}

        return null;
    }

这些是 System.out.println() 的结果,即使标签出现在同一个地方。

java.awt.Point[x=578,y=305]
java.awt.Point[x=0,y=0]

java.awt.Point[x=224,y=300]
java.awt.Point[x=0,y=0]

在我看来,在第二种情况下,数字来自父 JPanel 的左上角,而不是 JLabel 本身。

基本上我正在尝试使用第二种情况的代码并从第一种情况下获取数字。

4

1 回答 1

3

至于为什么你会得到不同的结果,我完全不知道。

但是,我相信你正在为自己做更多的工作......

Point loc = label.getLocation();
Window win = SwingUtilities.getWindowAncestor(label);
Point wrPos = SwingUtilities.convertPoint(label, loc, win);

将为您将标签坐标转换为窗口坐标空间。

知道记住,框架的位置是框架的左上角(不,真的是;)),但是你的标签存在于框架的contentPane边界内,它在框架边界内偏移,所以取决于你想要做什么,你可能会更好地使用contentPane而不是框架;)

这是我用来测试逻辑的一个简单示例;)

public class TestLocation {

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

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

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new LocationPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class LocationPane extends JPanel {

        private JLabel label;

        public LocationPane() {
            setLayout(new GridBagLayout());
            label = new JLabel("A Label");
            add(label);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();

            // While this works, really, don't do this, in paint methods
            // it's expensive and will slow down your system ;)
            Point loc = label.getLocation();
            Point pos = label.getLocationOnScreen();
            Window win = SwingUtilities.getWindowAncestor(this);
            Point wPos = win.getLocationOnScreen();
            Point wLoc = win.getLocation();

            Point rPos = SwingUtilities.convertPoint(label, label.getLocation(), win);
            JRootPane rootPane = SwingUtilities.getRootPane(this);
            Point cPos = SwingUtilities.convertPoint(label, loc, rootPane.getContentPane());
            Point wrPos = SwingUtilities.convertPoint(label, loc, win);

            FontMetrics fm = g2d.getFontMetrics();
            int rowHeight = fm.getHeight();

            String[] messages = new String[]{
                "Label.location = " + loc.x + "x" + loc.y,
//                "Label.locationOnScreen = " + pos.x + "x" + pos.y,
//                "Window.location = " + wLoc.x + "x" + wLoc.y,
//                "Window.locationOnScreen = " + wPos.x + "x" + wPos.y,
                "RelativeTo.Window = " + wrPos.x + "x" + wrPos.y,
                "RelativeTo.RootPane = " + rPos.x + "x" + rPos.y,
                "RelativeTo.ContentPane = " + cPos.x + "x" + cPos.y,
            };

            int y = 0;
            for (String msg : messages) {
                g2d.drawString(msg, 0, y + fm.getAscent());
                y += rowHeight;
            }

        }

    }

}
于 2012-11-09T05:52:30.180 回答