Java中的简单问题

Java中的简单问题,java,methods,Java,Methods,谁能说我怎样才能解决这个问题 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:

谁能说我怎样才能解决这个问题

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);
    }

    Kuular k = new Kuular(); 
    k.fs();; // HERE
公共类Kuular实现ActionListener{
ImageIcon f1=新的ImageIcon(“C:\\Users\\Student\\Desktop\\pic1.jpg”);
ImageIcon f2=新的ImageIcon(“C:\\Users\\Student\\Desktop\\pic2.jpg”);
ImageIcon f3=新的ImageIcon(“C:\\Users\\Student\\Desktop\\pic3.jpg”);
ImageIcon f4=新的ImageIcon(“C:\\Users\\Student\\Desktop\\pic4.jpg”);
list1=数组.asList(f1,f2,f3,f4);
List list2=新的ArrayList();
公共财政司(){
收藏。洗牌(列表1);
}
Kuular k=新Kuular();
k、 fs();;//这里
如何调用方法fs? 谢谢!

添加
main()
方法并执行该类

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();
    }
}
公共类Kuular实现ActionListener{
ImageIcon f1=新的ImageIcon(“C:\\Users\\Student\\Desktop\\pic1.jpg”);
ImageIcon f2=新的ImageIcon(“C:\\Users\\Student\\Desktop\\pic2.jpg”);
ImageIcon f3=新的ImageIcon(“C:\\Users\\Student\\Desktop\\pic3.jpg”);
ImageIcon f4=新的ImageIcon(“C:\\Users\\Student\\Desktop\\pic4.jpg”);
list1=数组.asList(f1,f2,f3,f4);
List list2=新的ArrayList();
公共财政司(){
收藏。洗牌(列表1);
}
公共静态void main(字符串[]args){
Kuular k=新Kuular();
k、 fs();
}
}
添加一个
main()
方法并执行该类

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();
    }
}
公共类Kuular实现ActionListener{
ImageIcon f1=新的ImageIcon(“C:\\Users\\Student\\Desktop\\pic1.jpg”);
ImageIcon f2=新的ImageIcon(“C:\\Users\\Student\\Desktop\\pic2.jpg”);
ImageIcon f3=新的ImageIcon(“C:\\Users\\Student\\Desktop\\pic3.jpg”);
ImageIcon f4=新的ImageIcon(“C:\\Users\\Student\\Desktop\\pic4.jpg”);
list1=数组.asList(f1,f2,f3,f4);
List list2=新的ArrayList();
公共财政司(){
收藏。洗牌(列表1);
}
公共静态void main(字符串[]args){
Kuular k=新Kuular();
k、 fs();
}
}

在java中,只有字段初始化可以在方法之外进行。所有其他指令必须包含在方法中,而方法必须在类内部

问候,,
Stéphane

在java中,只有字段初始化可以发生在方法之外。所有其他指令必须包含在方法中,方法必须在类中

问候,, 圣菲