Java JScrollPane getX()返回错误的值?
我想在按钮上方的ActionPerformed上放置一个小Jframe 我直接尝试获取添加按钮的JScrollPane的X(getX())和Y(getY())坐标,但它似乎总是返回错误的坐标 jScrollPane1.getLocation()返回的值 上述值与我在屏幕上放置JScrollPane的位置无关Java JScrollPane getX()返回错误的值?,java,swing,jframe,Java,Swing,Jframe,我想在按钮上方的ActionPerformed上放置一个小Jframe 我直接尝试获取添加按钮的JScrollPane的X(getX())和Y(getY())坐标,但它似乎总是返回错误的坐标 jScrollPane1.getLocation()返回的值 上述值与我在屏幕上放置JScrollPane的位置无关 如果我删除JScrollPane并直接尝试获取Jpanels坐标,这是可行的 这个好方法将帮助您: // Convert a coordinate relative to a compone
如果我删除JScrollPane并直接尝试获取Jpanels坐标,这是可行的 这个好方法将帮助您:
// 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();
这个好方法将帮助您:
// 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();
比如说
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);
}
}
}
比如说
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);
}
}
}
由于要在给定组件的正上方生成一个新帧,因此需要获取组件的屏幕坐标 为此,您需要使用组件的
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);
}
由于要在给定组件的正上方生成一个新帧,因此需要获取组件的屏幕坐标 为此,您需要使用组件的
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);
}
@哈利·乔伊:-)和OP的
java.awt.Point[x=10,y=170]
@哈利·乔伊:-)和OP的java.awt.Point[x=10,y=170]一样的/simlair数量
只有这可能是正确的方式,我的坏,我的脑袋里找不到空的硬盘空间,我的+1只有这可能是正确的方式,我的坏,我在我的脑袋里,我的+1,找不到空的硬盘空间来存储SwingGutilities