简体   繁体   中英

creating new jpanels using loop

i creating new jpanels using loop. but how i using different object name. here is my code:

for(int i=0; i<panelnumbers.length(); i++){
    MainConfig.page21.addNewPanel(MainConfig.page21.pos1, "INFORMACIÓN No. " + (i + 2));
}

public static void addNewPanel(int y, String title) {
    Add a = new Add(title);
    jLayeredPane3.add(a);
    a.setBounds(0, y, 1333, 450);
    jPanel1.setPreferredSize(new Dimension(1333, (pos + 480)));
    jLayeredPane3.setPreferredSize(new java.awt.Dimension(1333, (pos + 480)));
    jLayeredPane3.validate();
    jScrollPane1.getViewport().setViewPosition(new Point(0, (pos + 480)));
}

i creating new jpanels using loop. but how i using different object name.

You are using absolute coordinates, a layout manager gives a better GUI across platforms, later Windows versions, accessibility etcetera.

MainConfig.page21.setLayout(new BoxLayout(MainConfig.page21, BoxLayout.Y_AXIS));

Also normally one would not need the JPanel, an event listener would know how to retrieve the panel.

Nevertheless you could maintain an array of JPanels:

List<JPanel> panels = new ArrayList<>();

In addPanel create a local variable, and add that

public void addNewPanel(int y, String title) { // Not static
    JPanel panel = new JPane();
    ...
    panels.add(panel);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM