需要帮助链接类

时间:2014-01-19 19:03:45

标签: java class methods

正如标题所示,我需要帮助来链接课程。我有一个主要课程,4个课程将相互传递!

我的代码:

Mainclass:运行所有4个类

public class main {
    public static void main(String[] args){ 
        class4 c4 = new class4();
        class3 c3 = new class3(c4);
        class2 c2 = new class2(c3);

        c2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        c2.setSize(200,100);
        c2.setLocationRelativeTo(null);
        c2.setVisible(true);

        class1 c1 = new class1(c2);
        c1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        c1.setSize(200,100);
        c1.setLocationRelativeTo(null);
        c1.setVisible(true);
    }
}

class1:将String传递给class2中的方法。

public class class1 extends JFrame{
    private JButton jb;

    private class4 c4;
    private class2 c2;

    public class1(class2 c2){
        this();
        this.c2 = c2;
    }

    public class1(){
        super("");
        setLayout(new FlowLayout());

        jb = new JButton("click click");
        add(jb);

        jb.addActionListener(
                new ActionListener(){
                    public void actionPerformed(ActionEvent e){
                        sayHi("Hi buddy");
                    }});
    }

    public void sayHi(String x){
        c2.recieveHi(x);
    }

    public void recieveHi(String x){
        System.out.println(x);
    }

}

class2:从class1输出字符串,并将String传递给class3

public class class2 extends JFrame{
    private JTextField jt;

    private class3 c3;

    public class2(class3 c3){
        this();
        this.c3 = c3;
    }

    public class2(){
        super("Yeds");
        setLayout(new FlowLayout());

        jt = new JTextField(12);
        add(jt);
        //recieveHi("hey");
    }

    public void recieveHi(String x){
        String j = x;
        jt.setText(j);
        sayHi();
    }

    public void sayHi(){
        c3.recieveHi("Hey class3");
    }
}

class3:从class2输出字符串,并将String传递给class4

public class class3 {
    Scanner scan = new Scanner(System.in);

    private class4 c4;
    public class3(class4 c4){
        //this();
        this.c4 = c4;
        }

    public void recieveHi(String x){
        System.out.println(x);
        sayHi();

        //sayHi();
    }
    public void sayHi(){
        System.out.println();
        c4.recieveHi("Hey class4");
    }
}

class4:从class3输出String,并且应该将String发送回class1。但我不能让它发挥作用。

一个问题是我无法在mainclass中链接它们,因为:class4 c4 = new class4(c1);不起作用,c1不是简单地在class4需要时创建的,所以这不是一个选项。

    public class class4 {
    private class1 c1;

    //this are supposed to work, but doesnt...
    public void setClass1Object(class1 pC1) {
        this.c1 = pC1;
    }

    public void recieveHi(String x){
        System.out.println(x);
        killMessenger();
        setClass1Object(c1);
    }

    public void killMessenger(){
        String s = "back to class 1";
        c1.recieveHi(s);
    }
}

简单来说,我需要的是能够将String发送回class1。我需要它用于更大的项目,并且所有类都必须从主类开放。

1 个答案:

答案 0 :(得分:0)

我认为如果您首先应该初始化所有类,然后将它们设置为可见。 如果你喜欢这样,它可能会起作用:

public static void main(String[] args){ 
  class4 c4 = new class4();
  class3 c3 = new class3(c4);
  class2 c2 = new class2(c3);
  class1 c1 = new class1(c2);
  c4.setClass1Object(c1);
  // here make all the setters like setSize, setVisible etc
}