怎么做二级菜单 java
我写的代码,不知为何,鼠标放到“主题美化”上面,弹出的菜单就消失了。代码可以直接复制运行,求大神指教。packagecom.rollnews.test;importjav...
我写的代码,不知为何,鼠标放到 “主题美化” 上面,弹出的菜单就消失了。代码可以直接复制运行,求大神指教。
package com.rollnews.test;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
public class Test extends JFrame{
private JPopupMenu newsMenu = null;
private Test menu = null;
public Test()
{
//弹出菜单选项
JMenuItem websURL = new JMenuItem("自定义新闻网址");
JMenuItem userSetting = new JMenuItem("设置");
JMenuItem themeBoard = new JMenuItem("主题美化");
JMenuItem about = new JMenuItem("关于");
JMenuItem help = new JMenuItem("帮助");
JMenuItem skin = new JMenuItem("滚动条皮肤");
JMenuItem menuBorder = new JMenuItem("显示菜单边框");
JMenuItem newsFont = new JMenuItem("新闻字体样式");
JMenu theme = new JMenu();
theme.add(skin);
theme.add(menuBorder);
theme.add(newsFont);
themeBoard.add(theme);
newsMenu = new JPopupMenu();
newsMenu.add(websURL);
newsMenu.add(userSetting);
newsMenu.addSeparator();
newsMenu.add(themeBoard);
newsMenu.addSeparator();
newsMenu.add(about);
newsMenu.add(help);
this.setLayout(null);
this.setBounds(0, 0, 300, 300);
this.setVisible(true);
menu = this;
setMouseEnter();
}
public void setMouseEnter()
{
menu.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
newsMenu.show(e.getComponent() , e.getX(), e.getY());
}
});
}
public static void main(String args[])
{
new Test();
}
} 展开
package com.rollnews.test;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
public class Test extends JFrame{
private JPopupMenu newsMenu = null;
private Test menu = null;
public Test()
{
//弹出菜单选项
JMenuItem websURL = new JMenuItem("自定义新闻网址");
JMenuItem userSetting = new JMenuItem("设置");
JMenuItem themeBoard = new JMenuItem("主题美化");
JMenuItem about = new JMenuItem("关于");
JMenuItem help = new JMenuItem("帮助");
JMenuItem skin = new JMenuItem("滚动条皮肤");
JMenuItem menuBorder = new JMenuItem("显示菜单边框");
JMenuItem newsFont = new JMenuItem("新闻字体样式");
JMenu theme = new JMenu();
theme.add(skin);
theme.add(menuBorder);
theme.add(newsFont);
themeBoard.add(theme);
newsMenu = new JPopupMenu();
newsMenu.add(websURL);
newsMenu.add(userSetting);
newsMenu.addSeparator();
newsMenu.add(themeBoard);
newsMenu.addSeparator();
newsMenu.add(about);
newsMenu.add(help);
this.setLayout(null);
this.setBounds(0, 0, 300, 300);
this.setVisible(true);
menu = this;
setMouseEnter();
}
public void setMouseEnter()
{
menu.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
newsMenu.show(e.getComponent() , e.getX(), e.getY());
}
});
}
public static void main(String args[])
{
new Test();
}
} 展开
2个回答
2015-06-16
展开全部
楼主,请参考 ——— 把注释去掉就是了。
public static void main(String args[]){
//final JPopupMenu jpopupMenu1 = new JPopupMenu();
//JMenuItem jmenuItem1,jmenuItem2,jmenuItem3,jmenuItem4;
//JMenu t;
//JFrame frame=new JFrame();
//frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
//frame.setSize(new Dimension(400,200));
//jmenuItem1 = new JMenuItem("新增");
//jmenuItem2 = new JMenuItem("修改");
//jmenuItem3 = new JMenuItem("删除");
//t = new JMenu("kk");
//jpopupMenu1.add(jmenuItem1);
//jpopupMenu1.add(jmenuItem2);
//jpopupMenu1.add(t);
//t.add(jmenuItem3);
//JButton b=new JButton("右击我");
//b.addMouseListener(new MouseAdapter(){
// public void mouseClicked(MouseEvent e){
// jpopupMenu1.show(e.getComponent() , e.getX(), e.getY());
// }
//});
//
//frame.setLayout(new BorderLayout());
//frame.add(b);
//frame.setVisible(true);
}
public static void main(String args[]){
//final JPopupMenu jpopupMenu1 = new JPopupMenu();
//JMenuItem jmenuItem1,jmenuItem2,jmenuItem3,jmenuItem4;
//JMenu t;
//JFrame frame=new JFrame();
//frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
//frame.setSize(new Dimension(400,200));
//jmenuItem1 = new JMenuItem("新增");
//jmenuItem2 = new JMenuItem("修改");
//jmenuItem3 = new JMenuItem("删除");
//t = new JMenu("kk");
//jpopupMenu1.add(jmenuItem1);
//jpopupMenu1.add(jmenuItem2);
//jpopupMenu1.add(t);
//t.add(jmenuItem3);
//JButton b=new JButton("右击我");
//b.addMouseListener(new MouseAdapter(){
// public void mouseClicked(MouseEvent e){
// jpopupMenu1.show(e.getComponent() , e.getX(), e.getY());
// }
//});
//
//frame.setLayout(new BorderLayout());
//frame.add(b);
//frame.setVisible(true);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询