面板应该是框架内的字段吗?

Should the panels be fields inside the frame?

我创建了一个框架,里面有近 20 个面板(所有面板都有不同的种类和特性),只需创建它们并将它们添加到内容面板并记住他的订单号即可访问它们。

但现在我正在尝试从源代码生成一个 UML 图,我注意到我在框架内没有任何对此面板的引用。我是否应该创建 20 个现场面板并一一引用它们?还是刚刚好?我不知道正确的方法,谢谢!

P.S:我不知道这是否重要,但我的语言是 Java

您可以将面板存储在 JPanel[] 数组中,以便您可以随时调用它们。

import java.awt.Color;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class TestClass {
    static JFrame frame = new JFrame();
    static JPanel[] panel_order = new JPanel[20]; // 20 the number of panels you have

//generating the panels
public static void main(String[] args){     
    frame.setBounds(0, 0, 500, 400);

    for(int i=0;i<panel_order.length;i++){
        JPanel panel = new JPanel();
        // you can set the panel's location like:
        panel.setBounds(20*i, 0, 80, 50);
        //and border if you are making diagrams
        panel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        panel_order[i] = panel;
        frame.add(panel);
    }
frame.setVisible(true);
    //now you can access your panels by calling panel_order[panel_number];
        //example display the 5th panel's location
        System.out.println(panel_order[5].getLocation());
     }
}

你写你的面板的方式(都有不同的种类和特征)我怀疑这些面板也有不同的 类。

因此,要访问特定面板,您的代码可能类似于

((MyPanelType1) getContentPane().getComponent(0)).someMethod();

而每个面板都有字段

private MyPanelType1 firstPanel;

代码看起来像

firstPanel.someMethod();

现在决定哪个代码更容易阅读,并做出相应的决定。