如何使方法 return 成为面板?

How to make method return a panel?

我正在制作一个带有选项卡式窗格的 Java 应用程序,我希望某些窗格具有相同的面板布局和结构,我不想一遍又一遍地编写相同的代码而使我的代码混乱,所以我创建了一个方法,该方法 returns 一个具有我希望窗格具有的结构的 JPanel。

我正在初始化新变量并将它们带到方法中。我的问题是,在创建面板后,我无法在其中执行任何其他操作,因为它没有显示。我无法添加标签等(尽管如果我在它显示的方法中添加标签)。

我的问题是,是否可以通过某种方式更改我编写的代码,以便在面板返回后进行更改?

    JPanel panel2 =  panel2(); // this code bit is in the constructor
    JPanel mainPanel = new JPanel();    //Variables needed to create a panel
    JPanel LeftPanel = new JPanel();
    JPanel RightPanel = new JPanel();
    JSplitPane splitPaneH = new JSplitPane();
    JPanel panelTop = new JPanel();
    JPanel panelBottom = new JPanel();

private JPanel panel2() {
        JPanel newPanel = new JPanel();
        CreateAPanel(newPanel, LeftPanel,RightPanel,splitPaneH, panelTop,panelBottom);
        JLabel label = new JLabel ("lalala");
        LeftPanel.add(label,BorderLayout.CENTER);

        return newPanel;

}

private JPanel CreateAPanel(JPanel mainPanel, JPanel LeftPanel,JPanel RightPanel, JSplitPane splitPaneH, JPanel panelTop, JPanel panelBottom){
        mainPanel.setPreferredSize(new Dimension(1100, 630));
        mainPanel.setLayout(new BorderLayout());

        LeftPanel = new JPanel();
        RightPanel = new JPanel();
        splitPaneH = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
        panelTop = new JPanel();
        panelBottom = new JPanel();

        splitPaneH.setTopComponent(panelTop);
        splitPaneH.setBottomComponent(panelBottom);
        splitPaneH.setDividerLocation(300);
        splitPaneH.setPreferredSize(new Dimension(800,630));

        mainPanel.add(LeftPanel, BorderLayout.WEST);
        mainPanel.add(RightPanel,BorderLayout.EAST);
        LeftPanel.setBackground(Color.RED);

        LeftPanel.setPreferredSize(new Dimension (300,630));
        RightPanel.add(splitPaneH);

        return mainPanel;
}

您没有使用您的 return 值...

您的方法CreateAPanel(...)创建了所需的面板,但您只是不使用它

你应该这样调整你的方法panel2():

private JPanel panel2() 
{
    //JPanel newPanel = new JPanel(); don't create a new panel!
    //CreateAPanel(newPanel, LeftPanel,RightPanel,splitPaneH, panelTop,panelBottom);
    //instead do this:
    JPanel newPanel = CreateAPanel(newPanel, LeftPanel,RightPanel,splitPaneH, panelTop,panelBottom);
    JLabel label = new JLabel ("lalala");
    LeftPanel.add(label,BorderLayout.CENTER);
    return newPanel;
}

之后完全可以向 Panel 对象添加组件。你犯的唯一错误是 "inside the method body you create new JPanel instances to replace with original param references" 所以当方法 returns 对原始对象没有影响。我建议做一些不同的事情:

private JPanel[] CreateAPanel(JPanel mainPanel)
    {
        mainPanel.setPreferredSize(new Dimension(1100, 630));
        mainPanel.setLayout(new BorderLayout());

        JPanel leftPanel = new JPanel();
        JPanel rightPanel = new JPanel();
        JSplitPane splitPaneH = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
        JPanel panelTop = new JPanel();
        JPanel panelBottom = new JPanel();

        splitPaneH.setTopComponent(panelTop);
        splitPaneH.setBottomComponent(panelBottom);
        splitPaneH.setDividerLocation(300);
        splitPaneH.setPreferredSize(new Dimension(800,630));

        mainPanel.add(leftPanel, BorderLayout.WEST);
        mainPanel.add(rightPanel,BorderLayout.EAST);
        leftPanel.setBackground(Color.RED);

        leftPanel.setPreferredSize(new Dimension (300,630));
        rightPanel.add(splitPaneH);

        return new JPanel[]{mainPanel, leftPanel, rightPanel, panelTop, panelBottom};
    }

如果您想在结果 JPanel 中更改或添加更多组件,您可以在创建它们时为所有组件设置名称:

JPanel newPanel = new JPanel();
newPanel .setName("leftPanel");
resultPanel.add(newPanel, BorderLayout.WEST);

然后当您得到 resultPanel 时,您可以得到它的组件:

Component[] componentList = resultPanel.getContentPane().getComponents();
JPanel leftPanel = null;
for (Component component: componentList) {
    if (Objects.equals(component.getName(), "leftPanel")) {
        leftPanel = (JPanel) component;
    }
}

if (leftPanel != null) {
    // do something
}