Java:如何注册侦听JFrame运动的侦听器 [英] Java: how to register a listener that listen to a JFrame movement
本文介绍了Java:如何注册侦听JFrame运动的侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何跟踪JFrame本身的移动?我想注册一个每次都会被回调的监听器 JFrame.getLocation()
将返回一个新值。
How can you track the movement of a JFrame itself? I'd like to register a listener that would be called back every single time JFrame.getLocation()
is going to return a new value.
编辑以下代码显示已接受的答案正在解决我的问题:
EDIT Here's a code showing that the accepted answered is solving my problem:
import javax.swing.*;
public class SO {
public static void main( String[] args ) throws Exception {
SwingUtilities.invokeAndWait( new Runnable() {
public void run() {
final JFrame jf = new JFrame();
final JPanel jp = new JPanel();
final JLabel jl = new JLabel();
updateText( jf, jl );
jp.add( jl );
jf.add( jp );
jf.pack();
jf.setVisible( true );
jf.addComponentListener( new ComponentListener() {
public void componentResized( ComponentEvent e ) {}
public void componentMoved( ComponentEvent e ) {
updateText( jf, jl );
}
public void componentShown( ComponentEvent e ) {}
public void componentHidden( ComponentEvent e ) {}
} );
}
} );
}
private static void updateText( final JFrame jf, final JLabel jl ) {
// this method shall always be called from the EDT
jl.setText( "JFrame is located at: " + jf.getLocation() );
jl.repaint();
}
}
推荐答案
JFrame jf = new JFrame();
jf.addComponentListener(new ComponentListener() {...});
是你想要的。
这篇关于Java:如何注册侦听JFrame运动的侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文