Java - FlowPanel - 使用来自子项的父变量

时间:2010-11-10 16:46:09

标签: java

public class PageIndex extends FlowPanel {
    private PageHeader header;
    private PageCenter center;
    private PageFooter footer;
    public PageIndex() {
        this.header=new PageHeader();
        this.add(header);

        this.center=new PageCenter();
        this.add(center);

        this.footer=new PageFooter();
        this.add(footer);
   }
}

public class PageCenter extends FlowPanel {
    private PageMenu menu;
    private PageContent content;

    public PageCenter() {
       this.setStyle("center");

       this.menu=new PageMenu(content);
       this.add(menu);

       this.content=new PageContent();
       this.add(content);
   }
}

public class PageMenu extends FlowPanel {
    private PageContent content;
    private PageMenuLogin login;
    private PageMenuSearch search;
    private PageMenuOffers offers;
    private PageMenuStudents students;
    private PageMenuShopping shopping;
    private PageMenuEvents events;
    public PageMenu (PageContent content) {
        this.content=content;
        this.login=new PageMenuLogin();
        this.add(login);
        this.search=new PageMenuSearch();
        this.add(search);
        this.offers=new PageMenuOffers();
        this.add(offers);
        this.students=new PageMenuStudents();
        this.add(students);
        this.shopping=new PageMenuShopping();
        this.add(shopping);
        this.events=new PageMenuEvents();
        this.add(events);

        // IF I WRITE THIS I GET AN org.apache.jasper.JasperException: java.lang.NullPointerException
        this.content.daghine();
    }
}

我需要管理内容实例,但不可见。对不起,我有点生疏了:))

2 个答案:

答案 0 :(得分:1)

您如何管理变量?你在对象内,应该允许你做任何你需要的事情。如果需要访问父对象,请执行以下操作:

public class PageIndex extends FlowPanel {
    protected final PageContent content;
    ...

    public PageIndex() {
        content = new PageContent(this);
        this.add(content);
        ...
    }

    public PageContent getContent() {
        return this.content;
    }
}

public class PageContent extends FlowPanel {
    protected final PageIndex index;

    public PageContent(PageIndex index) {
        this.index = index;
    }

    public PageIndex getIndex() {
        return this.index;
    }

    public void exampleFunction() {
        createLabel().setLabel("HOLAAA").setStyle("content_title");
    }
}

public class PageMenuLogin extends FlowPanel {
    protected final PageIndex index;

    public PageContent(PageIndex index) {
        this.index = index;
        index.getContent().exampleFunction();
    }

    public PageIndex getIndex() {
        return this.index;
    }
}

答案 1 :(得分:1)

如果您需要从PageContent方法外部访问PageHeader,您可以简单地将标头变量放在PageContent类上。

以下内容应该让你开始......

public class PageContent extends .... {
    private PageHeader header;
    public PageContent() {
       this.header = new PageHeader()
    }

    // add setHeader/getHeader methods here.

}