Salut à tous ,
J'essaye actuellement de placer de simple JPanel dans une JFrame j. En faite celle ci est composé en trois parties.
- Un JPanel header : Qui se situe en haut d'un JFrame avec une hauteur de 80 px et sur toute la largeur de la jFrame.
- Un JPanel menu : Qui se situe sous le header à gauche avec une largeur fixe de 100px et sur toute la hauteur de la JFrame-80 (du header).
- un JPanel body : Qui se situe sous le header et prend le reste de la place disponible à droite du menu
Le problème est que ça ne s'affiche pas correctement,l'affichage se fait au centre de la jframe, ne s'ajuste pas quand je redimensionne la fenêtre, bref tout va mal et je ne comprend pas le pourquoi du comment donc si une âme charitable pouvait m'éclairer
Voici le code que j'ai réalisé :
MainFrame:
header :
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 import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.event.ComponentEvent; import java.awt.event.ComponentListener; import javax.swing.JFrame; import javax.swing.JLabel; public class MainFrame extends JFrame implements ComponentListener{ private Header header; private RoundedPanel body; private MenuPanel left; public MainFrame(){ setTitle("test"); this.setSize(800,600); this.setPreferredSize(this.getSize()); this.setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); header = new Header(getWidth()); c.fill = GridBagConstraints.BOTH; c.gridwidth = 2; c.gridheight= 1; c.gridx = 0; c.gridy = 0; add(header, c); left = new MenuPanel(getHeight()); c.fill = GridBagConstraints.BOTH; c.gridwidth = 1; c.gridheight= 1; c.gridx = 0; c.gridy = 1; add(left,c); body = new RoundedPanel(getWidth(),getHeight()); JLabel corps = new JLabel("corps"); body.add(corps); c.fill = GridBagConstraints.BOTH; c.gridwidth = 1; c.gridheight= 1; c.gridx = 1; c.gridy = 1; add(body,c); pack(); setVisible(true); setLocationRelativeTo(null); addComponentListener(this); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args){ MainFrame fen = new MainFrame(); fen.setVisible(true); } @Override public void componentHidden(ComponentEvent arg0) { } @Override public void componentMoved(ComponentEvent arg0) { } @Override public void componentResized(ComponentEvent arg0) { header.setWidth(this.getWidth()); left.setHeight(this.getHeight()); body.setWidthHeight(this.getWidth(),this.getHeight()); } @Override public void componentShown(ComponentEvent arg0) { } }
Menu:
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.Color; import javax.swing.JPanel; public class Header extends JPanel { private int width; public Header(int width){ super(); this.width = width; this.setSize(width,80); setPreferredSize(getSize()); setBackground(Color.GREEN); } public void setWidth(int width) { this.width = width; setSize(width, 80); } }
Corps:
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 import java.awt.Color; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JPanel; public class MenuPanel extends JPanel { private JButton boutton1,boutton2,boutton3; private int height; public MenuPanel(int height){ super(); this.height = height; this.setSize(100,height); this.setPreferredSize(getSize()); this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS)); setBackground(Color.red); boutton1 = new JButton("boutton1"); boutton2 = new JButton("boutton2"); boutton3 = new JButton("boutton3"); this.add(boutton1); this.add(boutton2); this.add(boutton3); } public void setHeight(int height) { this.height = height; this.setSize(100,height); } }
Merci d'avance!
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 import java.awt.BasicStroke; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.util.prefs.BackingStoreException; import javax.swing.JPanel; public class RoundedPanel extends JPanel { /** Stroke size. it is recommended to set it to 1 for better view */ protected int strokeSize = 1; /** Color of shadow */ protected Color shadowColor = Color.black; /** Sets if it drops shadow */ protected boolean shady = false; /** Sets if it has an High Quality view */ protected boolean highQuality = true; /** Double values for Horizontal and Vertical radius of corner arcs */ protected Dimension arcs = new Dimension(10, 10); /** Distance between shadow border and opaque panel border */ protected int shadowGap = 5; /** The offset of shadow. */ protected int shadowOffset = 0; /** The transparency value of shadow. ( 0 - 255) */ protected int shadowAlpha = 150; private int width,height; public RoundedPanel(int width, int height) { super(); this.width = width; this.height = height; this.setSize(width-100,height-80); this.setPreferredSize(getSize()); setBackground(Color.WHITE); setOpaque(false); } public void setWidthHeight(int width, int height) { this.width = width; this.height = height; setSize(width, height); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); int width = getWidth(); int height = getHeight(); int shadowGap = this.shadowGap; Color shadowColorA = new Color(shadowColor.getRed(), shadowColor.getGreen(), shadowColor.getBlue(), shadowAlpha); Graphics2D graphics = (Graphics2D) g; //Sets antialiasing if HQ. if (highQuality) { graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); } //Draws shadow borders if any. if (shady) { graphics.setColor(shadowColorA); graphics.fillRoundRect( shadowOffset,// X position shadowOffset,// Y position width - strokeSize - shadowOffset, // width height - strokeSize - shadowOffset, // height arcs.width, arcs.height);// arc Dimension } else { shadowGap = 1; } //Draws the rounded opaque panel with borders. graphics.setColor(getBackground()); graphics.fillRoundRect(0, 0, width - shadowGap, height - shadowGap, arcs.width, arcs.height); graphics.setColor(getForeground()); graphics.setStroke(new BasicStroke(strokeSize)); graphics.drawRoundRect(0, 0, width - shadowGap, height - shadowGap, arcs.width, arcs.height); //Sets strokes to default, is better. graphics.setStroke(new BasicStroke()); } }
Partager