Huwenxiaohua
Huwenxiaohua
2018-06-28 07:20

java awt编程学习,右键菜单不能显示:请看下列代码

5
  • java
  • 编程

import java.awt.*;
import java.awt.event.*;
public class PopupMenuTest
{
private TextArea ta=new TextArea(6,30);
private Frame f=new Frame("测试窗口");
PopupMenu pop=new PopupMenu();
CheckboxMenuItem autoWrap= new CheckboxMenuItem("自动换行");
MenuItem copyItem=new MenuItem("复制");
MenuItem pasteItem=new MenuItem("粘贴");
Menu format =new Menu("格式");
MenuItem commentItem=new MenuItem("注释",new MenuShortcut(KeyEvent.VK_SLASH,true));
MenuItem cancleItem=new MenuItem("取消注释");
MenuItem exit=new MenuItem("退出");
public void init()
{
ActionListener menuListener=e->
{
String cmd=e.getActionCommand();
ta.append("单击"+cmd+"菜单"+"\n");
if (cmd.equals("退出"))
{
System.exit(0);
}
};
commentItem.addActionListener(menuListener);
pop.add(autoWrap);
pop.addSeparator();
pop.add(copyItem);
pop.add(pasteItem);
pop.add(exit);
format.add(commentItem);
format.add(cancleItem);
pop.add(new MenuItem("-"));
pop.add(format);
final Panel p=new Panel();
p.setPreferredSize(new Dimension(300,160));
p.add(pop);
p.addMouseListener(new MouseAdapter()
{
public void MouseReleased(MouseEvent e)
{
if (e.isPopupTrigger())
{
pop.show(p,e.getX(),e.getY());
}
}
});
f.add(p);
f.add(ta,BorderLayout.NORTH);
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
f.pack();
f.setVisible(true);
}
public static void main(String[] args)
{
new PopupMenuTest().init();
}
}
我的电脑是Windows10

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答