这是我的原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("香蕉"); bananaItem.setIcon(new ImageIcon("pic/banana.jpg")); pearItem = new JMenuItem("甜梨"); pearItem.setIcon(new ImageIcon("pic/pear.jpg")); appleMenu = new JMenu("苹果"); redAppleItem = new JMenuItem("红苹果"); redAppleItem.setIcon(new ImageIcon("pic/redApple.jpg")); yellowAppleItem = new JMenuItem("黄苹果"); yellowAppleItem.setIcon(new ImageIcon("pic/yellowAppleItem.jpg")); greenAppleItem = new JMenuItem("绿苹果"); greenAppleItem.setIcon(new ImageIcon("pic/greenAppleItem.jpg")); menuYoghourt = new JMenu("酸奶菜单"); heavyItem = new JMenuItem("厚酸奶"); heavyItem.setIcon(new ImageIcon("pic/heavyYoghourt.jpg")); thinnerItem = new JMenuItem("轻酸奶"); thinnerItem.setIcon(new ImageIcon("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); }
时间: 2024-03-23 18:36:30 浏览: 171
Python删除Java源文件中全部注释的实现方法
首先,我们需要将原来的图片文件名作为参数传入该程序中的中,而不是直接将文件名硬编码在程序中。这样可以方便扩展和修改。
我们可以在程序中添加一个方法,用于设置菜单项的图标,并将图标文件名作为参数传入该方法:
```java
void setMenuItemIcon(JMenuItem item, String iconFileName) {
item.setIcon(new ImageIcon(iconFileName));
}
```
然后,我们可以在`init`方法中调用该方法,将图标文件名作为参数传入。修改后的`init`方法如下:
```java
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);
}
```
这样,我们就完成了对源文件中设置图片的方法进行替换的任务。
阅读全文