Java - 是否可以将JMenuBar添加到JFrame的装饰窗口? [英] Java - Is it possible to add a JMenuBar to a JFrame's decoration window?

查看:172
本文介绍了Java - 是否可以将JMenuBar添加到JFrame的装饰窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道我是否可以将JMenuBar添加到JFrame或JRootPane的装饰窗口,或者是否包含内容窗格内的边框。我看到像Firefox或Photoshop这样的应用程序在装饰窗口中有他们的菜单栏。

I'm wondering if I can add a JMenuBar to a JFrame or JRootPane's Decoration window, or otherwise the border that surrounds the content pane within. I see applications like Firefox or Photoshop having their menubar in the decoration window.

这可能吗?我环顾四周谷歌,但我还没有找到任何关于这种事情的结果。我希望Java具备这种能力。

Is this possible? I've looked around google, but I haven't been able to find any results over this kind of thing. I'm hoping Java has this capability.

推荐答案

不确定您要找的是什么,但可以添加 JMenuBar JFrame - JFrame.setJMenuBar()。有关详细信息,请参阅如何使用菜单教程。

Not sure what you're looking for, but you can add JMenuBar to JFrame - JFrame.setJMenuBar(). Look at How to Use Menus tutorial for details.

编辑:

下面是一个过度简化的未装饰框架示例,带有菜单,只是为了演示这个想法。

Below is an overly simplified example of undecorated frame with a menu, just to demo the idea.

您可能希望转向现有解决方案 - 为此目的,JIDE有 ResizableFrame 。它是开源 JIDE-oss 的一部分。 物质L& F 支持标题栏自定义(请参阅物质LaF发生了什么?)。你也可以非常有效地利用@camickr的 ComponentMover ComponentResizer 类,参见调整组件大小文章了解更多详情。

You may want to turn to existing solutions - JIDE has ResizableFrame for this purpose. It is part of open source JIDE-oss. Substance L&F has support for title bar customization (see What happened to the Substance LaF?). You can also very efficiently utilize ComponentMover and ComponentResizer classes by @camickr, see Resizing Components article 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(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);
    }
}

这篇关于Java - 是否可以将JMenuBar添加到JFrame的装饰窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆