Bonjour à tous,
Voilà, j'ai cherché à créer un bouton rond pour une application et j'ai trouvé cette classe (sur ce forum) qui me convient très bien
Malheureusement, lorsque je l'utilise et que j'aimerai lui attribué une action, j'obtiens toujours une erreur que je ne comprends vraiment pas. Et l'erreur ne vient que si j'ai ma ligne b1.setAction(new MonAction("rond 1")); (qui est quand meme capitale...)
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 package boutonRond; import java.awt.Color; import java.awt.Component; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Insets; import java.awt.RenderingHints; import java.awt.Shape; import java.awt.geom.Ellipse2D; import javax.swing.Action; import javax.swing.BorderFactory; import javax.swing.DefaultButtonModel; import javax.swing.Icon; import javax.swing.JButton; public class RoundButton extends JButton { public RoundButton() { this(null, null); } public RoundButton(Icon icon) { this(null, icon); } public RoundButton(String text) { this(text, null); } public RoundButton(Action a) { this(); setAction(a); } public RoundButton(String text, Icon icon) { setModel(new DefaultButtonModel()); init(text, icon); if(icon==null) { return; } setBorder(BorderFactory.createEmptyBorder(1,1,1,1)); setBackground(Color.BLACK); setContentAreaFilled(false); setFocusPainted(false); //setVerticalAlignment(SwingConstants.TOP); setAlignmentY(Component.TOP_ALIGNMENT); initShape(); } @Override public Dimension getPreferredSize() { Icon icon = getIcon(); Insets i = getInsets(); int iw = Math.max(icon.getIconWidth(), icon.getIconHeight()); return new Dimension(iw+i.right+i.left, iw+i.top+i.bottom); } protected Shape shape, base; protected void initShape() { if(!getBounds().equals(base)) { Dimension s = getPreferredSize(); base = getBounds(); shape = new Ellipse2D.Float(0, 0, s.width-1, s.height-1); } } @Override protected void paintBorder(Graphics g) { initShape(); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setColor(getBackground()); //g2.setStroke(new BasicStroke(1.0f)); g2.draw(shape); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF); } @Override public boolean contains(int x, int y) { initShape(); return shape.contains(x, y); } }
L'appel :
L'action :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8 ....... RoundButton b1 = new RoundButton(new ImageIcon("D:/Temp/tst_boutons/001.png")); b1.setPressedIcon(new ImageIcon("D:/Temp/tst_boutons/001d.png")); b1.setRolloverIcon(new ImageIcon("D:/Temp/tst_boutons/001g.png")); b1.setAction(new MonAction("rond 1")); panel.add(b1); ......
Et l'erreur :
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 public class MonAction extends AbstractAction { private String txt; public MonAction(String txt){ super(); this.txt = txt; } @Override public void actionPerformed(ActionEvent e) { System.out.println(txt); } }
C'est quoi le problème? Accès à mon bouton qui serait null? Pourtant instancié... je ne comprends pas! Merci d'avance si vous pouvez m'éclairer.
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 java.lang.NullPointerException at boutonRond.RoundButton.getPreferredSize(RoundButton.java:50) at java.awt.FlowLayout.layoutContainer(Unknown Source) at java.awt.Container.layout(Unknown Source) at java.awt.Container.doLayout(Unknown Source) at java.awt.Container.validateTree(Unknown Source) at java.awt.Container.validateTree(Unknown Source) at java.awt.Container.validateTree(Unknown Source) at java.awt.Container.validateTree(Unknown Source) at java.awt.Container.validate(Unknown Source) at java.awt.Container.validateUnconditionally(Unknown Source) at java.awt.Window.show(Unknown Source) at java.awt.Component.show(Unknown Source) at java.awt.Component.setVisible(Unknown Source) at java.awt.Window.setVisible(Unknown Source) at test.MainTest.main(MainTest.java:19) Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at boutonRond.RoundButton.getPreferredSize(RoundButton.java:50) at java.awt.FlowLayout.layoutContainer(Unknown Source) at java.awt.Container.layout(Unknown Source) at java.awt.Container.doLayout(Unknown Source) at java.awt.Container.validateTree(Unknown Source) at java.awt.Container.validateTree(Unknown Source) at java.awt.Container.validateTree(Unknown Source) at java.awt.Container.validateTree(Unknown Source) at java.awt.Container.validate(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$000(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)
Partager