问题描述
- java*AWT 为什么给多行文本域编写右键菜单,出现的不是我编写的菜单
-
为什么给多行文本域编写右键菜单,出现的不是我编写的菜单
原码贴上:
import java.awt.BorderLayout;
import java.awt.CheckboxMenuItem;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Menu;
import java.awt.MenuItem;
import java.awt.MenuShortcut;
import java.awt.Panel;
import java.awt.PopupMenu;
import java.awt.TextArea;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;public class PopupMenuTest {
private TextArea ta=new TextArea(4,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("格式");
// 创建commentItem菜单项,指定使用“Ctrl+Shift+“注释
MenuItem commentItem=new MenuItem("注释",new MenuShortcut(KeyEvent.VK_SHIFT,true));
MenuItem cancelIetm=new MenuItem("取消注释");
private void init() {//----------------------------------------------------------------------------public
// TODO 以Lambda表达式创建菜单事件监听器
ActionListener menuListener=e->{
String cmd=e.getActionCommand();
ta.append("单击“"+cmd+"“菜单"+"
");
if (cmd.equals("退出")) {
System.exit(0);
}};
// 为commentItem菜单添加事件监听
commentItem.addActionListener(menuListener);
// 为Pop菜单添加菜单项
pop.add(autoWrap);
// 使用addSeparator方法来添加菜单分割线
pop.addSeparator();
pop.add(copyItem);
pop.add(pasteItem);
// 为format菜单添加菜单项
format.add(commentItem);
format.add(cancelIetm);
// 使用new MenuItem("-")方法添加菜单分割线
pop.add(new MenuItem("-"));
// 将format菜单组合到pop菜单中,从而形成二级菜单
pop.add(format);
final Panel p=new Panel();
p.setPreferredSize(new Dimension(300, 160));
// 向p窗口中添加PopupMenu对象
p.add(pop);
// 添加鼠标事件监听器
p.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
// 如果释放的是鼠标右键
if (e.isPopupTrigger()) {
pop.show(p, e.getX(), e.getY());
}
}}); /*p.addMouseListener(new MouseAdapter() { @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub
// 如果释放的是鼠标右键
if (e.isPopupTrigger()) {
pop.show(p, e.getX(), e.getY());
}
}@Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } }); */ f.add(ta,BorderLayout.SOUTH); // 以匿名内部来的形式来创建事件监听对象 f.addWindowListener(new WindowListener() { @Override public void windowOpened(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowIconified(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowDeiconified(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowDeactivated(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowClosing(WindowEvent e) { // TODO Auto-generated method stub System.out.println("用户关闭窗口! "); System.out.println("用户关闭窗口! "); System.exit(0); } @Override public void windowClosed(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowActivated(WindowEvent e) { // TODO Auto-generated method stub } }); f.pack(); f.setVisible(true); } public static void main(String[] args) { new PopupMenuTest().init(); }
}
是因为AWT的实现机制吗?
AWT没有为GUI组件提供实现,调用的只是运行平台(windows)的GUI组件来创建和平台一致的对等体????是这样吗?我的理解有问题吗?
如果对的话,怎么实现右键菜单的功能呢?
不对的话为什么?