Questo codice che ho scritto al volo, ad esempio, crea un semplice effetto calamita quando il JInternalFrame è a 20 pixel dal bordo destro.
codice:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Calamita extends JFrame {
private class Interno extends JInternalFrame {
public Interno() {
setSize(300, 300);
setTitle("Titolo");
setVisible(true);
}
}
private class CL extends ComponentAdapter {
private Component p;
public CL(Component p) { this.p = p; }
public void componentMoved(ComponentEvent ce) {
Component c = ce.getComponent();
if (c.getLocation().x+c.getBounds().width >= p.getBounds().width-20) {
c.setLocation( p.getBounds().width-c.getBounds().width-2, c.getLocation().y);
}
p.repaint();
}
}
private JDesktopPane jdp;
private Interno interno;
public Calamita() {
jdp = new JDesktopPane();
setContentPane( jdp );
interno = new Interno();
interno.addComponentListener( new CL(this) );
jdp.add( interno );
setTitle("Titolo esterno");
setSize(800, 600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String [] args) {
Calamita c = new Calamita();
}
}
Ciao.