Java JMenu出现在JFrame后面
我正在用EclipseIDE制作一个Java小程序。我有一个JMenu和一个JFrame。由于某种原因,菜单出现在JFrame后面 像这样: 这就是它的名称:Java JMenu出现在JFrame后面,java,eclipse,applet,jframe,jmenu,Java,Eclipse,Applet,Jframe,Jmenu,我正在用EclipseIDE制作一个Java小程序。我有一个JMenu和一个JFrame。由于某种原因,菜单出现在JFrame后面 像这样: 这就是它的名称: public Main() throws Exception{ JFrame i = new JFrame("My Applet"); JLabel label = new JLabel(); ImageIcon icon = new ImageIcon(new URL( "lo
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);
}
}
public Main()引发异常{
JFrame i=新JFrame(“我的小程序”);
JLabel标签=新的JLabel();
ImageIcon图标=新的ImageIcon(新URL(
“loader.gif”);
icon.setImageObserver(空);
label.setIcon(图标);
i、 挫折背景(颜色:黑色);
JMenuBar menuBar=新的JMenuBar();
i、 setJMenuBar(菜单栏);
JMenu accountMenu=新JMenu(“账户”);
JMenu toolsMenu=新JMenu(“工具”);
JMenu选项菜单=新JMenu(“选项”);
菜单栏。添加(帐户菜单);
菜单栏添加(工具菜单);
菜单栏。添加(选项菜单);
JMenuItem AccountDeAction=新的JMenuItem(“账户详细信息”);
JMenuItem exitAction=新的JMenuItem(“退出”);
accountMenu.add(AccountDeAction);
optionsMenu.addSeparator();
选项菜单。添加(退出);
ButtonGroup bg=新建ButtonGroup();
AccountDeAction.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件arg0){
系统输出打印(“测试”);
}
});
i、 添加(标签);
i、 包装();
i、 setVisible(真);
String str=getPageSource(新URL(“我的URL链接在这里”);
Pattern=Pattern.compile(“gamep\\w+”);
Matcher match=pattern.Matcher(str);
if(match.find()){
Main stub=newmain(Pattern.compile(“”),str);
下载(“我的url链接在这里”,match.group(0)+“jar”);
setCodeBase(新URL(“我的URL链接在这里”+match.group(0)+.jar”);
setDocumentBase(新URL(“我的URL链接在这里”+match.group(0)+“jar”);
stub.getParameter(parameters.get(“java_参数”));
URLClassLoader classLoader=newurlclassloader(newurl[]{newurl(“file:thejar.jar”)});
Applet Applet=(Applet)classLoader.loadClass(“客户端”).newInstance();
applet.setStub(存根);
setPreferredSize(新维度(1000500));
applet.init();
applet.start();
i、 setJMenuBar(菜单栏);
i、 添加(applet);
i、 包装();
i、 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
标签可见(假);
}
}
我也尝试过调用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();
}
}
这不是对你问题的回答,只是一个旁注:你确定要使用applet吗?这项技术“几乎已经死了”。2015年仍然使用applet的唯一原因——如果您的项目已经在使用applet;没有办法避免它们。在任何其他情况下。。。忘了它们吧。那我该用什么呢?Java小程序和应用程序有很大区别吗?小程序的主要目的是启用“web浏览器中的Java应用程序”。如果您的唯一目的是创建一个具有图形用户前端的java应用程序;然后只需使用JFrame(参见此处)。小程序和“普通”应用程序之间的主要区别在于可以在浏览器中运行小程序。如果你不需要它,你也不需要小程序。我已经在使用JFrame了-请看我的代码。但它把我的JMenu推到了后面。。编辑:我不在乎它是否在tbh浏览器中运行。这只是一个教育项目。我正在从网上拉一个小程序。所以,你想显示小程序。可以尝试i.setContentPane(小程序);而不是i.add(applet);在您的代码中:)我认为代码中真正的问题是,由于Applet是一个重量级容器,JMenu是一个轻量级swing容器,所以重量级容器呈现在轻量级容器之上。试试我刚才说的话,让我知道。:)您的setContentPane解决方案工作得非常好!谢谢你!