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
|
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
class MaFenetre8 extends JFrame implements ActionListener
{
MaFenetre8()
{
setTitle("Essai texte");
setSize(300,150);
pan = new Panneau8();
contenu = getContentPane();
contenu.add(pan);
bouton = new JButton("Cliquez ici");
contenu.add(bouton, BorderLayout.SOUTH);
bouton.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if(source == bouton)
{
pan = new Panneau9();
contenu.add(pan);
}
}
private JPanel pan;
private JButton bouton;
private Container contenu;
}
class Panneau8 extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
int x = 20, y = 30;
String ch1 = "bonjour";
String ch2 = " monsieur";
g.drawString(ch1, x, y);
FontMetrics fm = g.getFontMetrics();
y += fm.getHeight();
g.drawString(ch2, x, y);
}
}
class Panneau9 extends JPanel
{
public void PaintComponent(Graphics g)
{
super.paintComponent(g);
int x = 20, y = 30;
String ch1 = "Hello";
String ch2 = " Sir";
g.drawString(ch1, x, y);
FontMetrics fm = g.getFontMetrics();
y += fm.getHeight();
g.drawString(ch2, x, y);
}
}
public class PremTxt1
{
public static void main(String args[])
{
MaFenetre8 fen = new MaFenetre8();
fen.setVisible(true);
fen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
} |
Partager