循环中的Java JMenuItem

mal*_*orn 1 java swing

我想为每个颜色列表添加菜单项行,但是有类似的错误

"at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)"

在我的循环中添加JMenuItem:

String[] colors = {
        "Blue", 
        "Yellow",
        "Orange",
        "Red", 
        "White", 
        "Black", 
        "Green", 
};

JMenuItem menuItem;
JMenu mnBackground = new JMenu("Background");
for (int mi=0; mi<=colors.length; mi++){
    String pos = colors[mi];
    JMenuItem Jmi =new JMenuItem(pos); // ERROR, though manually added Strings works...
    mnBackground.add(Jmi);
}
Run Code Online (Sandbox Code Playgroud)

我该怎么办?

Abd*_*hak 8

循环应该是:

for (int mi = 0; mi < colors.length; mi++)

for (int mi = 0; mi <= colors.length; mi++)