如何使我的JAVA方法链接正常工作?

时间:2018-12-18 05:38:19

标签: java method-chaining

我正在尝试做一些简单的方法链接,但始终会出现部分“找不到符号”错误。 例如:

public JButton[] getSignOnButtons() {
    return InitialFrame.getInitialPanel().getSignOnButtons();
}

我正在实现MVC模型,在View包中,我有4个类:View,InitialFrame,InitialPanel,NorthPanel。为了使我的Controller与View包通信,我总是经过View类。

我的控制器类需要访问View的类的属性,最好的方法是什么?

我以前通过公开所有View类的属性来“欺骗”它,所以我只能从view创建一个“ get”方法 例如

return InitialFrame.InitialPanel.Buttons;

感谢您的帮助。

每种情况下,错误仅表示“找不到符号”。

**从现在开始编辑……

这是整个视图包:

public class View {

    InitialFrame initialFrame;

    public View(){

        initialFrame = new InitialFrame();

    }

    public JFrame getInitialFrame() {
        return initialFrame;
    }

    public InitialPanel getInitialPanel() {
       return InitialFrame.getInitialPanel();
    }

    public JButton[] getSignOnButtons() {
        return initialFrame.getInitialPanel().getSignOnButtons();
    }
}

这是InitialFrame类:

public final class InitialFrame extends JFrame {

    private final InitialPanel initialPanel;

    public InitialFrame() {

        super("Welcome to the Sign-on Screen");
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        this.setSize(700, 700);
        this.setLayout(new GridLayout(1, 1));

        initialPanel = new InitialPanel();        
        this.add(initialPanel);

        //this.pack();
        this.setLocationRelativeTo(null);
        this.validate();
        this.repaint();
        this.setVisible(true);

        JButton[] test = initialPanel.getSignOnButtons();
        String newStr = initialPanel.getNorthPanel().getTest();    //Call to getTest
    }

    public InitialPanel getInitialPanel() {
        return initialPanel;
    }


}

// InitialPanel ___________

class InitialPanel extends JPanel{

    private BorderLayout InitialPanelLayout;
    private JButton[] signOnButtons;
    private NorthPanel northPanel;
    private JPanel southPanel;
    private JPanel leftPanel;
    private JPanel rightPanel;
    private JPanel centerPanel;
    private JLabel userNameLabel;
    private JTextField userNameTextField;

    public InitialPanel() {

        this.setSize(600, 600);
        InitialPanelLayout = new BorderLayout();
        this.setLayout(InitialPanelLayout);

        this.createPanels();
        this.formatCenterPanel();


        setVisible(true);
        this.validate();
        this.repaint();
    }

    /**
     * Method is to create panels for all the Border Layout of initial Panel
     * @param none
     */
    private void createPanels() {

        //Graphics comp2D = new Graphics();
        //comp2D.drawString("Free the bound periodicals", 22, 100);

        northPanel = new NorthPanel();
        northPanel.setSize(600, 200);
        this.add(northPanel, "North");

        southPanel = new JPanel();
        this.add(southPanel, "South");

        leftPanel = new JPanel();
        this.add(leftPanel, BEFORE_LINE_BEGINS);

        rightPanel = new JPanel();
        this.add(rightPanel, AFTER_LINE_ENDS);

        centerPanel = new JPanel();
        this.add(centerPanel, "Center");

    }

    /**
     * Method is to format the center panel on the opening window.
     * It uses 4 row grid layout, top row is a container with Label and TextField.
     * @param none 
     */
    private void formatCenterPanel() {

        centerPanel.setLayout(new GridLayout(5, 1));
        Container userName = new Container();
        userName.setLayout(new GridLayout(1, 2));
        userNameLabel = new JLabel("UserName: ");
        userName.add(userNameLabel);
        userNameTextField = new JTextField(30);
        userName.add(userNameTextField);

        centerPanel.add(userName);

        signOnButtons = new JButton[3];
        signOnButtons[0] = new JButton("Sign-On");
        signOnButtons[1] = new JButton("Register");
        signOnButtons[2] = new JButton("Exit");

        for (JButton butt: signOnButtons) {
            centerPanel.add(butt);
        }    
        centerPanel.validate();
        centerPanel.repaint();
    }

    public JButton[] getSignOnButtons() {
        return signOnButtons;
    }

    public JTextField getUserNameTextField() {
        return userNameTextField;
    }

    public NorthPanel getNorthPanel() {
        return northPanel;
    }
}

现在全部更新...

仅剩一个错误:“不能从静态上下文中引用非静态方法getInitialPanel()”

在观看课程中

public InitialPanel getInitialPanel() {
   return InitialFrame.getInitialPanel();
}

最终编辑: 主要解决方案是使用“ this”关键字。 之后,我可以使用Controller链接3种或更多方法来检索隐藏在View包中的属性。

例如,在“视图类”中:

public JButton[] getSignOnButtons() {
    return this.initialFrame.getInitialPanel().getSignOnButtons();
}

**编辑2018年12月25日 this.keyword不能每次都解决这个问题。这仍然是一个棘手的操作。 有时我只是允许NetBeans自己创建方法,因为它说即使命名完全相同也找不到方法。

3 个答案:

答案 0 :(得分:1)

方法链接从左到右的工作方式是,左方法返回的值必须在类中实现右方法;

例如;

MyObject

请注意,这里" Hello ".substring(1).trim()返回的是substring(1)值,该值在其(String)类中也实现了trim()方法。

在您的情况下; String

您正在从return InitialFrame.getInitialPanel().getSignOnButtons();方法返回Initialframe,但是getInitialPlane()类中没有getSignOnButtons()的实现。这就是JVM抱怨的原因。

答案 1 :(得分:0)

getSignOnButtons()中没有方法JPanel。也许在InitialPanel中?如果是这种情况,则InitialFrame.getInitialPanel()的返回类型应该是InitialPanel,而不是JPanel

答案 2 :(得分:0)

之所以发生这种情况,是因为您试图执行JFrame类中不存在的方法。声明此JFrame InitialFrame;时,Java会在编译时检查JFrame类中是否存在该方法。由于JFrame中不存在该错误,因此您似乎会遇到“找不到符号”错误。

此外,请尝试遵循代码标准-字段名称应使用小写驼峰字母(即intialFrame)。

尝试以下代码(尽管我尚未编译)-

public class View {

    private InitialFrame initialFrame;

    public View(){

        initialFrame = new InitialFrame();

    }

    public JFrame getInitialFrame() {
        return this.initialFrame;
    }

    public JPanel getInitialPanel() {
        return this.initialFrame.getInitialPanel();
    }

    public JButton[] getSignOnButtons() {
        return this.initialFrame.getInitialPanel().getSignOnButtons(); 
    }
}

public final class InitialFrame extends JFrame {

    private InitialPanel initialPanel;

    public InitialFrame() {

        super("Welcome to the Sign-on Screen");
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        this.setSize(700, 700);
        this.setLayout(new GridLayout(1, 1));

        initialPanel = new InitialPanel();        
        this.add(initialPanel);

        //this.pack();
        this.setLocationRelativeTo(null);
        this.validate();
        this.repaint();
        this.setVisible(true);

        JButton[] test = initialPanel.getSignOnButtons();
        String new = initialPanel.getNorthPanel().getTest(); 
    } 

    public JPanel getInitialPanel() {
        return this.initialPanel;
    }


}

我假设您还有另一个InitialPanel类,该类正在扩展JPanel并在其中定义了getSignOnButtons()方法。