|
[code:1]
import java.awt.*;
import java.awt.event.*;
public class TestMenu
{
public static void main(String args[])
{
MyMenuFrame mf=new MyMenuFrame();
mf.setSize(new Dimension(600,400));
mf.setVisible(true);
}
}
class MyMenuFrame extends Frame
implements ActionListener,ItemListener
{
MenuBar m_MenuBar;
Menu menuFile,menuEdit,m_Edit_Paste;
MenuItem mi_File_Open,mi_File_Close,mi_File_Exit,mi_Edit_Copy;
MenuItem pi_New,pi_Del,pi_Pro,mi_Paste_All,mi_Paste_Part;
CheckboxMenuItem mi_Edit_Cut;
PopupMenu popM;
TextArea ta;
MyMenuFrame()
{
super("window with menu");
a=new TextArea("\n\n\n\n\n\n\t\t\t not be choosed",5,20);
ta.addMouseListener(new HandleMouse(this));
add("Center",ta);
popM=new PopupMenu();
pi_New=new MenuItem("new");
pi_New.addActionListener(this);
popM.add(pi_New);
pi_Del=new MenuItem("del");
pi_Del.addActionListener(this);
popM.add(pi_Del);
pi_Pro=new MenuItem("pro");
pi_Pro.addActionListener(this);
popM.add(pi_Pro);
ta.add(popM);
m_MenuBar=new MenuBar();
menuFile=new Menu("File");
mi_File_Open=new MenuItem("Open",new MenuShortcut('o'));
mi_File_Close=new MenuItem("close",new MenuShortcut('w'));
mi_File_Exit=new MenuItem("Exit");
mi_File_Exit.setShortcut(new MenuShortcut('x'));
mi_File_Open.setActionCommand("open");
mi_File_Exit.setActionCommand("exit");
mi_File_Open.addActionListener(this);
mi_File_Close.addActionListener(this);
mi_File_Exit.addActionListener(this);
menuFile.add(mi_File_Open);
menuFile.add(mi_File_Close);
menuFile.addSeparator();
menuFile.add(mi_File_Exit);
m_MenuBar.add(menuFile);
menuEdit=new Menu("Edit");
mi_Edit_Copy=new MenuItem("Copy");
mi_Edit_Cut=new CheckboxMenuItem("Cut");
m_Edit_Paste=new Menu("Paste");
mi_Paste_All=new MenuItem("Copy All");
mi_Paste_Part=new MenuItem("Copy Part");
mi_Edit_Copy.addActionListener(this);
mi_Edit_Cut.addActionListener(this);
m_Edit_Paste.add(mi_Paste_All);
m_Edit_Paste.add(mi_Paste_Part);
mi_Paste_All.addActionListener(this);
mi_Paste_Part.addActionListener(this);
menuEdit.add(mi_Edit_Copy);
menuEdit.add(mi_Edit_Cut);
menuEdit.addSeparator();
menuEdit.add(m_Edit_Paste);
m_MenuBar.add(menuEdit);
this.setMenuBar(m_MenuBar);
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand()=="exit")
{
dispose();
System.exit(0);
}
else
ta.setText("\n\n\n\n\n\n\n\t\t\t"+e.getActionCommand());
}
public void itemStateChanged(ItemEvent e)
{
if(e.getSource()==mi_Edit_Cut)
if(((CheckboxMenuItem)e.getSource()).getState())
ta.setText("\n\n\n\n\n\n\n\t\t\t"+"you choose"
+((CheckboxMenuItem)e.getSource()).getLabel());
else
ta.setText("\n\n\n\n\n\n\n\t\t\t"+"not choose"
+((CheckboxMenuItem)e.getSource()).getLabel());
}
}
class HandleMouse extends MouseAdapter
{
MyMenuFrame m_Parent;
HandleMouse(MyMenuFrame mf)
{
m_Parent=mf;
}
public void mouseReleased(MouseEvent e)
{
if(e.isPopupTrigger())
m_Parent.popM.show((Component)e.getSource(),e.getX(),e.getY());
}
}
[/code:1]
在linux下编译,鼠标事件不会发生
而在windows下确可以
这是什么原因
而且快捷键也不一样 |
|