开发者

easy problem in Java

Could anybody say how can I fix this problem?

public class Kuular implements ActionListener {

    ImageIcon f1 = new ImageIcon("C:\\Users\\Student\\Desktop\\pic1.jpg");
    ImageIcon f2 = new ImageIcon("C:\\Users\\Student\\Desktop\\pic2.jpg");
    ImageIcon f3 开发者_Python百科= new ImageIcon("C:\\Users\\Student\\Desktop\\pic3.jpg");
    ImageIcon f4 = new ImageIcon("C:\\Users\\Student\\Desktop\\pic4.jpg");

    List<ImageIcon> list1 = Arrays.asList(f1, f2, f3, f4);
    List<ImageIcon> list2 = new ArrayList<ImageIcon>(); 

    public void fs() {
        Collections.shuffle(list1);
    }

    Kuular k = new Kuular(); 
    k.fs();; // HERE

How can I call method fs? Thanks!


Add a main() method and execute the class

public class Kuular implements ActionListener {

    ImageIcon f1 = new ImageIcon("C:\\Users\\Student\\Desktop\\pic1.jpg");
    ImageIcon f2 = new ImageIcon("C:\\Users\\Student\\Desktop\\pic2.jpg");
    ImageIcon f3 = new ImageIcon("C:\\Users\\Student\\Desktop\\pic3.jpg");
    ImageIcon f4 = new ImageIcon("C:\\Users\\Student\\Desktop\\pic4.jpg");

    List<ImageIcon> list1 = Arrays.asList(f1, f2, f3, f4);
    List<ImageIcon> list2 = new ArrayList<ImageIcon>(); 

    public void fs() {
        Collections.shuffle(list1);
    }

    public static void main(String[] args) {
        Kuular k = new Kuular(); 
        k.fs();
    }
}


In java, only field initialization can occur outside of methods. All other instructions must be contained within methods, and methods must be inside classes.

Regards, Stéphane

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜