1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.lang.reflect.InvocationTargetException;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class JFrameTest extends JFrame {
private class Info extends JPanel{
JLabel longitude;
JLabel latitude;
public Info(){
longitude=new JLabel("...");
latitude=new JLabel("...");
setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
this.add(longitude);
this.add(latitude);
}
public JLabel getLongitude() {
return longitude;
}
public void setLongitude(JLabel longitude) {
this.longitude = longitude;
}
public JLabel getLatitude() {
return latitude;
}
public void setLatitude(JLabel latitude) {
this.latitude = latitude;
}
}
private Info infos;
public JFrameTest(){
this.getContentPane().add(infos=new Info());
this.addMouseMotionListener(new MouseMotionListener() {
public void mouseMoved(MouseEvent me) {
infos.getLongitude().setText(""+me.getX());
infos.getLatitude().setText(""+me.getY());
// infos.revalidate();
System.out.println(me.getX() + " - " + me.getY()); // pour test
}
public void mousePressed(MouseEvent me) {}
public void mouseDragged(MouseEvent me) {}
});
this.setSize(new Dimension(500,200));
this.setVisible(true);
}
public static void main (String[] argv) throws InterruptedException, InvocationTargetException{
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
new JFrameTest();
}
});
}
} |
Partager