小编mal*_*orn的帖子

Java:在封闭范围内定义的局部变量mi必须是最终的或有效的最终

我得到了错误,就像在主题中一样,我恳请你如何修复它... 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)

java

18
推荐指数
3
解决办法
8万
查看次数

类型不匹配:从String转换为List <String>

我想到了我的学校课程的算法,但我猜想一些基础知识的难度......

这是我的问题代码:

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()对吗?

java

4
推荐指数
1
解决办法
3090
查看次数

循环中的Java JMenuItem

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

"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)

我该怎么办?

java swing

1
推荐指数
1
解决办法
166
查看次数

标签 统计

java ×3

swing ×1