【发布时间】:2015-03-30 08:08:46
【问题描述】:
我正在 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 语句中调用菜单,但不起作用。有没有办法强制它在上面?我听说使用 JPanel 可以工作,但我不知道如何工作...
提前感谢您的任何建议:)
【问题讨论】:
-
不是您的问题的答案,只是附注:您确定要使用小程序吗?这项技术“几乎死了”。在 2015 年仍然使用小程序的唯一原因 - 如果您的项目已经在使用它们;并且没有办法避免它们。在任何其他情况下......忘记它们。
-
那我应该用什么? Java Applet 和 Application 有很大区别吗?
-
小程序的主要目的是启用“Web 浏览器中的 Java 应用程序”。如果您的唯一目的是创建一个具有图形用户前端的 Java 应用程序;然后就去买一个 JFrame(见这里docs.oracle.com/javase/tutorial/uiswing/components/frame.html)。小程序和“普通”应用程序之间的主要区别只是可以在浏览器中运行小程序这一事实。如果你不需要它,你就不需要小程序。
-
我已经在使用 JFrame - 请参阅我的代码。但它把我的 JMenu 推到了后面。 编辑:我不在乎它是否在浏览器中运行。这只是一个教育项目。
标签: java eclipse applet jframe jmenu