//************************************************************* // Java applet // 「PopupTest」 // //          作 成 者:ルート高菜    //          作成開始月:2006/6 //          最終更新月:2006/6 [TN086/J76] //************************************************************* import java.awt.*; import java.awt.event.*; import javax.swing.*; public class PopupTest extends JApplet implements MouseListener,ActionListener { //Eclipseの場合、シリアライズ可能クラスでこれがないと警告が出る private static final long serialVersionUID=0; // Container cp; JLabel label; // PopupMenu popupMenu; MenuItem def,cyan,yellow,magenta; Menu gray; MenuItem darkGray,normalGray,lightGray; // public void init() { //コンテナ枠 cp=getContentPane(); cp.setBackground(Color.WHITE); label=new JLabel("ポップアップメニューから背景色を変更"); cp.add(label); // def=new MenuItem("default(white)"); cyan=new MenuItem("cyan"); yellow=new MenuItem("yallow"); magenta=new MenuItem("magenta"); darkGray=new MenuItem("dark gray"); normalGray=new MenuItem("gray"); lightGray=new MenuItem("light gray"); gray=new Menu("gray"); gray.add(darkGray); gray.add(normalGray); gray.add(lightGray); // def.addActionListener(this); cyan.addActionListener(this); yellow.addActionListener(this); magenta.addActionListener(this); darkGray.addActionListener(this); normalGray.addActionListener(this); lightGray.addActionListener(this); // popupMenu=new PopupMenu("PopupMenu"); popupMenu.add(def); popupMenu.addSeparator(); popupMenu.add(cyan); popupMenu.add(yellow); popupMenu.add(magenta); popupMenu.add(gray); // add(popupMenu); // addMouseListener(this); } //メニュー項目イベント public void actionPerformed(ActionEvent e){ if(e.getSource()==def){ cp.setBackground(Color.WHITE); }else if(e.getSource()==cyan){ cp.setBackground(Color.CYAN); }else if(e.getSource()==yellow){ cp.setBackground(Color.YELLOW); }else if(e.getSource()==magenta){ cp.setBackground(Color.MAGENTA); }else if(e.getSource()==darkGray){ cp.setBackground(Color.DARK_GRAY); }else if(e.getSource()==normalGray){ cp.setBackground(Color.GRAY); }else if(e.getSource()==lightGray){ cp.setBackground(Color.LIGHT_GRAY); } } //マウスイベント(右ボタン開放で、その位置に表示) public void mouseClicked(MouseEvent arg0) { } public void mousePressed(MouseEvent arg0) { } public void mouseReleased(MouseEvent e) { if(e.isPopupTrigger()){ popupMenu.show(this,e.getX(),e.getY()); } } public void mouseEntered(MouseEvent arg0) { } public void mouseExited(MouseEvent arg0) { } }