Hello tout le monde...
Je cherchais à souligner le contenu d'un JLabel mais apparemment on peut pas... je me trompe ?
(Je ne veux pas d'html !)
Hello tout le monde...
Je cherchais à souligner le contenu d'un JLabel mais apparemment on peut pas... je me trompe ?
(Je ne veux pas d'html !)
Bonjour,
C'est bien dommage parce que c'est vraiment la solution la plus simple. Vu que la classe Font ne permet pas de souligner, il ne te reste plus qu'à surcharger paintComponent et dessiner une ligne au bon endroit, mais ça va être chaud.Envoyé par soad
Ouais ca va etre très chaud...Envoyé par xavlours
Je crois que je vais utiliser JXHyperlink de swingx
Merci
Envoyé par soadJ'ai du mal à suivre, là ... Enfin le principal c'est que ça marche.Envoyé par soad
fais en 3 minute (maximum)
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 import javax.swing.JFrame; import javax.swing.JLabel; import java.awt.geom.Rectangle2D; import java.awt.FlowLayout; import java.awt.Color; import java.awt.Graphics; import java.awt.Font; /** * Created by IntelliJ IDEA. * User: bebe * Date: 06-sept.-2006 * Time: 19:29:23 * To change this template use File | Settings | File Templates. */ public class UnderlinedLabel extends JLabel { private Color lineColor; public UnderlinedLabel(String text, Color lineColor) { super(text); this.lineColor = lineColor; } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); //To change body of overridden methods use File | Settings | File Templates. Rectangle2D textBounds = getFontMetrics(getFont()).getStringBounds(getText(), g); int y = (int) (getHeight() / 2 + textBounds.getHeight() / 2); int w = (int) (textBounds.getWidth()); g.setColor(lineColor); g.drawLine(0, y, w, y); } public Color getLineColor() { return lineColor; } public void setLineColor(Color lineColor) { this.lineColor = lineColor; } /** * * @param args */ public static void main(String[] args) { JFrame myFrame = new JFrame("Underlined myLabel"); myFrame.setLayout(new FlowLayout(FlowLayout.LEFT)); UnderlinedLabel myLabel = new UnderlinedLabel("Hello World!", Color.RED); Font labelFont = myLabel.getFont(); myLabel.setFont(new Font(labelFont.getFamily(), labelFont.getStyle(), labelFont.getSize() + 10)); myFrame.add(myLabel); myFrame.pack(); myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); myFrame.setLocationRelativeTo(null); myFrame.setVisible(true); } }
Pkoi JXHyperlink utilise de l'html ?Envoyé par xavlours
Sauf que si on centre le texte ca ne marche pas !!!Envoyé par bbclone
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 import javax.swing.JFrame; import javax.swing.JLabel; import java.awt.geom.Rectangle2D; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Graphics; import java.awt.Font; /** * Created by IntelliJ IDEA. * User: bebe * Date: 06-sept.-2006 * Time: 19:29:23 * To change this template use File | Settings | File Templates. */ public class UnderlinedLabel extends JLabel { private Color lineColor; public UnderlinedLabel(String text, Color lineColor) { super(text, JLabel.CENTER); this.lineColor = lineColor; } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); //To change body of overridden methods use File | Settings | File Templates. Rectangle2D textBounds = getFontMetrics(getFont()).getStringBounds(getText(), g); int y = (int) (getHeight() / 2 + textBounds.getHeight() / 2); int w = (int) (textBounds.getWidth()); g.setColor(lineColor); g.drawLine(0, y, w, y); } public Color getLineColor() { return lineColor; } public void setLineColor(Color lineColor) { this.lineColor = lineColor; } /** * * @param args */ public static void main(String[] args) { JFrame myFrame = new JFrame("Underlined myLabel"); myFrame.setLayout(new BorderLayout()); UnderlinedLabel myLabel = new UnderlinedLabel("Hello World!", Color.RED); Font labelFont = myLabel.getFont(); myLabel.setFont(new Font(labelFont.getFamily(), labelFont.getStyle(), labelFont.getSize() + 10)); myFrame.add(myLabel, BorderLayout.CENTER); myFrame.pack(); myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); myFrame.setLocationRelativeTo(null); myFrame.setVisible(true); } }
C'est bon j'ai corrigé !
voilà pour ceux que ca intéresse :
Merci
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 import javax.swing.JFrame; import javax.swing.JLabel; import java.awt.geom.Rectangle2D; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Graphics; import java.awt.Font; /** * Created by IntelliJ IDEA. * User: bebe * Date: 06-sept.-2006 * Time: 19:29:23 * To change this template use File | Settings | File Templates. */ public class UnderlinedLabel extends JLabel { private Color lineColor; public UnderlinedLabel(String text, Color lineColor) { super(text); this.lineColor = lineColor; } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); //To change body of overridden methods use File | Settings | File Templates. Rectangle2D textBounds = getFontMetrics(getFont()).getStringBounds(getText(), g); // Alignement verticale selon la position du texte int y = (int) textBounds.getHeight(); if(getVerticalAlignment() == JLabel.BOTTOM) y = getHeight()-2; else if(getVerticalAlignment() == JLabel.CENTER) y = (int) (getHeight() / 2 + textBounds.getHeight() / 2); // Largueur du texte int w = (int) (textBounds.getWidth()); // Alignement horizontale selon la position du texte int x = 0; if(getHorizontalAlignment() == JLabel.CENTER) x = getWidth()/2 - w/2; else if(getHorizontalAlignment() == JLabel.RIGHT) x = getWidth() - w; g.setColor(lineColor); g.drawLine(x, y, x+w, y); } public Color getLineColor() { return lineColor; } public void setLineColor(Color lineColor) { this.lineColor = lineColor; } /** * * @param args */ public static void main(String[] args) { JFrame myFrame = new JFrame("Underlined myLabel"); myFrame.setLayout(new BorderLayout()); UnderlinedLabel myLabel = new UnderlinedLabel("Hello World!", Color.RED); Font labelFont = myLabel.getFont(); myLabel.setFont(new Font(labelFont.getFamily(), labelFont.getStyle(), labelFont.getSize() + 10)); myFrame.add(myLabel, BorderLayout.CENTER); myFrame.pack(); myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); myFrame.setLocationRelativeTo(null); myFrame.setVisible(true); } }
l'alignment c'est pas le seul probleme.
ca c'est un peu mieux ;-)
j'avai oublier les code dans le message avant. j'ai plus l'habitude
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 import javax.swing.plaf.basic.BasicLabelUI; import javax.swing.*; import java.awt.*; /** * Created by IntelliJ IDEA. * User: bebe * Date: 06-sept.-2006 * Time: 21:28:17 * To change this template use File | Settings | File Templates. */ public class UnderlineLabelUI extends BasicLabelUI { static { labelUI = new UnderlineLabelUI(Color.BLACK); } private static Rectangle paintIconR = new Rectangle(); private static Rectangle paintTextR = new Rectangle(); private static Rectangle paintViewR = new Rectangle(); private static Insets paintViewInsets = new Insets(0, 0, 0, 0); private Color linecolor; public UnderlineLabelUI(Color linecolor) { this.linecolor = linecolor; } public void paint(Graphics g, JComponent c) { JLabel label = (JLabel) c; String text = label.getText(); Icon icon = (label.isEnabled()) ? label.getIcon() : label.getDisabledIcon(); if ((icon == null) && (text == null)) { return; } FontMetrics fm = g.getFontMetrics(); paintViewInsets = c.getInsets(paintViewInsets); paintViewR.x = paintViewInsets.left; paintViewR.y = paintViewInsets.top; paintViewR.height = c.getHeight() - (paintViewInsets.top + paintViewInsets.bottom); paintViewR.width = c.getWidth() - (paintViewInsets.left + paintViewInsets.right); paintIconR.x = paintIconR.y = paintIconR.width = paintIconR.height = 0; paintTextR.x = paintTextR.y = paintTextR.width = paintTextR.height = 0; String clippedText = layoutCL(label, fm, text, icon, paintViewR, paintIconR, paintTextR); Graphics2D g2d = (Graphics2D) g; if (icon != null) { icon.paintIcon(c, g, paintIconR.x, paintIconR.y); } if (text != null) { int textX = paintTextR.x; int textY = paintTextR.y + fm.getMaxAscent(); if (label.isEnabled()) { paintEnabledText(label, g2d, clippedText, textX, textY - 1); textY += fm.getMaxDescent() - 1; g2d.setColor(linecolor); // or label.getForeground ?? g2d.drawLine(textX, textY, textX + paintTextR.width, textY); } else { paintDisabledText(label, g2d, clippedText, textX, textY - 1); textY += fm.getMaxDescent() - 1; g2d.setColor(linecolor.darker()); // or label.getForeground ?? g2d.drawLine(textX, textY, textX + paintTextR.width, textY); } } } /** * @param args */ public static void main(String[] args) { JFrame myFrame = new JFrame("Underlined label using UI"); myFrame.setLayout(new GridBagLayout()); JLabel myLabels[] = {new JLabel("Hello World (alignment = center)!", SwingConstants.CENTER), new JLabel("Hello World (alignment = left)!", SwingConstants.LEFT), new JLabel("Hello World (alignment = right)!", SwingConstants.RIGHT), new JLabel("Hello World (alignment = leading)!", SwingConstants.LEADING), new JLabel("Hello World (alignment = trailing)!", SwingConstants.TRAILING)}; Color[] colors = new Color[]{Color.BLACK, Color.BLUE, Color.RED, Color.MAGENTA, Color.PINK}; Font labelFont = myLabels[0].getFont(); GridBagConstraints gbc = new GridBagConstraints(); gbc.insets = new Insets(10, 5, 5, 5); gbc.gridx = 0; gbc.gridy = GridBagConstraints.RELATIVE; gbc.fill = GridBagConstraints.HORIZONTAL; for (int i = 0; i < 5; i++) { myLabels[i].setUI(new UnderlineLabelUI(colors[i])); myLabels[i].setFont(new Font(labelFont.getFamily(), labelFont.getStyle(), labelFont.getSize() + i * i)); if ((i % 2) == 0) { myLabels[i].setEnabled(false); } myFrame.add(myLabels[i], gbc); } myFrame.pack(); myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); myFrame.setLocationRelativeTo(null); myFrame.setVisible(true); } }
J'avais pas besoin d'image mais on sait jamais pour plus tardEnvoyé par bbclone
Merci...
maintnant tu vois une solution c'etais pas tres chaud.
si ?
Mouais... en java2d j'suis pas très bon !Envoyé par bbclone
Mais c'est vrai, c'est tjs plus facile quand on a la réponse
Vous avez un bloqueur de publicités installé.
Le Club Developpez.com n'affiche que des publicités IT, discrètes et non intrusives.
Afin que nous puissions continuer à vous fournir gratuitement du contenu de qualité, merci de nous soutenir en désactivant votre bloqueur de publicités sur Developpez.com.
Partager