我正在使用弹簧制作带有菜单栏的秋千框架,我的问题是,当我启动框架时,没有菜单栏根本没有出现,我将向您展示beans.xml和bean类,请告诉我我为什么没有出现菜单栏,我有4个豆类如下:
MenuItem bean:
public class MyMenuItem extends JMenuItem {
private ArrayList<String> menuItemNames;
public MyMenuItem() { }
public ArrayList<String> getMenuItemNames() {
return menuItemNames;
}
public void setMenuItemNames(ArrayList<String> menuItemNames) {
this.menuItemNames = menuItemNames;
}
}
菜单bean:
public class MyMenu extends JMenu {
private static final long serialVersionUID = 1L;
private MyMenuItem myMenuItem;
private ArrayList<String> menuNames;
private MyMenuItem menuItem;
public MyMenu() {}
@PostConstruct
public void init() {
for (String menuItemName : myMenuItem.getMenuItemNames()) {
menuItem = new MyMenuItem();
menuItem.setText(menuItemName);
this.add(menuItem);
}
}
public MyMenuItem getMyMenuItem() {
return myMenuItem;
}
public void setMyMenuItem(MyMenuItem myMenuItem) {
this.myMenuItem = myMenuItem;
}
public ArrayList<String> getMenuNames() {
return menuNames;
}
public void setMenuNames(ArrayList<String> menuNames) {
this.menuNames = menuNames;
}
}
MenuBar bean:
public class MyMenuBar extends JMenuBar {
private static final long serialVersionUID = 1L;
private MyMenu myMenu;
public MyMenu getMyMenu() {
return myMenu;
}
public void setMyMenu(MyMenu myMenu) {
this.myMenu = myMenu;
}
public MyMenuBar() {
}
@PostConstruct
public void init() {
for (String menu : myMenu.getMenuNames()) {
MyMenu mm = new MyMenu();
mm.setText(menu);
this.add(mm);
}
}
}
和Frame bean:
public class MainFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private MyMenuBar myMenuBar;
@PostConstruct
public void init(){
this.add(myMenuBar);
}
public MainFrame() {
}
public MyMenuBar getMyMenuBar() {
return myMenuBar;
}
public void setMyMenuBar(MyMenuBar myMenuBar) {
this.myMenuBar = myMenuBar;
}
}
这是我的bean配置文件beans.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<bean id="menuItem" class="com.spring.frame.MyMenuItem" init-method="init">
<property name="menuItemNames">
<bean class="org.springframework.beans.factory.config.ListFactoryBean">
<property name="targetListClass">
<value>java.util.ArrayList</value>
</property>
<property name="sourceList">
<list>
<value>Copy</value>
<value>Paste</value>
<value>Cut</value>
</list>
</property>
</bean>
</property>
</bean>
<bean id="menu" class="com.spring.frame.MyMenu" init-method="init">
<property name="myMenuItem">
<ref bean="menuItem"></ref>
</property>
<property name="menuNames">
<bean class="org.springframework.beans.factory.config.ListFactoryBean">
<property name="targetListClass">
<value>java.util.ArrayList</value>
</property>
<property name="sourceList">
<list>
<value>File</value>
<value>Edit</value>
</list>
</property>
</bean>
</property>
</bean>
<bean id="menuBar" class="com.spring.frame.MyMenuBar" init-method="init">
<property name="myMenu">
<ref bean="menu" />
</property>
</bean>
<bean id="frame" class="com.spring.frame.MainFrame" init-method="init">
<property name="myMenuBar">
<ref bean="menuBar"></ref>
</property>
</bean>
感谢并且抱歉,如果代码很长。
答案 0 :(得分:0)
<强> setJMenuBar(myMenuBar); 强> 代替 this.add(myMenuBar);