JMenu出现在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可以用,但我不知道该怎么用...

在此先感谢您的任何建议:)

赫米特

例如,您可以尝试

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

选择出现在JMenu上

来自分类Dev

弹出框出现在DialogFragment后面

来自分类Dev

Windows出现在面板后面

来自分类Dev

div出现在彼此后面

来自分类Dev

JMenu 栏未出现在 GUI 中

来自分类Dev

按钮未出现在JFrame中

来自分类Dev

按钮未出现在JFrame中

来自分类Dev

图像未出现在JFrame中

来自分类Dev

JButton 未出现在 JFrame 上

来自分类Dev

从出现在Windows中的文件资源管理器后面的BAT中启动Javaw JFrame

来自分类Dev

Google Maps:信息框出现在标记后面吗?

来自分类Dev

iOS普通tableview行出现在标题后面

来自分类Dev

::-ms-thumb出现在轨道后面

来自分类Dev

为什么此StaticLine出现在sizer后面?

来自分类Dev

Bootstrap Dropdown出现在其他元素后面

来自分类Dev

菜单栏出现在iframe后面

来自分类Dev

Google Maps:信息框出现在标记后面吗?

来自分类Dev

弹出窗口出现在DialogFragment后面

来自分类Dev

如何使挑逗出现在图像上而不是后面

来自分类Dev

使隐藏的滑动div出现在链接后面

来自分类Dev

固定的<nav>元素出现在某些页面元素后面

来自分类Dev

出现在父级后面的 HTML 子级

来自分类Dev

FullCalendar 出现在 z-index 较高的背景后面

来自分类Dev

怎么让conky出现在桌面文件夹后面?

来自分类Dev

日期选择器出现在模态类后面

来自分类Dev

弹出的窗口出现在活动窗口的后面吗?

来自分类Dev

图像不会出现在JFrame上

来自分类Dev

图像不会出现在JFrame上

来自分类Dev

MenuBar出现在JFrame的左侧。如何使其显示在JFrame的顶部?