[英]Why isn't my 'Menu' bar displaying in my JFrame 'Window' Object?
Here I've created the JFrame object, 'window'. 在这里,我创建了JFrame对象“ window”。 And set it to 'true'. 并将其设置为“ true”。
import java.awt.*;
public static void main(String[] args) {
JFrame window = new JFrame("GUI Test");
window.setSize(250, 100);
window.setLocation(100, 100);
window.setVisible(true);
Here I've added menu items to the 'menu'. 在这里,我已将菜单项添加到“菜单”中。 And set visibility as 'true'. 并将可见性设置为“ true”。
JMenuItem home = new JMenuItem("Home");
JMenuItem about = new JMenuItem("About");
JMenuItem tag = new JMenuItem("Tag");
JMenu menu = new JMenu("menu");
menu.add(home);
menu.add(about);
menu.add(tag);
menu.setVisible(true);
}
I'm not getting any errors. 我没有任何错误。 Then what am I missing? 那我想念什么呢? Why doesn't my menu display in the 'window' object? 为什么菜单没有显示在“窗口”对象中?
You need to add your JMenu
s to a JMenuBar
and set to to the JFrame
using JFrame#setJMenuBar
您需要添加JMenu
到A S JMenuBar
并设置到JFrame
使用JFrame#setJMenuBar
See How to use Menus for more details 有关更多详细信息,请参见如何使用菜单 。
Frirst creat JMenubar and add the JMenu into that. Frirst创建JMenubar并将JMenu添加到其中。
JMenuBar menubar = new JMenuBar();
Add the JMenu into JMenuBar and JMenuar into JFrame to display in the window. 将JMenu添加到JMenuBar中,将JMenuar添加到JFrame中,以在窗口中显示。
menubar.add(menu);
window.setJMenuBar(menubar);
You need to add JMenuBar
with window, 您需要添加带有窗口的JMenuBar
,
JMenuBar menuBar= new JMenuBar();
.....
menuBar.add(menu);//Add menu with JMenuBar.
window.setJMenuBar(menuBar);
Add the Jmenu
into in you JFrame
将Jmenu
添加到您的JFrame
window.add(menu);
and also set the layout to JFrame
并将布局设置为JFrame
window.setLayout(new Flawlayout()); // you can try other layout too
And best way is 最好的方法是
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.