1

我正在使用弹簧制作带有菜单栏的摆动框架,我的问题是,当我启动框架时,菜单栏根本没有出现,我将向您展示 beans.xml 和 bean 类,请告诉我为什么菜单栏没有出现,我有 4 个 bean 如下:

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;
   }
}

菜单豆:

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);
        }
    }
}

和框架豆:

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>

如果代码很长,谢谢和抱歉。

4

1 回答 1

0

setJMenuBar(myMenuBar); 而不是 this.add(myMenuBar);

于 2014-05-28T20:25:58.173 回答