Bonjour,
Je suis en train de créer une application JAVA ayant une interface graphique SWING.
Lorsque je veux lancer, j'appuie sur la flèche verte et cela n'affiche que la Frame et pas les composants.
Je suis obligé de fermer la Frame, et relancer l'application plusieurs fois pour que je puisse avoir l'application bien lancée.
Je ne comprends pas si cela vient de mon programme (étonnant puisque finalement il se lance comme je le désire), d'Eclipse ou bien de mon ordinateur.
Je vous donne ici toutes les classes que j'utilise :
Classe JeuDeDe :
Classe Controller :Code:
1
2
3
4
5
6
7
8
9
10 package com; import com.controller.Controller; public class JeuDeDes { public static void main(String[] args) { Controller control = new Controller(); } }
Classe Fenetre :Code:
1
2
3
4
5
6
7
8
9
10
11 package com.controller; import com.view.*; public class Controller { public Fenetre fenetre = null; public Controller() { fenetre = new Fenetre(); } }
Classe PanneauJoueurs :Code:
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 package com.view; import java.awt.BorderLayout; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import javax.swing.JFrame; import javax.swing.JPanel; import com.view.panels.PanneauCommandes; import com.view.panels.PanneauDes; import com.view.panels.PanneauJoueurs; import com.view.panels.PanneauNbreJoueurs; import com.view.panels.PanneauScore; public class Fenetre extends JFrame{ JPanel panneau = null; public Fenetre() { super("Jeu de dés"); WindowListener l = new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }; addWindowListener(l); this.setExtendedState(JFrame.MAXIMIZED_BOTH); this.setVisible(true); panneau = new JPanel(new BorderLayout()); phaseNbreJoueurs(); } public void phaseJeu() { PanneauJoueurs pj = new PanneauJoueurs(); PanneauCommandes pc = new PanneauCommandes(); PanneauScore ps = new PanneauScore(); PanneauDes pd = new PanneauDes(); panneau.add(ps, BorderLayout.EAST); panneau.add(pj, BorderLayout.NORTH); panneau.add(pd, BorderLayout.CENTER); panneau.add(pc, BorderLayout.SOUTH); this.setContentPane(panneau); } }
Classe PanneauCommandes :Code:
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 package com.view.panels; import java.awt.Color; import java.awt.FlowLayout; import javax.swing.JLabel; import javax.swing.JPanel; import com.view.drawings.LabelsRoundedBorder; public class PanneauJoueurs extends JPanel { public PanneauJoueurs() { super(); this.setLayout(new FlowLayout()); JLabel labelJoueur1 = new JLabel("Louis"); JLabel labelJoueur2 = new JLabel("Lilie"); JLabel labelJoueur3 = new JLabel("Papa"); labelJoueur1.setBackground(Color.YELLOW); labelJoueur1.setForeground(Color.BLUE); labelJoueur1.setOpaque(true); labelJoueur1.setBorder(new LabelsRoundedBorder(Color.BLACK, 20)); labelJoueur2.setBackground(Color.LIGHT_GRAY); labelJoueur2.setForeground(Color.BLACK); labelJoueur2.setOpaque(true); labelJoueur2.setBorder(new LabelsRoundedBorder(Color.BLACK, 20)); labelJoueur3.setBackground(Color.LIGHT_GRAY); labelJoueur3.setForeground(Color.BLACK); labelJoueur3.setOpaque(true); labelJoueur3.setBorder(new LabelsRoundedBorder(Color.BLACK, 20)); this.add(labelJoueur1); this.add(labelJoueur2); this.add(labelJoueur3); } }
Classe PanneauScore :Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.view.panels; import java.awt.FlowLayout; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JPanel; public class PanneauCommandes extends JPanel { public PanneauCommandes() { super(new FlowLayout()); JButton button = new JButton("Lancer les dés"); this.add(button); } }
Classe PanneauDes :Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package com.view.panels; import javax.swing.BoxLayout; import javax.swing.JLabel; import javax.swing.JPanel; public class PanneauScore extends JPanel { public PanneauScore() { super(); int score1 = 5000; int score2 = 3550; JLabel joueur1 = new JLabel("Louis = " + score1); JLabel joueur2 = new JLabel("Lilie = " + score2); this.add(joueur1); this.add(joueur2); this.setLayout (new BoxLayout (this, BoxLayout.Y_AXIS)); } }
Classe LabelsRoundedBorder :Code:
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 package com.view.panels; import java.awt.Color; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import javax.swing.JPanel; import com.view.drawings.DessinDes; public class PanneauDes extends JPanel { public PanneauDes() { super(); DessinDes dd1 = new DessinDes(1, Color.PINK, 100, 200); DessinDes dd2 = new DessinDes(2, Color.MAGENTA, 200, 500); DessinDes dd3 = new DessinDes(3, Color.WHITE, 100, 400); DessinDes dd4 = new DessinDes(4, Color.BLACK, 300, 300); DessinDes dd5 = new DessinDes(5, Color.BLUE, 500 , 100); //dé1 this.add(dd1); //dé2 this.add(dd2); //dé3 this.add(dd3); //dé4 this.add(dd4); //dé5 this.add(dd5); } }
Classe DessinDes :Code:
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 package com.view.drawings; import java.awt.Color; import java.awt.Component; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Insets; import java.awt.RenderingHints; import java.awt.geom.RoundRectangle2D; import javax.swing.border.AbstractBorder; public class LabelsRoundedBorder extends AbstractBorder { private final Color color; private final int gap; public LabelsRoundedBorder(Color c, int g) { color = c; gap = g; } @Override public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) { Graphics2D g2d = (Graphics2D) g.create(); g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY); g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE); g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE); g2d.setColor(color); g2d.draw(new RoundRectangle2D.Double(x + 1, y + 1, width - 2, height - 2, gap, gap)); g2d.dispose(); } @Override public Insets getBorderInsets(Component c) { return (getBorderInsets(c, new Insets(gap, gap, gap, gap))); } @Override public Insets getBorderInsets(Component c, Insets insets) { insets.left = insets.top = insets.right = insets.bottom = gap / 2; return insets; } @Override public boolean isBorderOpaque() { return false; } }
Je vous remercie pour vos conseils !Code:
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 package com.view.drawings; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.JComponent; public class DessinDes extends JComponent { private Color pointsCoul = null; private int nb = 1; private int pos_y = 0; private int pos_x = 0; public DessinDes(int _nb, Color _pointsCoul, int _pos_x, int _pos_y) { super(); this.setPreferredSize(new Dimension(100, 100)); this.pointsCoul = _pointsCoul; this.nb = _nb; this.pos_y = _pos_y; this.pos_x = _pos_x; } @Override public void paintComponent(Graphics g) { // passage du contexte graphique super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g.setColor(Color.RED); g.fillRect(0, 0, 80, 80); g.setColor(Color.GREEN); g.drawRect(0, 0, 80, 80); // choix de la couleur pour les points g.setColor(pointsCoul); switch(nb) { case 1: //Dé 1 g.fillOval(33, 33, 15, 15); break; case 2: //Dé 2 g.fillOval(55, 10, 15, 15); g.fillOval(10, 55, 15, 15); break; case 3: //Dé 3 g.fillOval(55, 10, 15, 15); g.fillOval(33, 33, 15, 15); g.fillOval(10, 55, 15, 15); break; case 4: //Dé 4 g.fillOval(10, 10, 15, 15); g.fillOval(55, 10, 15, 15); g.fillOval(10, 55, 15, 15); g.fillOval(55, 55, 15, 15); break; case 5: g.fillOval(10, 10, 15, 15); g.fillOval(55, 10, 15, 15); g.fillOval(33, 33, 15, 15); g.fillOval(10, 55, 15, 15); g.fillOval(55, 55, 15, 15); break; case 6: //Dé 6 g.fillOval(10, 10, 15, 15); g.fillOval(55, 10, 15, 15); g.fillOval(10, 33, 15, 15); g.fillOval(55, 33, 15, 15); g.fillOval(10, 55, 15, 15); g.fillOval(55, 55, 15, 15); break; } this.setLocation(pos_x, pos_y); } }