JScrollPane getX() 返回错误值?

发布于 2024-12-06 01:20:54 字数 306 浏览 0 评论 0原文

我希望在按钮上方放置一个小 Jframe,在 ActionPerformed 上

我直接尝试获取添加按钮的 JScrollPane 的 X (getX()) 和 Y(getY()) 坐标,但它似乎总是 错误坐标

返回jScrollPane1.getLocation() 返回的

java.awt.Point[x=10,y=170]

值上述值与我将 JScrollPane 放置在屏幕上的位置无关。

如果我删除 JScrollPane 并直接尝试获取 Jpanels 坐标,这将有效!

I wish to place a small Jframe right above the Button, on ActionPerformed

I directly tried to get the X (getX()) and Y(getY()) co-ordinates of the JScrollPane in which the button is added, but it always seems to return wrong co-coordinates

values returned by jScrollPane1.getLocation()

java.awt.Point[x=10,y=170]

The above values are same independent on where I place the JScrollPane on the screen.

This works if I remove the JScrollPane and directly try to get the Jpanels co-ordinates!!

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

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

发布评论

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

评论(3

雅心素梦 2024-12-13 01:20:54

例如

private void showDialog() {
    if (canShow) {
        location = myButton.getLocationOnScreen();
        int x = location.x;
        int y = location.y;
        dialog.setLocation(x - 466, y - 514);
        if (!(dialog.isVisible())) {
            Runnable doRun = new Runnable() {

                @Override
                public void run() {
                    dialog.setVisible(true);
                    //setFocusButton();
                    //another method that moving Focus to the desired JComponent
                }
            };
            SwingUtilities.invokeLater(doRun);
        }
    }
}

for example

private void showDialog() {
    if (canShow) {
        location = myButton.getLocationOnScreen();
        int x = location.x;
        int y = location.y;
        dialog.setLocation(x - 466, y - 514);
        if (!(dialog.isVisible())) {
            Runnable doRun = new Runnable() {

                @Override
                public void run() {
                    dialog.setVisible(true);
                    //setFocusButton();
                    //another method that moving Focus to the desired JComponent
                }
            };
            SwingUtilities.invokeLater(doRun);
        }
    }
}
陌伤浅笑 2024-12-13 01:20:54

这个很好的方法会帮助你:

// Convert a coordinate relative to a component's bounds to screen coordinates
Point pt = new Point(component.getLocation());
SwingUtilities.convertPointToScreen(pt, component);
// pt is now the absolute screen coordinate of the component

添加:我没有意识到,但就像mKorbel写的那样,你可以简单地调用

Point pt = component.getLocationOnScreen();

This nice method will help you:

// Convert a coordinate relative to a component's bounds to screen coordinates
Point pt = new Point(component.getLocation());
SwingUtilities.convertPointToScreen(pt, component);
// pt is now the absolute screen coordinate of the component

Add: I didn't realise, but like mKorbel wrote, you can simply call

Point pt = component.getLocationOnScreen();
椒妓 2024-12-13 01:20:54

由于您想要在给定组件的正上方生成一个新框架,因此您需要获取组件的屏幕坐标。

为此,您需要使用组件的 getLocationOnScreen() 方法。

这是一个有用的代码片段:

public void showFrameAboveCmp(Frame frame, Component cmp) {
    Dimension size = cmp.getSize();
    Point loc = cmp.getLocationOnScreen();
    Dimension frameSize = frame.getSize();
    loc.x += (size.width  - frameSize.width)/2;
    loc.y += (size.height - frameSize.height)/2;
    frame.setBounds(loc.x, loc.y, frameSize.width, frameSize.height);
    frame.setVisible(true);
}

Since you want to spawn a new frame right above a given component, you want to get the screen coordinates of your component.

For this, you need to use the getLocationOnScreen() method of your component.

Here is a useful code snippet :

public void showFrameAboveCmp(Frame frame, Component cmp) {
    Dimension size = cmp.getSize();
    Point loc = cmp.getLocationOnScreen();
    Dimension frameSize = frame.getSize();
    loc.x += (size.width  - frameSize.width)/2;
    loc.y += (size.height - frameSize.height)/2;
    frame.setBounds(loc.x, loc.y, frameSize.width, frameSize.height);
    frame.setVisible(true);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文