JMenu 出现在 JFrame 后面

JMenu appearing behind JFrame

我正在 Eclipse IDE 中制作 Java Applet。我有一个 JMenu 和一个 JFrame。由于某种原因,菜单出现在 JFrame 后面..

像这样:

这里是调用它的地方:

public Main() throws Exception{

    JFrame i = new JFrame("My Applet");
    JLabel label = new JLabel();

    ImageIcon icon = new ImageIcon(new URL(
               "loader.gif"));
    icon.setImageObserver(null);
    label.setIcon(icon);
    i.setBackground(Color.BLACK);


    JMenuBar menuBar = new JMenuBar();

    i.setJMenuBar(menuBar);

    JMenu accountMenu = new JMenu("Account");
    JMenu toolsMenu = new JMenu("Tools");
    JMenu optionsMenu = new JMenu("Options");

    menuBar.add(accountMenu);
    menuBar.add(toolsMenu);
    menuBar.add(optionsMenu);

    JMenuItem accountdetAction = new JMenuItem("Account Details");
    JMenuItem exitAction = new JMenuItem("Exit");

    accountMenu.add(accountdetAction);


    optionsMenu.addSeparator();
    optionsMenu.add(exitAction);

    ButtonGroup bg = new ButtonGroup();

    accountdetAction.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            System.out.println("test");
        }
    });

    i.add(label);
    i.pack();
    i.setVisible(true);


    String str = getPageSource(new URL("my url link goes here"));
    Pattern pattern = Pattern.compile("gamep\w+");
    Matcher match = pattern.matcher(str);
    if (match.find()){
         Main stub = new Main(Pattern.compile("<param name=\"([^\s]+)\"\s+value=\"([^>]*)\">"), str);
         Download("my url link goes here", match.group(0)+ "jar");
         stub.setCodeBase(new URL("my url link goes here" + match.group(0)+ ".jar"));
         stub.setDocumentBase(new URL("my url link goes here" + match.group(0)+ "jar"));
         stub.getParameter (parameters.get("java_arguments"));
         URLClassLoader classLoader = new URLClassLoader(new URL[]{ new URL("file:thejar.jar")});
         Applet applet = (Applet) classLoader.loadClass ("client").newInstance();
         applet.setStub(stub);
         applet.setPreferredSize(new Dimension(1000, 500));
         applet.init();
         applet.start();

         i.setJMenuBar(menuBar);


         i.add(applet);
         i.pack();
         i.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         label.setVisible(false);


    }
}

我也尝试过在 if 语句中调用菜单,但不起作用。有没有办法强制它在上面?我听说使用 JPanels 很有效,但我不知道该怎么做...

提前感谢您的任何建议:)

我认为这不是一个好的实现。为什么要在 JFrame 中使用小程序? JavaFX 可能是更好的解决方案。 :)

举个例子,你可以试试这个

import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

import java.awt.Graphics;

public final class PullingAppletExample{
    private JFrame mainWindow = null;   
    private JMenuBar menuContainer = null;
    private JMenu    menu1 = null; 
    private JMenu    menu2 = null;
    private JMenuItem mnuItem1 = null;
    private JMenuItem mnuItem2 = null;
    private JMenuItem mnuItem3 = null;
    private JMenuItem mnuItem4 = null;

    public PullingAppletExample(){

        menu1 = new JMenu("Menu #1");
        menu2 = new JMenu("Menu #2");
        mnuItem1 = new JMenuItem("Item #1");
        mnuItem2 = new JMenuItem("Item #2");
        mnuItem3 = new JMenuItem("Item #3");
        mnuItem4 = new JMenuItem("Item #4");

        menu1.add(mnuItem1);
        menu1.add(mnuItem2);
        menu1.add(mnuItem3);

        menu2.add(mnuItem4);

        menuContainer = new JMenuBar();
        menuContainer.add(menu1);
        menuContainer.add(menu2);

        mainWindow = new JFrame();
        mainWindow.setTitle("Applet Inside JFrame");
        mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainWindow.setBounds(100,100,500,500);
        mainWindow.setJMenuBar(menuContainer);
        mainWindow.setVisible(true);
        mainWindow.setContentPane(new CustomApplet());

    }
    private class CustomApplet extends JApplet{
        @Override
        public void init(){
            repaint();
        }
        @Override
        public void paint(Graphics g){
            g.drawString("Hello World",100,50);
        }
    }

    public static void main(String[] args) {
        new PullingAppletExample();
    }
}