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
| public class MyToolBar extends JPanel implements ActionListener {
private final int NB_BOUTONS = 9;
private ImageIcon [] tabIcons = new ImageIcon[NB_BOUTONS];
private JButton [] tabBoutons = new JButton[NB_BOUTONS];
private String [] tabAlt = new String[NB_BOUTONS];
JPopupMenu testIconDeroul;
public MyToolBar () {
this.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
// POUR LA COULEUR DE FOND UNIE, CA MARCHE, MAIS COMMENT FAIRE POUR UN DÉGRADÉ ???
this.setBackground(new Color(255, 255, 255));
// Création des icones dont on va se servir pour les boutons de la barre d'outils
tabIcons[0] = new ImageIcon("icons/kcontrol.png");
tabIcons[1] = new ImageIcon("icons/filesave.png");
tabIcons[2] = new ImageIcon("icons/xmag.png");
tabIcons[3] = new ImageIcon("icons/kjobviewer.png");
tabIcons[4] = new ImageIcon("icons/undo.png");
tabIcons[5] = new ImageIcon("icons/redo.png");
tabIcons[6] = new ImageIcon("icons/editdelete.png");
tabIcons[7] = new ImageIcon("icons/editcut.png");
tabIcons[8] = new ImageIcon("icons/editpaste.png");
// Définition des messages alternatifs correspondants
tabAlt[0] = "Boite à outils";
tabAlt[1] = "Enregistrer";
tabAlt[2] = "Zoom";
tabAlt[3] = "Imprimer";
tabAlt[4] = "Annuler dernière action";
tabAlt[5] = "Reproduire dernière action";
tabAlt[6] = "Supprimer";
tabAlt[7] = "Couper";
tabAlt[8] = "Coller";
// Création de tous les boutons et ajout à la barre d'outils
for (int i=0; i<NB_BOUTONS; i++) {
// Création des boutons
tabBoutons[i] = new JButton(tabIcons[i]);
// Les boutons sont de taille minimum
tabBoutons[i].setMargin(new Insets(0,0,0,0));
// Les boutons ont un fond transparents
tabBoutons[i].setContentAreaFilled(false);
// Avec les messages alternatifs correspondants
tabBoutons[i].setToolTipText(tabAlt[i]);
// Ajout à la barre d'outils
this.add(tabBoutons[i]);
// Ajout des ActionListeners
tabBoutons[i].addActionListener(this);
}
// TEST ICONE DEROULANT POPUP (EXEMPLE SIMPLE)
testIconDeroul = new JPopupMenu();
testIconDeroul.add(new JMenuItem("sous menu 1"));
testIconDeroul.add(new JMenuItem("sous menu 2"));
}
// ACTION
public void actionPerformed (ActionEvent e) {
// On récupère le bouton qui a été cliqué
Component boutonClicked = (Component)e.getSource();
// Coordonées ou s'affichera le menu déroulant
int posXpopup = 0;
int posYpopup = boutonClicked.getY() + boutonClicked.getHeight();
// Affichage du popup exemple
testIconDeroul.show(boutonClicked, posXpopup, posYpopup);
// ACTION Bouton 0
if (e.getSource() == tabBoutons[0]) {
System.out.println(tabAlt[0]);
}
// ACTION Bouton 1
if (e.getSource() == tabBoutons[1]) {
System.out.println(tabAlt[1]);
}
// ACTION Bouton 2
if (e.getSource() == tabBoutons[2]) {
System.out.println(tabAlt[2]);
}
// ACTION Bouton 3
if (e.getSource() == tabBoutons[3]) {
System.out.println(tabAlt[3]);
}
// ACTION Bouton 4
if (e.getSource() == tabBoutons[4]) {
System.out.println(tabAlt[4]);
}
// ACTION Bouton 5
if (e.getSource() == tabBoutons[5]) {
System.out.println(tabAlt[5]);
}
// ACTION Bouton 6
if (e.getSource() == tabBoutons[6]) {
System.out.println(tabAlt[6]);
}
// ACTION Bouton 7
if (e.getSource() == tabBoutons[7]) {
System.out.println(tabAlt[7]);
}
// ACTION Bouton 8
if (e.getSource() == tabBoutons[8]) {
System.out.println(tabAlt[8]);
}
}
} |
Partager