我正在做秋千。我必须创建一个包含 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);
}
}
请帮我。