IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

AWT/Swing Java Discussion :

ajouter menu sur JToolBar


Sujet :

AWT/Swing Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé
    Inscrit en
    Avril 2007
    Messages
    679
    Détails du profil
    Informations forums :
    Inscription : Avril 2007
    Messages : 679
    Par défaut ajouter menu sur JToolBar
    bonsoir tout le monde

    je vous demande comment ajouter jmenuitem sur JToolBar

    par exemple si je click sur le bouton de JToolBar alors un menu s'ouvre
    voilla le code

    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
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
     
    import javax.swing.SwingUtilities;
    import java.awt.BorderLayout;
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    import java.awt.Dimension;
    import java.awt.Rectangle;
    import java.awt.GridBagLayout;
    import javax.swing.JToolBar;
    import java.awt.Color;
    import javax.swing.JButton;
    import javax.swing.ImageIcon;
    import java.awt.Font;
    import javax.swing.JMenu;
    import javax.swing.JMenuItem;
    import javax.swing.JPopupMenu;
     
    public class menu extends JFrame {
     
    	private static final long serialVersionUID = 1L;
     
    	private JPanel jContentPane = null;
     
    	private JPanel jPanel = null;
     
    	private JToolBar jJToolBarBar = null;
     
    	private JButton jButton = null;
     
    	private JButton jButton1 = null;
     
    	private JButton jButton11 = null;
     
     
    	private JPanel getJPanel() {
    		if (jPanel == null) {
    			jPanel = new JPanel();
    			jPanel.setLayout(null);
    			jPanel.setBounds(new Rectangle(10, 15, 565, 216));
    			jPanel.add(getJJToolBarBar(), null);
    		}
    		return jPanel;
    	}
     
    	private JToolBar getJJToolBarBar() {
    		if (jJToolBarBar == null) {
    			jJToolBarBar = new JToolBar();
    			jJToolBarBar.setBounds(new Rectangle(40, 51, 424, 83));
    			jJToolBarBar.setBackground(new Color(0, 138, 255));
    			jJToolBarBar.setFloatable(false);
    			jJToolBarBar.setOrientation(JToolBar.HORIZONTAL);
    			jJToolBarBar.add(getJButton());
    			jJToolBarBar.add(getJButton11());
    			jJToolBarBar.add(getJButton1());
    		}
    		return jJToolBarBar;
    	}
     
     
    	private JButton getJButton() {
    		if (jButton == null) {
    			jButton = new JButton(new ImageIcon("src/image/NOUVEAU.png"));
    			jButton.setFont(new Font("Dialog", Font.BOLD, 14));
    			jButton.setText("   ajouter      ");
    		}
    		return jButton;
    	}
     
     
    	private JButton getJButton1() {
    		if (jButton1 == null) {
    			jButton1 = new JButton();
    			jButton1.setFont(new Font("Dialog", Font.BOLD, 12));
    			jButton1.setText("  supprimer       ");
    			jButton1.setIcon(new ImageIcon(getClass().getResource("/SRC/IMAGE/NOUVEAU.PNG")));
    		}
    		return jButton1;
    	}
     
     
    	private JButton getJButton11() {
    		if (jButton11 == null) {
    			jButton11 = new JButton();
    			jButton11.setFont(new Font("Dialog", Font.BOLD, 14));
    			jButton11.setText("  modifier     ");
    			jButton11.setIcon(new ImageIcon(getClass().getResource("/SRC/IMAGE/NOUVEAU.PNG")));
    		}
    		return jButton11;
    	}
     
     
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		SwingUtilities.invokeLater(new Runnable() {
    			public void run() {
    				menu thisClass = new menu();
    				thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    				thisClass.setVisible(true);
    			}
    		});
    	}
     
    	public menu() {
    		super();
    		initialize();
    	}
     
    	private void initialize() {
    		this.setSize(605, 277);
    		this.setContentPane(getJContentPane());
    		this.setTitle("JFrame");
    	}
     
     
    	private JPanel getJContentPane() {
    		if (jContentPane == null) {
    			jContentPane = new JPanel();
    			jContentPane.setLayout(null);
    			jContentPane.add(getJPanel(), null);
    		}
    		return jContentPane;
    	}
     
    }
    merci

  2. #2
    Membre expérimenté Avatar de uhrand
    Profil pro
    Développeur informatique
    Inscrit en
    Octobre 2009
    Messages
    203
    Détails du profil
    Informations personnelles :
    Localisation : Luxembourg

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Octobre 2009
    Messages : 203
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    final JPopupMenu jPopupMenu = new JPopupMenu();
    jPopupMenu.add(new JMenuItem("1"));
    jPopupMenu.add(new JMenuItem("2"));
    jButton.addActionListener(new ActionListener() {
     
        public void actionPerformed(ActionEvent e) {
            jPopupMenu.show(jButton, 0, 0);
        }
    });

  3. #3
    Membre éclairé
    Inscrit en
    Avril 2007
    Messages
    679
    Détails du profil
    Informations forums :
    Inscription : Avril 2007
    Messages : 679
    Par défaut
    Citation Envoyé par uhrand Voir le message
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    final JPopupMenu jPopupMenu = new JPopupMenu();
    jPopupMenu.add(new JMenuItem("1"));
    jPopupMenu.add(new JMenuItem("2"));
    jButton.addActionListener(new ActionListener() {
     
        public void actionPerformed(ActionEvent e) {
            jPopupMenu.show(jButton, 0, 0);
        }
    });
    mais ça fonctionne pas si je click sur le bouton jBouton

    voila la nouvelle modification
    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
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
     
    import javax.swing.SwingUtilities;
    import java.awt.BorderLayout;
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    import java.awt.Dimension;
    import java.awt.Rectangle;
    import java.awt.GridBagLayout;
    import javax.swing.JToolBar;
    import java.awt.Color;
    import javax.swing.JButton;
    import javax.swing.ImageIcon;
    import java.awt.Font;
    import javax.swing.JMenu;
    import javax.swing.JMenuItem;
    import javax.swing.JPopupMenu;
     
    public class menu extends JFrame {
     
    	private static final long serialVersionUID = 1L;
     
    	private JPanel jContentPane = null;
     
    	private JPanel jPanel = null;
     
    	private JToolBar jJToolBarBar = null;
     
    	private JButton jButton = null;
     
    	private JButton jButton1 = null;
     
    	private JButton jButton11 = null;
     
    	private JPopupMenu jPopupMenu = null;
     
    	private JPopupMenu jPopupMenu1 = null;
     
    	private JMenuItem jMenuItem = null;
     
    	private JMenuItem jMenuItem1 = null;
     
    	/**
             * This method initializes jPanel       
             *      
             * @return javax.swing.JPanel   
             */
    	private JPanel getJPanel() {
    		if (jPanel == null) {
    			jPanel = new JPanel();
    			jPanel.setLayout(null);
    			jPanel.setBounds(new Rectangle(10, 15, 565, 216));
    			jPanel.add(getJJToolBarBar(), null);
    		}
    		return jPanel;
    	}
     
    	/**
             * This method initializes jJToolBarBar 
             *      
             * @return javax.swing.JToolBar 
             */
    	private JToolBar getJJToolBarBar() {
    		if (jJToolBarBar == null) {
    			jJToolBarBar = new JToolBar();
     
     
    			jJToolBarBar.setBounds(new Rectangle(40, 51, 424, 83));
    			jJToolBarBar.setBackground(new Color(0, 138, 255));
    			jJToolBarBar.setFloatable(false);
    			jJToolBarBar.setOrientation(JToolBar.HORIZONTAL);
    			jJToolBarBar.add(getJButton());
    			jJToolBarBar.add(getJButton11());
    			jJToolBarBar.add(getJButton1());
    			jJToolBarBar.add(getJPopupMenu());
    		}
    		return jJToolBarBar;
    	}
     
    	/**
             * This method initializes jButton      
             *      
             * @return javax.swing.JButton  
             */
    	private JButton getJButton() {
    		if (jButton == null) {
    			jButton = new JButton(new ImageIcon("src/image/NOUVEAU.png"));
    			jButton.setFont(new Font("Dialog", Font.BOLD, 14));
    			jButton.setText("   ajouter      ");
    			jButton.addActionListener(new java.awt.event.ActionListener() {
    				public void actionPerformed(java.awt.event.ActionEvent e) {
    					jPopupMenu.show(jButton, 0, 0);
     
    					System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
    				}
    			});
    		}
    		return jButton;
    	}
     
    	/**
             * This method initializes jButton1     
             *      
             * @return javax.swing.JButton  
             */
    	private JButton getJButton1() {
    		if (jButton1 == null) {
    			jButton1 = new JButton();
    			jButton1.setFont(new Font("Dialog", Font.BOLD, 12));
    			jButton1.setText("  supprimer       ");
    			jButton1.setIcon(new ImageIcon(getClass().getResource("/SRC/IMAGE/NOUVEAU.PNG")));
    		}
    		return jButton1;
    	}
     
    	/**
             * This method initializes jButton11    
             *      
             * @return javax.swing.JButton  
             */
    	private JButton getJButton11() {
    		if (jButton11 == null) {
    			jButton11 = new JButton();
    			jButton11.setFont(new Font("Dialog", Font.BOLD, 14));
    			jButton11.setText("  modifier     ");
    			jButton11.setIcon(new ImageIcon(getClass().getResource("/SRC/IMAGE/NOUVEAU.PNG")));
    		}
    		return jButton11;
    	}
     
    	/**
             * This method initializes jPopupMenu   
             *      
             * @return javax.swing.JPopupMenu       
             */
    	private JPopupMenu getJPopupMenu() {
    		if (jPopupMenu == null) {
    			jPopupMenu = new JPopupMenu();
    		}
    		return jPopupMenu;
    	}
     
    	/**
             * This method initializes jPopupMenu1  
             *      
             * @return javax.swing.JPopupMenu       
             */
    	private JPopupMenu getJPopupMenu1() {
    		if (jPopupMenu1 == null) {
    			jPopupMenu1 = new JPopupMenu();
    			jPopupMenu1.add(getJMenuItem());
    			jPopupMenu1.add(getJMenuItem1());
    			jPopupMenu.add(new JMenuItem("1"));
    			jPopupMenu.add(new JMenuItem("2"));
     
    		}
    		return jPopupMenu1;
    	}
     
    	/**
             * This method initializes jMenuItem    
             *      
             * @return javax.swing.JMenuItem        
             */
    	private JMenuItem getJMenuItem() {
    		if (jMenuItem == null) {
    			jMenuItem = new JMenuItem();
    			jMenuItem.setText("salut");
    		}
    		return jMenuItem;
    	}
     
    	/**
             * This method initializes jMenuItem1   
             *      
             * @return javax.swing.JMenuItem        
             */
    	private JMenuItem getJMenuItem1() {
    		if (jMenuItem1 == null) {
    			jMenuItem1 = new JMenuItem();
    			jMenuItem1.setText("merci");
    		}
    		return jMenuItem1;
    	}
     
    	/**
             * @param args
             */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		SwingUtilities.invokeLater(new Runnable() {
    			public void run() {
    				menu thisClass = new menu();
    				thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    				thisClass.setVisible(true);
    			}
    		});
    	}
     
    	/**
             * This is the default constructor
             */
    	public menu() {
    		super();
    		initialize();
    	}
     
    	/**
             * This method initializes this
             * 
             * @return void
             */
    	private void initialize() {
    		this.setSize(605, 277);
    		this.setContentPane(getJContentPane());
    		this.setTitle("JFrame");
    	}
     
    	/**
             * This method initializes jContentPane
             * 
             * @return javax.swing.JPanel
             */
    	private JPanel getJContentPane() {
    		if (jContentPane == null) {
    			jContentPane = new JPanel();
    			jContentPane.setLayout(null);
    			jContentPane.add(getJPanel(), null);
    		}
    		return jContentPane;
    	}
     
    }  //  @jve:decl-index=0:visual-constraint="10,10"

  4. #4
    Membre expérimenté Avatar de uhrand
    Profil pro
    Développeur informatique
    Inscrit en
    Octobre 2009
    Messages
    203
    Détails du profil
    Informations personnelles :
    Localisation : Luxembourg

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Octobre 2009
    Messages : 203
    Par défaut
    Citation Envoyé par minooo Voir le message
    mais ça fonctionne pas si je click sur le bouton jBouton
    pas étonnant avec ta façon de programmer: le menu est rempli dans la méthode getJPopupMenu1 qui n'est jamais appelée. D'ailleur, ça fonctionne si tu insère mon code inchangé .

  5. #5
    Membre éclairé
    Inscrit en
    Avril 2007
    Messages
    679
    Détails du profil
    Informations forums :
    Inscription : Avril 2007
    Messages : 679
    Par défaut
    Citation Envoyé par uhrand Voir le message
    pas étonnant avec ta façon de programmer: le menu est rempli dans la méthode getJPopupMenu1 qui n'est jamais appelée. D'ailleur, ça fonctionne si tu insère mon code inchangé .
    comment faire je n'arrive pas à l'insérer

  6. #6
    Membre expérimenté Avatar de uhrand
    Profil pro
    Développeur informatique
    Inscrit en
    Octobre 2009
    Messages
    203
    Détails du profil
    Informations personnelles :
    Localisation : Luxembourg

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Octobre 2009
    Messages : 203
    Par défaut
    1. Marquer le code avec la souris
    2. Ctrl+C
    3. Ctrl+V dans la méthode que tu as écrite pour ce bouton

Discussions similaires

  1. Réponses: 5
    Dernier message: 07/07/2011, 08h22
  2. Ajouter liens sur un menu Flash existant
    Par charlotte_14 dans le forum ActionScript 3
    Réponses: 3
    Dernier message: 22/11/2010, 15h37
  3. Réponses: 15
    Dernier message: 08/10/2008, 00h04
  4. Est t il possible d'avoir un menu sur un clic droit ???
    Par almisuifre dans le forum C++Builder
    Réponses: 6
    Dernier message: 21/12/2004, 11h21
  5. Ajout Menu dans un PopupMenu
    Par Larion dans le forum C++Builder
    Réponses: 6
    Dernier message: 15/08/2004, 12h45

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo