I'm trying to add a JButton
to my JPanel
multiple times using a GridLayout
. For some reason though, every time I run the program it only shows 1 button.
Here's the code:
jPLeft = new JPanel();
jPLeft.setPreferredSize(new Dimension(600,500));
jPLeft.setBackground(Color.WHITE);
jPLeft.setLayout(new GridLayout(2,2));
jPLeft.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
window.add(jPLeft, BorderLayout.CENTER);
imageSand = new ImageIcon("..\\CSY1020\\src\\resources\\sand.jpg");
jBSand = new JButton(imageSand);
jPLeft.add(jBSand);
jPLeft.add(jBSand);
jPLeft.add(jBSand);
jPLeft.add(jBSand);
A Component
can only be added once, and can only have 1 parent Container
imageSand = new ImageIcon("..\\CSY1020\\src\\resources\\sand.jpg");
for (int i = 0; i < 4; i++) {
JButton jBSand = new JButton(imageSand);
jPLeft.add(jBSand);
jPLeft.add(jBSand);
jPLeft.add(jBSand);
jPLeft.add(jBSand);
}
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.