简体   繁体   中英

add JMenuBar to JPanel

I cannot add a JMenuBar to a JPanel .

My code is as follows:

public class MyClass extends JPanel
{
      public MyClass ()
      {
              JMenuBar b = new JMenuBar();
              add(b);
              b.add(new JMenu("Menu"));
              ...
       }
}
JMenuBar bar = new JMenuBar();
JMenuItem item = new JMenuItem("Item1");
bar.add(item);//item menu
add(bar); // ad to panel

if you want to position themselves could use an example design BorderLayout

setLayout(new BorderLayout());
add(bar, BorderLayout.NORTH);//position menu

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM