考虑到该文件中需要导包等其他操作,请把整个源文件发给我,方便我直接复制
时间: 2024-03-23 19:36:31 浏览: 48
源文件
好的,下面是修改后的源文件:
```java
import javax.swing.*;
public class WindowMenu extends JFrame {
JMenuBar menubar;
JMenu menuFruit;
JMenuItem bananaItem, pearItem;
JMenu menuYoghourt;
JMenuItem heavyItem, thinnerItem;
JMenu appleMenu;
JMenuItem redAppleItem,yellowAppleItem,greenAppleItem;
public WindowMenu(){}
public WindowMenu(String s, int x, int y, int w, int h) {
init(s);
setLocation(x, y);
setSize(w, h);
setVisible(true);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
void init(String s) {
setTitle(s);
menubar = new JMenuBar();
menuFruit = new JMenu("水果菜单");
bananaItem = new JMenuItem("香蕉");
setMenuItemIcon(bananaItem, "pic/banana.jpg");
pearItem = new JMenuItem("甜梨");
setMenuItemIcon(pearItem, "pic/pear.jpg");
appleMenu = new JMenu("苹果");
redAppleItem = new JMenuItem("红苹果");
setMenuItemIcon(redAppleItem, "pic/redApple.jpg");
yellowAppleItem = new JMenuItem("黄苹果");
setMenuItemIcon(yellowAppleItem, "pic/yellowApple.jpg");
greenAppleItem = new JMenuItem("绿苹果");
setMenuItemIcon(greenAppleItem, "pic/greenApple.jpg");
menuYoghourt = new JMenu("酸奶菜单");
heavyItem = new JMenuItem("厚酸奶");
setMenuItemIcon(heavyItem, "pic/heavyYoghourt.jpg");
thinnerItem = new JMenuItem("轻酸奶");
setMenuItemIcon(thinnerItem, "pic/thinnerYoghourt.jpg");
menuFruit.add(bananaItem);
menuFruit.add(pearItem);
menuFruit.addSeparator();
menuFruit.add(appleMenu);
appleMenu.add(redAppleItem);
appleMenu.add(yellowAppleItem);
appleMenu.add(greenAppleItem);
menuYoghourt.add(heavyItem);
menuYoghourt.add(thinnerItem);
menubar.add(menuFruit);
menubar.add(menuYoghourt);
setJMenuBar(menubar);
}
void setMenuItemIcon(JMenuItem item, String iconFileName) {
item.setIcon(new ImageIcon(iconFileName));
}
}
```
注意:在运行该程序时,需要确保`pic`文件夹下的图片文件存在,并且程序能够读取到这些文件。
阅读全文