开发者

easy problem in Java

开发者 https://www.devze.com 2023-03-15 23:02 出处:网络
Could anybody say how can I fix this problem? public class Kuular implements ActionListener { ImageIcon f1 = new ImageIcon(\"C:\\\\Users\\\\Student\\\\Desktop\\\\pic1.jpg\");

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

精彩评论

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

关注公众号