Java – how to add jtextfield to the menubar of JFrame?

I've been trying to reload JMenu and put some custom code to support jtextfield, but it's not going well My main goal is to add a search field to the right of the menu item So I have some files, edit, help on the left, and then on the right will be the search bar, almost like some Google search bars in some browsers Someone has an idea, how can I add this feature?

Solution

I've never seen anything like jmenuitem. I think always is in jmenubar

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 JCombo@R_546_2419@(new Object[]{"height","length","volume"}));
        menuBar.add(@R_546_2419@.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();
            }
        });
    }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>