当前位置: 首页 > 知识库问答 >
问题:

为什么jlabel.getLocationonScreen()根据父JPanel布局而不同地返回

汝楷
2023-03-14
  1. 面板。添加(标签);
  2. Panel.setLayout(new BorderLayout());panel.add(label,borderlayout.north);

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

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

尽管我保持所有其他代码不变,但下面的方法调用给出了非常不同的结果:

    Point location = getLocationOnFrame(label);
    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;
    }
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]

共有1个答案

戴树
2023-03-14

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

然而,我相信你在为自己做更多的工作...

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

将标签坐标转换为windows坐标空间。

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

        }

    }

}
 类似资料:
  • 我想知道我们是否可以有一个布局不同于其父JFrame的JPanel。例如。如果我有JFrame与边框布局,我们有一个JPanel嵌入到它,它有不同的布局。有可能吗? 我正在努力做这件事。但是这样JPanel的组件就不会显示出来了。

  • 我正在使用Microsoft Graph Users API根据我们的ActiveDirectory验证用户名或电子邮件地址列表。名称搜索: https://graph.microsoft.com/v1.0/me/people/?$search=John.Smith 返回其他数据,如和。但如果我使用电子邮件搜索: null 我是否需要额外的权限来获取相同的数据? 更新:我按照下面的建议在https

  • 问题内容: 当左花括号在新行上时,返回,并且警报中显示“ no-it break:undefined”。 当花括号与处于同一行时,将返回一个对象,并警告“奇妙”。 问题答案: 这是JavaScript的陷阱之一:自动分号插入。不以分号结尾但可能是语句结尾的行会自动终止,因此您的第一个示例实际上是这样的: 在第二个示例中,您返回一个对象(由花括号构建),该对象的属性及其值为,实际上与此相同:

  • 我使用DrawerLayout,最近我想改变抽屉布局中listView的重力。但是在我从将listView的重力更改为后,抽屉布局无法锁定 setDrawerLockMode()工作; ` 有什么线索可以解释为什么我不能在其他引力中使用锁定模式吗? 谢谢

  • 问题内容: 查看javadoc,我看到ArrayList有一个重载的add方法: public boolean add(E e) 将指定的元素追加到此列表的末尾。 和 public void add(int index,E元素) 将指定的元素插入此列表中的指定位置。将当前在该位置的元素(如果有)和任何后续元素右移(将其索引添加一个)。 我注意到第一个返回了一个,而第二个返回了一个。事实证明,第一个

  • 问题内容: 我已经阅读了一些文章,但没有得到实际的信息。有人可以向我解释。 仅与IE6相关吗? 这是什么? 是IE唯一的TAG吗? 编辑: 我发现此信息对我很有帮助 由于Internet Explorer很老(因为它是最早可用的浏览器之一),所以它没有像现在的浏览器那样重新启动。因此,随着时间的流逝,Microsoft开始改编新引擎以使用CSS。看起来不错……但是,CSS改变了Internet E