JPopupMenuポップアップメニュー例

1483 ワード

package Assis;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class PopupMenuDemo extends JFrame {
	
	private static final long serialVersionUID = 1L;

	public PopupMenuDemo(String strTitle) {
		//  
		super(strTitle);

		//  
		final JPopupMenu mnuRoot = new JPopupMenu();
		mnuRoot.add(new JMenuItem(" ", new ImageIcon("new.gif")));
		mnuRoot.add(new JMenuItem(" ", new ImageIcon("save.gif")));
		mnuRoot.add(new JMenuItem(" ", new ImageIcon("print.gif")));

		final JLabel label = new JLabel(" ", JLabel.CENTER);
		label.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				mnuRoot.show(label, e.getX(), e.getY());
			}
		});

		this.getContentPane().add(label);
	}

	//  400, 400
	public Dimension getPreferredSize() {
		return new Dimension(400, 400);
	}

	//  
	public static void main(String[] args) {
		//  
		PopupMenuDemo frame = new PopupMenuDemo(" ");

		//  ( )
		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});

		//  
		frame.pack();
		frame.setVisible(true);

	}
}