Answer the question
In order to leave comments, you need to log in
How to add a link to an ArrayList?
I got acquainted with the collections recently, so do not judge strictly.
It is necessary to reduce the entry from duplication, the code starting
name1= new JPanel();
name1.setPreferredSize(new Dimension(900, 900)); etc.
Tried to write JPanel links to ArrayList and create all panels in one loop.
I clambered all over the garbage, found interesting posts, tried to piece it together, but something didn’t go according to plan!)
You need to create an array using ArrayList that will store links in itself (private JPanel name1, name2, name3, name4, name5, name6 , name7; ) and they will then be called in a loop to create an object
name1= new JPanel();
name1.setPreferredSize(new Dimension(900, 900));
so that I don't write to this
name1= new JPanel();
name1.setPreferredSize(new Dimension(900, 900));
name2= new JPanel();
name2.setPreferredSize(new Dimension(900, 900));
name3= new JPanel();
name3.setPreferredSize(new Dimension(900, 900));
name4= new JPanel();
name4.setPreferredSize(new Dimension(900, 900));
name5= new JPanel();
name5.setPreferredSize(new Dimension(900, 900));
name6= new JPanel();
name6.setPreferredSize(new Dimension(900, 900));
name7= new JPanel();
name7.setPreferredSize(new Dimension(900, 900));
What does it look like now!
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
public class Itibandr extends JFrame {
private JPanel name1, name2, name3, name4, name5, name6, name7;
public static void main(String[] args) {
new Itibandr();
}
private Itibandr() {
initPanel();
initFrame();
initMenuBar();
}
private void initPanel() {
name1= new JPanel();
name1.setPreferredSize(new Dimension(900, 900));
name2= new JPanel();
name2.setPreferredSize(new Dimension(900, 900));
name3= new JPanel();
name3.setPreferredSize(new Dimension(900, 900));
name4= new JPanel();
name4.setPreferredSize(new Dimension(900, 900));
name5= new JPanel();
name5.setPreferredSize(new Dimension(900, 900));
name6= new JPanel();
name6.setPreferredSize(new Dimension(900, 900));
name7= new JPanel();
name7.setPreferredSize(new Dimension(900, 900));
}
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
public class Itibandr extends JFrame {
ArrayList<Object> arrayPanel = new ArrayList<Object>();
arrayPanel.add(name1, name2, name3, name4, name5, name6, name7); // я знаю, что так объявлять нельзя!
public static void main(String[] args) {
new Itibandr();
}
private Itibandr() {
initPanel();
initFrame();
initMenuBar();
}
private void initPanel() {
// примерно написал!!
for (arrayPanel name: arrayPanel) {
name = new JPanel();
name.setPreferredSize(new Dimension(900, 900));
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question