【问题标题】:Is there a way to move a jMenuBar to somewhere else than the top of my jFrame?有没有办法将 jMenuBar 移动到我的 jFrame 顶部以外的其他地方?
【发布时间】:2019-04-30 04:12:44
【问题描述】:

我正在创建一个未装饰的 jFrame,并且我想在这个 jFrame 中添加一个 jMenuBar 以允许我使用下拉菜单来处理各种事情。

但是,jMenuBar 始终默认位于我的 jFrame 的最顶部,并且它没有装饰,它会按下我用“最小化”和“关闭”按钮设置在那里的 jPanel,并试图将它移动到其他任何地方失败。

我尝试过使用 jToolBar,虽然它允许在放置方面进行更多自定义,但我找不到将下拉菜单添加到那里的按钮的方法。

有没有办法将 jMenuBar 移动到顶部以外的其他位置或向 jToolBar 添加下拉菜单?

【问题讨论】:

    标签: java swing


    【解决方案1】:

    你应该看看Layout Managers

    这里有一个简单的例子来实现你想要的,使用BorderLayout

    public static void main(String[] args) {
        JFrame jf = new JFrame();
    
        JPanel container = new JPanel();
        container.setLayout(new BorderLayout());
    
        JLabel label = new JLabel("North");
        JMenuBar menuBar = new JMenuBar();
    
        JMenu menu = new JMenu("A south menu");
        menu.add(new JMenuItem("A south item"));
    
        menuBar.add(menu);
    
        container.add(label, BorderLayout.NORTH);
        container.add(menuBar, BorderLayout.SOUTH);
    
        jf.add(container);
        jf.setPreferredSize(new Dimension(200,100));
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setVisible(true);
        jf.pack();
    }
    

    结果:

    【讨论】:

      猜你喜欢
      • 2014-01-08
      • 1970-01-01
      • 1970-01-01
      • 2011-03-28
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 2011-12-31
      相关资源
      最近更新 更多