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
| package com.jacquemin.chart;
import java.awt.BorderLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Chart extends JPanel {
private JLabel tit = new JLabel();
private BorderLayout layout = new BorderLayout();
private String position = BorderLayout.NORTH;
protected Chart() {
tit.setText("Title Chart");
this.setLayout(layout);
this.add(tit, position);
this.validate();
}
protected void setTitre(String titre, String position) {
if (position.equals(BorderLayout.NORTH)
|| position.equals(BorderLayout.SOUTH)
|| position.equals(BorderLayout.EAST)
|| position.equals(BorderLayout.WEST) && position != this.position) {
this.position = position;
tit.setText(titre);
this.getLayout().removeLayoutComponent(tit);
this.add(tit, position);
this.validate();
}
}
public static void createFrame() throws InterruptedException{
javax.swing.JFrame fen = new javax.swing.JFrame();
fen.setVisible(true);
Chart ch = new Chart();
fen.add(ch);
//fen.validate();
Thread.sleep(1000);
ch.setTitre("Nouveau titre",BorderLayout.SOUTH);
Thread.sleep(1000);
ch.setTitre("Nouveau titre",BorderLayout.NORTH);
}
} |