我得到了错误,就像在主题中一样,我恳请你如何修复它... ERROR在menuItem-loop中,我尝试将textArea前景色设置为从menuItem中拾取的颜色:( colors [mi])
String[] colors = {
"blue",
"yellow",
"orange",
"red",
"white",
"black",
"green",
};
JMenu mnForeground = new JMenu("Foreground");
for (int mi=0; mi<colors.length; mi++){
String pos = Character.toUpperCase(colors[mi].charAt(0)) + colors[mi].substring(1);
JMenuItem Jmi =new JMenuItem(pos);
Jmi.setIcon(new IconA(colors[mi]));
Jmi.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JMenuItem item = (JMenuItem) e.getSource();
IconA icon = (IconA) item.getIcon();
Color kolorIkony = getColour(colors[mi]); // ERROR HERE: (colors[mi])
textArea.setForeground(kolorIkony);
}
});
mnForeground.add(Jmi);
}
public Color getColour(String colour){
try {
kolor = Color.decode(colour);
} …Run Code Online (Sandbox Code Playgroud) 我想到了我的学校课程的算法,但我猜想一些基础知识的难度......
这是我的问题代码:
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws FileNotFoundException {
String allWords = System.getProperty("user.home") + "/allwords.txt";
Anagrams an = new Anagrams(allWords);
for(List<String> wlist : an.getSortedByAnQty()) {
//[..............];
}
}
}
public class Anagrams {
List<String> myList = new ArrayList<String>();
public List<String> getSortedByAnQty() {
myList.add("aaa");
return myList;
}
}
Run Code Online (Sandbox Code Playgroud)
我得到"类型不匹配:无法从元素类型字符串转换为列表"如何初始化getSortedByAnQty()对吗?
我想为每个颜色列表添加菜单项行,但是有类似的错误
"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)
我该怎么办?