JAVA二级菜单
课堂上老师说的比较块..所以我在写的时候可能有写错.但是就是找不到原因...请指教指教..packagemenu;importjava.awt.*;importjava....
课堂上老师说的比较块..所以我在写的时候可能有写错.但是就是找不到原因. ..请指教指教..
package menu;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JPopupMenu;
public class MyMenu extends Frame
{
MenuBar menubar;
Menu file,edit,help,subedit;
MenuItem f1,f2,f3,f4,e2,e3,e4,se1;
MenuItem e1;
MyMenu(String s)
{
super(s);
menubar = new MenuBar();
setMenuBar(menubar);
file = new Menu("文件");
edit = new Menu("编辑");
help = new Menu("帮助");
subedit=new Menu("二级菜单");
menubar.add(file);
menubar.add(edit);
menubar.add(help);
e1=new MenuItem("copy");
//
e1.setShortcut(new MenuShortcut(KeyEvent.VK_C));
e1=new MenuItem("-");
e1=new MenuItem("cut");
se1=new MenuItem("s");
edit.add(e1);
edit.add(e2);
edit.add(e3);
subedit.add(se1);
edit.add(subedit);
PopupMenu popm = new PopupMenu("编辑");
popm.add(e1);
add(popm);
setSize(600,600);
addWindowListener
(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
//popm.show(this, 150, 150);
addMouseListener(new MouseAdapter()
{
public void pmshow(int x,int y)
{
popm.show(this, x, y);
}
public void mousePressed(MouseEvent e)
{
if(e.getButton()==e.BUTTON3)
{
pmshow(e.getX(),e.getY());
}
}
});
setVisible(true);
}
public static void main(String[] args)
{
new MyMenu("我的菜单");
}
} 展开
package menu;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JPopupMenu;
public class MyMenu extends Frame
{
MenuBar menubar;
Menu file,edit,help,subedit;
MenuItem f1,f2,f3,f4,e2,e3,e4,se1;
MenuItem e1;
MyMenu(String s)
{
super(s);
menubar = new MenuBar();
setMenuBar(menubar);
file = new Menu("文件");
edit = new Menu("编辑");
help = new Menu("帮助");
subedit=new Menu("二级菜单");
menubar.add(file);
menubar.add(edit);
menubar.add(help);
e1=new MenuItem("copy");
//
e1.setShortcut(new MenuShortcut(KeyEvent.VK_C));
e1=new MenuItem("-");
e1=new MenuItem("cut");
se1=new MenuItem("s");
edit.add(e1);
edit.add(e2);
edit.add(e3);
subedit.add(se1);
edit.add(subedit);
PopupMenu popm = new PopupMenu("编辑");
popm.add(e1);
add(popm);
setSize(600,600);
addWindowListener
(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
//popm.show(this, 150, 150);
addMouseListener(new MouseAdapter()
{
public void pmshow(int x,int y)
{
popm.show(this, x, y);
}
public void mousePressed(MouseEvent e)
{
if(e.getButton()==e.BUTTON3)
{
pmshow(e.getX(),e.getY());
}
}
});
setVisible(true);
}
public static void main(String[] args)
{
new MyMenu("我的菜单");
}
} 展开
1个回答
展开全部
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JPopupMenu;
public class MyMenu extends Frame {
MenuBar menubar;
static MyMenu myMenu;
Menu file, edit, help, subedit;
MenuItem f1, f2, f3, f4, e2, e3, e4, se1;
MenuItem e1;
PopupMenu popm = new PopupMenu("编辑");
MyMenu(String s) {
super(s);
menubar = new MenuBar();
setMenuBar(menubar);
file = new Menu("文件");
edit = new Menu("编辑");
help = new Menu("帮助");
subedit = new Menu("二级菜单");
menubar.add(file);
menubar.add(edit);
menubar.add(help);
e1 = new MenuItem("copy");
//
e1.setShortcut(new MenuShortcut(KeyEvent.VK_C));
e2 = new MenuItem("-");
e3 = new MenuItem("cut");
se1 = new MenuItem("s");
edit.add(e1);
edit.add(e2);
edit.add(e3);
subedit.add(se1);
edit.add(subedit);
popm.add(e1);
add(popm);
setSize(600, 600);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// popm.show(this, 150, 150);
addMouseListener(new MouseAdapter() {
public void pmshow(int x, int y) {
popm.show(myMenu, x, y);
}
public void mousePressed(MouseEvent e) {
if (e.getButton() == e.BUTTON3) {
pmshow(e.getX(), e.getY());
}
}
});
setVisible(true);
}
public static void main(String[] args) {
myMenu=new MyMenu("我的菜单");
}
}
楼主的错误太多...
我只得给楼主改好..然后楼主再看一下...
运行个试试....和自己的比一下就知道了...
有几个是空指针...因为楼主没有给它赋值....
现在已经写好了....
祝楼主早日成功!!
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JPopupMenu;
public class MyMenu extends Frame {
MenuBar menubar;
static MyMenu myMenu;
Menu file, edit, help, subedit;
MenuItem f1, f2, f3, f4, e2, e3, e4, se1;
MenuItem e1;
PopupMenu popm = new PopupMenu("编辑");
MyMenu(String s) {
super(s);
menubar = new MenuBar();
setMenuBar(menubar);
file = new Menu("文件");
edit = new Menu("编辑");
help = new Menu("帮助");
subedit = new Menu("二级菜单");
menubar.add(file);
menubar.add(edit);
menubar.add(help);
e1 = new MenuItem("copy");
//
e1.setShortcut(new MenuShortcut(KeyEvent.VK_C));
e2 = new MenuItem("-");
e3 = new MenuItem("cut");
se1 = new MenuItem("s");
edit.add(e1);
edit.add(e2);
edit.add(e3);
subedit.add(se1);
edit.add(subedit);
popm.add(e1);
add(popm);
setSize(600, 600);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// popm.show(this, 150, 150);
addMouseListener(new MouseAdapter() {
public void pmshow(int x, int y) {
popm.show(myMenu, x, y);
}
public void mousePressed(MouseEvent e) {
if (e.getButton() == e.BUTTON3) {
pmshow(e.getX(), e.getY());
}
}
});
setVisible(true);
}
public static void main(String[] args) {
myMenu=new MyMenu("我的菜单");
}
}
楼主的错误太多...
我只得给楼主改好..然后楼主再看一下...
运行个试试....和自己的比一下就知道了...
有几个是空指针...因为楼主没有给它赋值....
现在已经写好了....
祝楼主早日成功!!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询