Java – can jmenubar be added to the decoration window of JFrame?

I want to know if I can add jmenubar to the decoration window of JFrame or jrootpane, or if I can include the border in the content pane I see apps like Firefox or Photoshop have their menu bar in the decoration window

Is that possible? I looked around Google, but I haven't found any results about this kind of thing I hope Java has this ability

Solution

Not sure what you're looking for, but you can add jmenubar to JFrame – JFrame setJMenuBar(). For more information, see the how to use menus tutorial

Edit:

The following is an overly simplified example of an undecorated frame with a menu just to demonstrate the idea

You may want to move to an existing solution – Jide uses a resizableframe for this purpose It is part of the open source Jide OSS Substance L & F supports title bar customization (see what happened to the substance LAF?) You can also use @ camickr's componentmover and componentresizer classes very effectively. For more information, see the resizing components article

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(p.x + e.getX() - point.x,p.y + 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);
    }
}
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
分享
二维码
< <上一篇
下一篇>>