当前位置: 首页 > 工具软件 > jMenu > 使用案例 >

java menuitem 图标_java – 在JMenu中添加图标和文本

甘西岭
2023-12-01

我正在研究秋千.我必须创建一个包含10个菜单的主屏幕.

我已经通过JFrame和JMenu的菜单创建了表单.现在该菜单应包含文本和图像两者,并且应该在单击菜单时调用操作执行方法,以便可以在单击菜单按钮时执行操作.

现在我用JMenuItem和图标图标上的图标制作Jmenu但我想要带有图标和文字的菜单

我目前的代码是

public class MenuScreen extends JFrame implements ActionListener{

Container cp;

JMenuBar menuBar;

JLabel logo;

public MenuScreen() {

super("");

cp=this.getContentPane();

cp.setBackground(Color.gray);

menuBar= new JMenuBar();

logo=new JLabel(im);

cp.add(logo);

logo.setBounds(100,80,500,350);

helpmenu= new JMenu(" Help ");

homemenu=new JMenu(" Home ");

fieldsmenu= new JMenu(" Fields ");

backmenu= new JMenu(" Back ");

forwardmenu= new JMenu(" Forward ");

panelmenu= new JMenu(" Panel ");

searchmenu= new JMenu(" Search ");

quickmenu= new JMenu(" Quick ");

infomenu= new JMenu(" Info ");

exitmenu= new JMenu(" Exit ");

mastermenu= new JMenu(" Master ");

tarrifmenu= new JMenu(" Tarrif ");

contactmenu= new JMenu(" Contact ");

webmenu= new JMenu(" Web ");

wordmenu= new JMenu(" Word ");

legaldictionarymenu= new JMenu(" LegalDictionary ");

budgetmenu=new JMenu(" Budget 2012 2013 ");

memberdetailmenu= new JMenu(" Member Details ");

Font f1= new Font("Arial",Font.BOLD,16);

budgetmenu.setFont(f1);

legaldictionarymenu.setFont(f1);

helpmenu.setFont(f1);

JMenuItem backmenuitem= new JMenuItem(backicon);

backmenu.add(backmenuitem);

JMenuItem exitmenuitem= new JMenuItem(exiticon);

exitmenu.add(exitmenuitem);

menuBar.add(helpmenu);

menuBar.add(homemenu);

menuBar.add(fieldsmenu);

menuBar.add(backmenu);

menuBar.add(forwardmenu);

menuBar.add(panelmenu);

menuBar.add(searchmenu);

menuBar.add(quickmenu);

menuBar.add(infomenu);

menuBar.add(exitmenu);

menuBar.add(mastermenu);

menuBar.add(tarrifmenu);

menuBar.add(contactmenu);

menuBar.add(webmenu);

menuBar.add(wordmenu);

menuBar.add(legaldictionarymenu);

menuBar.add(budgetmenu);

menuBar.add(memberdetailmenu);

setJMenuBar(menuBar);

/*

Adding Listeners to the menus where required

*/

searchmenu.addActionListener(this);

}

public void actionPerformed(ActionEvent ae)

{

JOptionPane.showMessageDialog(null,"clicked");

if(ae.getActionCommand().equals("Search"))

{

SearchForm frm=new SearchForm();

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

frm.setBounds(0,0,screenSize.width, screenSize.height);

frm.setVisible(true);

}

}

public static void main(String args[])

{

MenuScreen frm= new MenuScreen();

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

frm.setBounds(0,0,screenSize.width, screenSize.height);

frm.setVisible(true);

}

}

请帮我.

 类似资料: