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
| import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
public class ClicDroit extends JFrame {
private JPopupMenu jpm=new JPopupMenu();
private JPanel leConteneur=new JPanel();
private JMenu Lemenu1=new JMenu("Fichier");
private JMenu Lemenu2=new JMenu("Aide");
private JMenuBar laBar=new JMenuBar();
private JMenuItem item1=new JMenuItem("Option1");
private JMenuItem item2=new JMenuItem("Option2");
public ClicDroit(){
this.setSize(500,500);
this.setTitle("clic droit");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setLayout(new BorderLayout());
Lemenu1.add(item1);
Lemenu1.add(item2);
this.laBar.add(Lemenu1);
this.laBar.add(Lemenu2);
this.setJMenuBar(laBar);
leConteneur.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent event){
if(event.isPopupTrigger()){
jpm.add(item1);
jpm.add(item2);
jpm.show(leConteneur, event.getX(), event.getY());
//System.out.println("test");
}
}
});
this.setContentPane(leConteneur);
this.setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub cl
new ClicDroit();
}} |
Partager