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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
| package essais_Patrice;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.LayoutManager;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class DandD
{
public static void main (String[] args)
{
JFrame frame = new JFrame( "Test DnD" );
frame.getContentPane().add( new PaysRoi("<html><div align='center'><i>Empire<br>Byzantin</i></div></html>","<html><div align='center'>Michel II<br>820 à 829</div></html>") );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.getContentPane ().setBackground (Color.CYAN);
frame.setSize( 600, 600 );
frame.setVisible( true );
}
}
class PaysRoi extends JPanel implements MouseMotionListener
{
public PaysRoi(String pays,String roi)
{
setOpaque(false);
JLabel Lpays=new JLabel(pays);
Lpays.setAlignmentX(CENTER_ALIGNMENT);
Lpays.setOpaque (false);
JLabel Lroi= new JLabel(roi);
Lroi.setAlignmentX (CENTER_ALIGNMENT);
Lroi.setBorder (BorderFactory.createBevelBorder (1, Color.gray, Color.BLACK));
Lroi.setOpaque (false);
add (Lpays);
add (Lroi);
setLayout(new LayoutManager()
{
public void addLayoutComponent (String name, Component comp)
{}
public void layoutContainer (Container parent)
{
Component[] composants=parent.getComponents ();
int w0=composants[0].getPreferredSize ().width;
int w1=composants[1].getPreferredSize ().width;
int w=Math.max(w0,w1);
int h0=composants[0].getPreferredSize ().height;
int h1=composants[1].getPreferredSize ().height;
int h=h0+h1;
composants[0].setBounds ((w-w0)/2, 0, w0, h0);
composants[1].setBounds ((w-w1)/2,h0,w1,h1);
parent.setBounds (0,0,w,h);
}
public Dimension minimumLayoutSize (Container parent)
{
return null ;
}
public Dimension preferredLayoutSize (Container parent)
{
return null ;
}
public void removeLayoutComponent (Component comp)
{}
});
addMouseMotionListener( this );
}
public void mouseDragged (MouseEvent e)
{
this.setLocation (this.getLocation ().x+e.getX ()-this.getSize ().width/2,this.getLocation ().y + e.getY ()-this.getSize ().height/2);
}
public void mouseMoved (MouseEvent e)
{}
} |
Partager