public class r {
public static void main(String[]args){
windows a =new windows("窗口",100,100,600,600);//设置窗口
}
}
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class windows extends JFrame{
JMenuBar menubar;//创建菜单条
JMenu menuFruit;//创建菜单
JMenuItem bananaItem,pearItem;//创建菜单项
JMenu appleMenu;//创建菜单
JMenuItem redAppleItem,yellowAppleItem;//创建菜单项
public windows(String s,int x,int y,int w,int h){//传参
gongneng(s);
setLocation(x,y);//设置位置传参
setSize(w,h);//设置大小传参
setVisible(true);//设置是否可见
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//设置关闭按钮
}
void gongneng(String s){
setTitle(s);
menubar=new JMenuBar();
menuFruit=new JMenu("水果菜单");
bananaItem=new JMenuItem("香蕉");
bananaItem.setIcon(new ImageIcon("banana.jpg"));
pearItem=new JMenuItem("梨子");
pearItem.setIcon(new ImageIcon("pear.jpg"));
appleMenu=new JMenu("苹果");
redAppleItem=new JMenuItem("红苹果");
redAppleItem.setIcon(new ImageIcon("redApple.jpg"));
yellowAppleItem=new JMenuItem("黄苹果");
redAppleItem.setIcon(new ImageIcon("yellowApple.jpg"));
menuFruit.add(bananaItem);
menuFruit.add(pearItem);
menuFruit.addSeparator();
menuFruit.add(appleMenu);
appleMenu.add(redAppleItem);
appleMenu.add(yellowAppleItem);
menubar.add(menuFruit);
setJMenuBar(menubar);
}
}