Bonsoir,

Je suis en train de développer une application en java. Actuellement, je travail sur mon menu mais je n'arrive pas à centrer mon texte "Menu" sur mon panel.
Quelqu'un aurait-il une solution ? Voici mon code :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
 
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URL;
 
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
 
@SuppressWarnings("serial")
public class Fenetre1 extends JFrame implements ActionListener {
 
	public JButton bouton1,bouton2,bouton3,bouton5;
 
	public Fenetre1() {
	  initComponents();
	}
 
    public void initComponents() {	
 
        setTitle("Fenetre 1");
        setSize(800, 500);
 
        setIconImage(Toolkit.getDefaultToolkit().getImage("src/img/icone.png"));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        setLocationRelativeTo(null);
        setContentPane(buildContentPane());
 
        java.awt.Font myFont = new java.awt.Font ("Serif", Font.BOLD+Font.ITALIC , 25 ) ;
 
        bouton1 = new JButton("Version 1"); 					//ajout bouton1
    	bouton1.setFont ( myFont ) ;
    	bouton1.setBackground(Color.red);
    	bouton1.setBounds(270,260, 250, 50);
    	add(bouton1);
    	bouton1.addActionListener(this);
 
    	bouton2 = new JButton("Version 2");						//ajout bouton2
    	bouton2.setFont ( myFont ) ;
    	bouton2.setBackground(Color.red);
    	bouton2.setBounds(270,315, 250, 50);
    	add(bouton2);
    	bouton2.addActionListener(this);
 
    	bouton3 = new JButton("Version 3");						//ajout bouton3
    	bouton3.setFont ( myFont ) ;
    	bouton3.setBackground(Color.red);
    	bouton3.setBounds(270,370, 250, 50);
    	add(bouton3);
    	bouton3.addActionListener(this);
 
        javax.swing.GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
 
 
        JLabel icone;											 //ajout image menu 
    	ImageIcon image = new ImageIcon("src/img/icone2.png");
    	icone = new JLabel ("",image,JLabel.CENTER);
    	icone.setBounds(300,50,200,200);
    	add(icone);
 
    }
 
	private JPanel buildContentPane(){
        ImageFond1 panel = new ImageFond1();
    	JLabel label = new JLabel("Menu",JLabel.CENTER);
        Font Font1 = new java.awt.Font ("Serif", Font.BOLD+Font.ITALIC , 25 );
        label.setFont(Font1);
        label.setForeground(Color.white);
        label.setOpaque(false);
        panel.add(label);
		return panel;
	}
 
	@Override
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		if  (source==bouton1)
		 {	 
			dispose();
			fenetre_2 fra = new fourmi_v2(); //creation fenetre
			fra.run();
			bouton5 = new JButton("Menu");
			bouton5.setBackground(Color.red);
			bouton5.setBounds(0,0, 250, 50);
			fra.add(bouton5);
			bouton5.addActionListener(this);
			 }
	     else if(source==bouton5)
		 {
	    	 System.out.println(this.getClass().getName());
			 System.out.println("ok");
			 Fenetre1 fen= new Fenetre1();
		 }	
	     else if (source==bouton2)
	     {
	 		dispose();
	 		fenetre_3 fr = new fourmi_v4(); //creation fenetre
	 		fr.run();
	 		bouton5 = new JButton("Menu");
			bouton5.setBackground(Color.red);
			bouton5.setBounds(0,0, 250, 50);
			fr.add(bouton5);
			bouton5.addActionListener(this);
	 	 }
	     else if (source==bouton3)
	     {
	    	dispose();
	  		fenetre_4 fr5 = new fourmi_v5(); //creation fenetre
	  		fr5.run();
	  		bouton5 = new JButton("Menu");
	 		bouton5.setBackground(Color.red);
	 		bouton5.setBounds(0,0, 250, 50);
	 		fr5.add(bouton5);
	 		bouton5.addActionListener(this);
	     }
	}
	 public static void main(String[] args) {
			try {
		        URL url = Fenetre1.class.getClassLoader().getResource("music.wav"); //importation du son de type .wav
		        AudioInputStream audioIn = AudioSystem.getAudioInputStream(url); //Obtient un flux d'entrée audio à partir de fichier fourni 
		        Clip clip = AudioSystem.getClip(); // Obtient un clip pour lire un fichier 
		        clip.open(audioIn); //ouverture du son 
		        clip.start(); //commencer à jouer le son
		    }
			catch (LineUnavailableException | UnsupportedAudioFileException | IOException e) {
				throw new RuntimeException(e);
			}
	Fenetre1 fen= new Fenetre1();
	 }
}
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
 
@SuppressWarnings("serial")
public class ImageFond1 extends JPanel {
 
        public void paintComponent(Graphics g){
        	super.paintComponents(g);
                try {
                        Image img = ImageIO.read(new File("src/img/ecran.jpg"));
                        g.drawImage(img, 0, 0, this);
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
 
        } 
}
Merci d'avance