需要帮助链接类 [英] Need help to link classes

查看:178
本文介绍了需要帮助链接类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所示,我需要帮助来链接类。

As the title suggests, I need help to link classes. I got one main class, and 4 classes which are going to pass to each other!

我的代码:

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:字符串到类2中的方法。

class1: pass a String to a method in 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: strong>从class1输出字符串,并传递一个String到class3

class2: Output String from class1, and pass a String to 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: from class2,并传递一个String到class4

class3: Output String from class2, and pass a String to 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,并且应该发送一个字符串回到class1。但我不能让它工作。

class4: Output String from class3, and are supposed to send a String back to class1. But I can't make it work.

一个问题是,我不能链接他们在mainclass,因为: class4 c4 = new class4(c1); 不工作, c1 不是简单地做,当class4需要它,所以这不是一个选项。 >

A problem is that I can't link them in mainclass, because this: class4 c4 = new class4(c1); doesn't work, c1 isn't simply made when class4 needs it, so that's not an option.

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

为了简单起见,我需要的只是能发送一个字符串回到class1。我需要一个更大的项目,所有的类都必须从主类打开。

To simple that, all I need is to being able to send a String back to class1. I need it for a bigger project, and all the classes have to get opened from the mainclass.

推荐答案

首先初始化所有类,然后设置它们可见。
如果你喜欢这个可能会工作:

I think if you should first initialize all the classes and afterwards to set them visible. If you do like this probably it will work:

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
}

这篇关于需要帮助链接类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆