Java Is it possible to add a JMenuBar to the JFrame layout window?

I am wondering if I can add a JMenuBar to a JFrame or JRootPane Decoration window or, otherwise, the border that surrounds the content panel inside. I see applications like Firefox or Photoshop that have menus in the design window.

Is it possible? I looked at google, but I could not find any results on this. I hope Java has this feature.

+6
source share
3 answers

Not sure what you're looking for, but you can add a JMenuBar to a JFrame - JFrame.setJMenuBar () . See How to use the menu for details.

Edit:

Below is an oversimplified example of a disordered frame with a menu, just to showcase the idea.

You can refer to existing solutions - for this purpose JIDE has a ResizableFrame . This is part of the open source JIDE-oss . The L & F agent supports header customization (see What happened to LaF? ). You can also very efficiently use the ComponentMover and ComponentResizer classes by @camickr, see Resizing components for more details.

 import javax.swing.*; import java.awt.event.*; import java.awt.*; public class UndecoratedFrameDemo { private static Point point = new Point(); public static void main(String[] args) { final JFrame frame = new JFrame(); frame.setUndecorated(true); frame.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { point.x = e.getX(); point.y = e.getY(); } }); frame.addMouseMotionListener(new MouseMotionAdapter() { public void mouseDragged(MouseEvent e) { Point p = frame.getLocation(); frame.setLocation(px + e.getX() - point.x, py + e.getY() - point.y); } }); frame.setSize(300, 300); frame.setLocation(200, 200); frame.setLayout(new BorderLayout()); frame.getContentPane().add(new JLabel("Drag to move", JLabel.CENTER), BorderLayout.CENTER); JMenuBar menuBar = new JMenuBar(); JMenu menu = new JMenu("Menu"); menuBar.add(menu); JMenuItem item = new JMenuItem("Exit"); item.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { System.exit(0); } }); menu.add(item); frame.setJMenuBar(menuBar); frame.setVisible(true); } } 
+9
source

Try it...

Adding JMenuBar to JFrame

 JMenuBar menuBar = new JMenuBar(); myFrame.setJMenuBar(menuBar); 

Adding JMenuBar to JPanel

 JMenuBar menuBar = new JMenuBar(); myPanel.add(menuBar); 

See the JMenuBar Tutorial for more information on this topic.

+2
source

I did something, and the short answer is ... no.

Basically, the frame design is disconnected from the OS, so we do not have access to it.

However, if you want to work hard, you can implement your own design. You must take responsibility for resizing and moving.

you can check

http://java-swing-tips.blogspot.com.au/2010/05/custom-decorated-titlebar-jframe.html and http://www.paulbain.com/2009/10/13/howto-draggabe- jframe-without-decoration / for ideas

+1
source

Source: https://habr.com/ru/post/921011/


All Articles