IT_Programming/Java

JPopupMenu Example

JJun ™ 2008. 8. 20. 08:46

 

 

 

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
class MenuPopup_test extends JFrame implements MouseListener
{
   JMenu file = new JMenu("파일");
   JMenuItem test1 = new JMenuItem("속성1"); // 제거될 가능성이 있는 컴포넌트
    JMenuItem test2 = new JMenuItem("속성2"); // 제거될 가능성이 있는 컴포넌트
    JPopupMenu pop = new JPopupMenu();
   JMenuItem del = new JMenuItem("삭제");
   JMenuItem temp;
   public MenuPopup_test(){
   
     this.setVisible(true);
     this.setSize(500,300);
      // 메뉴설정
      JMenuBar jmb = new JMenuBar();
      this.setJMenuBar(jmb);
      jmb.add(file);
      file.add(test1);
      file.add(test2);
      //팝업설정
      pop.add(del);

      test1.addMouseListener(this);
      test2.addMouseListener(this);
      del.addMouseListener(this);
   }

   public void mouseClicked(MouseEvent e){}
   public void mouseEntered(MouseEvent e){}
   public void mouseExited(MouseEvent e) {}

   public void mousePressed(MouseEvent e){
    if(e.getSource()==del){
    file.remove(temp);
   }
   }
   public void mouseReleased(MouseEvent e){

     if(e.getSource()==test1  e.getSource()==test2){
       //메뉴마우스좌표+메뉴좌표+창테두리값
       popUpShow(e.getX()+e.getComponent().getX()+5, e.getY()+e.getComponent().getY()+30+20);
       //삭제메뉴
       temp=(JMenuItem) e.getComponent();
  
     }
  }

  private void popUpShow(int x, int y) {
// TODO Auto-generated method stub
   pop.show(this, x, y);
}

public static void main(String[] args)
  {
       new MenuPopup_test();
  }
}

'IT_Programming > Java' 카테고리의 다른 글

JTabbedPane & JEditorPane Example   (0) 2008.08.20
JDialog Example   (0) 2008.08.20
image view (이미지 여러 방향으로 뒤집기)  (0) 2008.08.20
java HTML Parsing & ParserCallback   (0) 2008.08.20
다중파일 전송   (0) 2008.08.20