[英]ImageIcon get Parent JButton
I have an ImageIcon
inside of a JButton
. 我在
JButton
有一个ImageIcon
。 The JButton
knows what it's "position" is relative to another Object, but the ImageIcon
doesn't. JButton
知道它的“位置”是相对于另一个对象的,但ImageIcon
却不知道。 How can I get the JButton
element from within the ImageIcon
class? 如何从
ImageIcon
类中获取JButton
元素?
I tried something like this: 我试过这样的事情:
storedPosition = getParent().getPosition();
but I'm getting a 但我得到了一个
The method getParent() is undefined for the type Piece
对于类型Piece,方法getParent()未定义
error. 错误。
How can I get the JButton element from within the ImageIcon class?
如何从ImageIcon类中获取JButton元素?
Yes you can do it using ImageIcon#getImageObserver() and ImageIcon#setImageObserver() . 是的,您可以使用ImageIcon#getImageObserver()和ImageIcon #setImageObserver()来完成 。
Sample code: 示例代码:
ImageIcon icon = new ImageIcon();
JButton btn = new JButton(icon);
// set the Image Observer of the ImageIcon
icon.setImageObserver(btn);
...
// get Image Observer back from ImageIcon
JButton observer = (JButton) icon.getImageObserver();
if (observer == btn) {
System.out.println("We got the JButton from ImageIcon");
}
output: 输出:
We got the JButton from ImageIcon
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.