简体   繁体   English

是否可以在不将其添加到JPanel中的情况下移动JMenuBar

[英]Is it possible to move JMenuBar without adding it in a JPanel

I am facing a problem with my JMenuBar location in my Java application. 我在Java应用程序中遇到了JMenuBar位置的问题。

I am actually using ComponentResizer.java code from the article . 我实际上正在使用文章中的 ComponentResizer.java代码。

and everything with the resizing works fine except from the north area of my application (undecorated JFrame) and its corners (of North Area) because the JMenuBar is preventing me from resizing from that area. 调整大小的一切都很好,除了我的应用程序的北部区域(未修饰的JFrame)及其角落(北区),因为JMenuBar阻止我从该区域调整大小。

Is there a solution or maybe a hack to move the JMenuBar down a little or enable the Resizing in the north area? 是否有一个解决方案或者可能是一个黑客将JMenuBar向下移动一点或者在北部地区启用Resizing?

I also use setJMenuBar() method to add the JMenuBar to the north area of my application. 我还使用setJMenuBar()方法将JMenuBar添加到我的应用程序的北部区域。

Code: 码:

  public class MyApp extends JFrame {

private MyApp frame;
private JMenuBar menuBar;

public static void main(String[] args) {
    frame = new MyApp();
    frame.setUndecorated(true);
    frame.setVisible(true);
}
public MyApp(){
    initComponents();
}
private void initComponents(){
    setTitle("MediaForm");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 673, 482);
    menuBar = new JMenuBar();
    setJMenuBar(menuBar);
}
}

I've not tested this, but JRootPane is a JComponet , it may be possible to add a EmptyBorder to it, allowing the JMenuBar to be offset. 我没有测试过这个,但是JRootPane是一个JComponet ,它可能会向它添加一个EmptyBorder ,允许JMenuBar被偏移。

Failing that, you'll probably need to implement your own JRootPane to manage the layout of the JMenuBar & content pane 如果做不到这一点,您可能需要实现自己的JRootPane来管理JMenuBar和内容窗格的布局

UPDATE with Testing 更新测试

public class TestMenuFrame extends JFrame {

    public TestMenuFrame() throws HeadlessException {

        setTitle("Test");

        getRootPane().setBorder(new EmptyBorder(10, 10, 10, 10));

        JMenuBar mb = new JMenuBar();
        mb.add(new JMenu("Test"));

        setJMenuBar(mb);

        setSize(100, 100);

        getContentPane().setBackground(Color.RED);

        setLocationRelativeTo(null);

    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        new TestMenuFrame().setVisible(true);

    }
}

样品

JMenuBar extends JComponent, therefore you can put it anywhere you would put an ordinary component. JMenuBar扩展了JComponent,因此您可以将它放在任何放置普通组件的位置。 Note that it could be confusing to the users if you put it to unexpected locations. 请注意,如果将用户置于意外位置,可能会让用户感到困惑。

Also see this: add JMenuBar to a JPanel? 另见: 将JMenuBar添加到JPanel?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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