java 在 Swing 中的 JMenu 中添加图标和文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12527092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:14:59  来源:igfitidea点击:

add icon and text in JMenu in swing

javaswingmenujframeawt

提问by ADESH RAJPUT

I am working on swings. I have to create a mainscreen consisting 10 menus. I have created the form by JFrame and menu by JMenu. Now that menu should contain text and image both and action performed method should be called on clicking of the menu so that action could be performed on clicking of the menu button.

我在做秋千。我必须创建一个包含 10 个菜单的主屏幕。我已经通过 JFrame 创建了表单,通过 JMenu 创建了菜单。现在该菜单应该包含文本和图像,并且应该在单击菜单时调用操作执行方法,以便可以在单击菜单按钮时执行操作。

Right now I'm making Jmenu with JMenuItem and icon on image icon but I want the menu with icon and text

现在我正在使用 JMenuItem 和图像图标上的图标制作 Jmenu,但我想要带有图标和文本的菜单

my current code is

我目前的代码是

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);
}
}

please help me.

请帮我。

回答by basiljames

Use the constructor JMenuItem(String text, Icon icon)
If you want JMenu with Icon and Text do

使用构造函数JMenuItem(String text, Icon icon)
如果你想要带有图标和文本的 JMenu 做

helpmenu= new JMenu(" Help  ");
helpmenu.setIcon(..);

回答by Kumar Vivek Mitra

-For the JMenuItemuse JMenuItem((String text, Icon icon)constructor,

-对于JMenuItem使用JMenuItem((String text, Icon icon)构造函数

-And for JMenutry using the constructorwhich takes String as an Argument JMenu(String s), and methodsetIcon()

-JMenu尝试使用将 String 作为 Argument和方法构造函数JMenu(String s)setIcon()

For setting the special key options on the MenuItems usesetAccelerator()

要在 MenuItems 上设置特殊键选项,请使用setAccelerator()

Eg:

例如:

myItem.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_P, ActionEvent.CTRL_MASK));

myItem.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_P, ActionEvent.CTRL_MASK));

回答by Robin

Set the icon and text in your Actionwhich you place on the menu (using the key-value pair mechanism). The keys are defined in the Actioninterface.

设置Action您放置在菜单上的图标和文本(使用键值对机制)。键在Action接口中定义。

The class javadoc of the Actioninterfacealso contains a nice overview table of the available keys and what they affect.

接口类 javadocAction还包含一个很好的可用键及其影响的概述表。