I have isolated the funny behavior, but still don't understand why it behaves this way.
None the less I was right about it not accepting the same object to add twice.
Below is identical code with 1 difference.
ADDs JPanel2 to JPanel1 twice.
import java.awt.*;
import javax.swing.*;
public class Sudoku2 extends JFrame {
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
public Sudoku2(){
panel2.setLayout(new GridLayout(3, 3));
panel2.add(new JTextField(1));
panel3.setLayout(new GridLayout(3, 3));
panel3.add(new JTextField(1));
panel1.setLayout(new GridLayout(3, 3));
panel1.add(panel2);
panel1.add(panel2); // ONLY CHANGE
add(panel1);
}
public static void main(String[] args){
Sudoku2 frame = new Sudoku2();
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Adds JPanel2 and JPanel3 to JPanel1
import java.awt.*;
import javax.swing.*;
public class Sudoku2 extends JFrame {
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
public Sudoku2(){
panel2.setLayout(new GridLayout(3, 3));
panel2.add(new JTextField(1));
panel3.setLayout(new GridLayout(3, 3));
panel3.add(new JTextField(1));
panel1.setLayout(new GridLayout(3, 3));
panel1.add(panel2);
panel1.add(panel3); //ONLY CHANGE
add(panel1);
}
public static void main(String[] args){
Sudoku2 frame = new Sudoku2();
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Compile them both and you will see the behavior, unfortunately I cant explain it. great question for the teacher!