JMenu出现在JFrame背后

时间:2015-03-30 08:08:47

标签: java eclipse applet jframe jmenu

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

像这样: enter image description here

这是它所称的地方:

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的作品,但我不知道如何工作......

提前感谢任何建议:)

2 个答案:

答案 0 :(得分:2)

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

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();
    }
}

答案 1 :(得分:0)

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