How can I add a JTextField to a JFrame's MenuBar?

后端 未结 1 612
独厮守ぢ
独厮守ぢ 2020-11-27 22:05

I\'ve been trying to overload JMenu and put in some custom code to support a JTextField but that isn\'t going well. My main purpose here is to add a search field to the rig

相关标签:
1条回答
  • 2020-11-27 22:47

    I never see that as JMenuItem, I think that alyways placed in JMenuBar

    enter image description here

    import java.awt.ComponentOrientation;
    import javax.swing.*;
    
    public class MenuGlueDemo {
    
        public MenuGlueDemo() {
            JMenuBar menuBar = new JMenuBar();
            menuBar.add(createMenu("Menu 1"));
            menuBar.add(createMenu("Menu 2"));
            menuBar.add(createMenu("Menu 3"));
            menuBar.add(new JSeparator());
            menuBar.add(new JButton("   Seach ....  "));
            menuBar.add(new JTextField("   Seach ....  "));
            menuBar.add(new JComboBox(new Object[]{"height", "length", "volume"}));
            menuBar.add(Box.createHorizontalGlue());
            menuBar.add(createMenu("About"));
            JFrame frame = new JFrame("MenuGlueDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(menuBar);
            frame.pack();
            frame.setVisible(true);
        }
    
        public JMenu createMenu(String title) {
            JMenu m = new JMenu(title);
            m.add("Menu item #1 in " + title);
            m.add("Menu item #2 in " + title);
            m.add("Menu item #3 in " + title);
            if (title.equals("About")) {
                m.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
            }
            return m;
        }
    
        public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    MenuGlueDemo menuGlueDemo = new MenuGlueDemo();
                }
            });
        }
    }
    
    0 讨论(0)
提交回复
热议问题