Yours is almost a minimal example program. To truly comply, it must compile (yours doesn't -- new TestClass()
?), and it should be in one file with imports. This for example is closer:
import java.awt.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
public class Run extends JFrame {
public Run() {
// !!?? add(new TestClass());
add(new Center()); // !! this is better
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] _) {
new Run();
}
}
class Buttons extends JPanel {
public Buttons() {
setLayout(new GridLayout(2, 3));
add(new JButton("Button 1"));
add(new JButton("Button 2"));
add(new JButton("Button 3"));
add(new JButton("Button 4"));
add(new JButton("Button 5"));
add(new JButton("Button 6"));
}
}
class Center extends JPanel {
JPanel centerFrame = new JPanel();
public Center() {
setLayout(new BorderLayout());
centerFrame.setBackground(Color.RED);
centerFrame.setBorder(new LineBorder(Color.BLUE, 6));
centerFrame.add(panel1());
add(centerFrame, BorderLayout.CENTER);
add(new Buttons(), BorderLayout.PAGE_END);
System.out.println(centerFrame.getLayout()); // !! hm, this may be important
}
public JPanel panel1() {
JPanel pane = new JPanel(new BorderLayout());
JPanel content = new JPanel();
content.setPreferredSize(new Dimension(400, 200));
pane.add(content, BorderLayout.CENTER);
return pane;
}
}
But more importantly, if you run this program, you'll see that I've added a line of code which will answer your question for you. :)