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 105 106 107 108 109 110 111 112 113
| package testSizeJSP2pack;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class TestJSP extends JPanel{
private JFrame f;
private JPanel bg;
JEditorPane jep;
JScrollPane jsp;
public TestJSP(JFrame f){
this.f=f;
this.jep= new JEditorPane();
jep.setContentType("text/html");
jep.setText(" test ");
//this.jsp=jsp =new JScrollPane(jep);
this.add(jep/*jsp*/,
new GridBagConstraints(0,0,1,1,1,1, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0,0,0,0),0,0)
);
bg= new JPanel();
bg.setBackground(Color.red);
bg.setPreferredSize(new Dimension(400,400));
this.f.getContentPane().add(bg, BorderLayout.CENTER);
f.getLayeredPane().addComponentListener(this.topLevelContainerListener);
f.getLayeredPane().add(this,JLayeredPane.POPUP_LAYER);
f.pack();
f.setVisible(true);
}
final ComponentAdapter topLevelContainerListener = new ComponentAdapter(){
public void componentResized(ComponentEvent e){
setPosition();
}
};
public static void main(String[] args){
JFrame f = new JFrame();
TestJSP t = new TestJSP(f);
t.jep.setText("<html>Bonjour <br> je voudrais <br> une canette SVP </html>");
t.jep.setPreferredSize(t.jep.getPreferredSize());
//t.jsp.revalidate();
t.setPosition();
}
public void setPosition(){
this.jep.setPreferredSize(jep.getPreferredSize());
//this.jsp.revalidate();
// La position du panneau t se calcule à partir du milieu de la fenetre qui la contient
int xDroit = (int)(this.bg.getWidth()/2.0); //Extrémité droite du panneau à afficher
int yBas = (int) (this.bg.getHeight()/2.0); //Extrémité basse du panneau à afficher
int xGauche = (int) (xDroit - this.getPreferredSize().getWidth());
int yHaut = (int) (yBas - this.getPreferredSize().getHeight());
System.out.println("xG "+ xGauche +" xD "+ xDroit);
System.out.println("yT "+ yHaut +" yB "+ yBas);
System.out.println("largeur "+ this.getPreferredSize().getWidth() + " longueur "+ this.getPreferredSize().getHeight());
System.out.println("largeur jep "+ this.jep.getPreferredSize().getWidth() + " longueur jep "+ this.jep.getPreferredSize().getHeight());
if(xGauche<0){
xGauche = 0;
}
if(yHaut<0){
yHaut = 0;
}
int longueur = xDroit - xGauche;
int largeur = yBas - yHaut;
this.setBounds(xGauche, yHaut, largeur, longueur);
this.jep.setPreferredSize(jep.getPreferredSize());
//this.jsp.revalidate();
//this.revalidate();
}
} |
Partager