日期:2014-05-20  浏览次数:20829 次

popupMenu显示问题
我再JTextArea对象中加入了一个popupMenu菜单,可是点击右键不能显示,请问如何让该popupMenu显示出来呢?

------解决方案--------------------
package jsp;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;

public class Frame1 extends JFrame {
JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();
JTextArea text=new JTextArea();
JPopupMenu menu=new JPopupMenu();
JMenuItem jmenu=new JMenuItem( "hide ");

public Frame1() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}

/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(borderLayout1);
setSize(new Dimension(400, 300));
setTitle( "Frame Title ");
this.add(text,borderLayout1.CENTER);
menu.add(jmenu);

text.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
if (e.getButton()==MouseEvent.BUTTON3)
{
menu.show(text,e.getX(),e.getY());
}else{
menu.setVisible(false);
}
}
});
}

public static void main(String[] args)
{
new Frame1().setVisible(true);
}
}