弹出菜单

import java.awt.Container;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;

public class TestPopup extends JFrame implements ActionListener, MouseListener {

	private static final long serialVersionUID = 1L;

	Container container;

	JPopupMenu jpm;

	JMenuItem copy;
	JMenuItem paste;
	JMenuItem cut;
	JMenuItem select;
	JMenuItem delete;

	JScrollPane jsp;

	JTextArea jta;

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

	public TestPopup() {
		super("PopupMenu");
		initUI();
	}

	public void initUI() {
		container = getContentPane();

		setBounds(300, 300, 500, 390);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		jta = new JTextArea();
		jta.setLineWrap(true);

		jsp = new JScrollPane(jta);

		jpm = new JPopupMenu();

		copy = new JMenuItem("copy");
		paste = new JMenuItem("paste");
		cut = new JMenuItem("cut");
		delete=new JMenuItem("delete");
		select=new JMenuItem("select");

		copy.setAccelerator(KeyStroke.getKeyStroke('C', InputEvent.CTRL_MASK));
		paste.setAccelerator(KeyStroke.getKeyStroke('V', InputEvent.CTRL_MASK));
		cut.setAccelerator(KeyStroke.getKeyStroke('X', InputEvent.CTRL_MASK));
		delete.setAccelerator(KeyStroke.getKeyStroke('D',InputEvent.CTRL_MASK));
		select.setAccelerator(KeyStroke.getKeyStroke('A',InputEvent.CTRL_MASK));

		jpm.add(copy);
		jpm.add(paste);
		jpm.add(cut);
		jpm.add(delete);
		jpm.add(select);

		jta.add(jpm);
		
		container.add(jsp);

		jta.addMouseListener(this);
		copy.addActionListener(this);
		paste.addActionListener(this);
		cut.addActionListener(this);
		delete.addActionListener(this);
		select.addActionListener(this);
		
		setVisible(true);
	}

	/**
	 * 剪切板上是否有文本数据
	 * @return
	 */
	public boolean hasSelected() {
		boolean result = false;
		Clipboard clipboard = getToolkit().getSystemClipboard();
		Transferable content = clipboard.getContents(this);
		try {
			if (content.getTransferData(DataFlavor.stringFlavor) instanceof String) {
				result = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 是否有文本被选中
	 * @return
	 */
	public boolean hasSelect() {
		boolean result = false;
		int start = jta.getSelectionStart();
		int end = jta.getSelectionEnd();
		if (start != end) {
			result = true;
		}
		return result;
	}

	public void actionPerformed(ActionEvent e) {
		String cmd = e.getActionCommand();
		if (cmd.equals(copy.getText())) {
			jta.copy();
		} else if (cmd.equals(paste.getText())) {
			jta.paste();
		} else if (cmd.equals(cut.getText())) {
			jta.cut();
		}else if(cmd.equals(delete.getText())){
			StringBuffer sb=new StringBuffer(jta.getText());
			int start=jta.getSelectionStart();
			int end=jta.getSelectionEnd();
			sb.delete(start,end);
			jta.setText(sb.toString());
		}else if(cmd.equals(select.getText())){
			jta.selectAll();
		}
	}

	public void mouseClicked(MouseEvent e) {
	}

	public void mousePressed(MouseEvent e) {
		if (e.getButton() == MouseEvent.BUTTON3) {
			copy.setEnabled(hasSelect());
			paste.setEnabled(hasSelected());
			cut.setEnabled(hasSelect());
			delete.setEnabled(hasSelect());
			jpm.show(this, e.getX(), e.getY());
		}
	}

	public void mouseReleased(MouseEvent e) {
	}

	public void mouseEntered(MouseEvent e) {
	}

	public void mouseExited(MouseEvent e) {
	}

}

 

你可能感兴趣的:(java,jsp,swing)