该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
package huhu;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
public class MainFrame extends JFrame {
private MenuBar menubar;
public MainFrame(){
setSize(200,200);
setVisible(true);
setContentPane(getMenubar());
}
private MenuBar getMenubar(){
if(menubar==null){
menubar=new MenuBar();
}
return menubar;
}
public static void main(String[] args) {
new MainFrame();
}
}
package huhu;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class MenuBar extends JMenuBar{
private JMenu jmenu;
private JMenuItem item;
public MenuBar(){
add(getJmenu());
}
private JMenu getJmenu(){
if(jmenu==null){
jmenu=new JMenu("文件");
add(getItem(),null);
}
return jmenu;
}
private JMenuItem getItem(){
if(item==null){
item=new JMenuItem("打开");
}
return item;
}
}