Java Swing- can't get things to display properly using Border Layout, also getting an illegal component position error

Jerome Durham :

I just wrote a simple code where I want a canvas to appear in the CENTER of a JFrame and a config panel to appear in the NORTH of the same JFrame.However after adding them both the 'canvas' doesn't get displayed at all.If I only add the canvas it does get displayed but as soon as I add the config panel it doesn't get displayed anymore.

Furthermore if I try to add something else in the SOUTH of the same JFrame I get a illegal component position error no matter what..

I'm a complete noob when it comes to Swing so sorry if the answer is obvious but I tried googling and researching this for the last few hours without any luck.

Here's what I tried:

public class MainFrame extends JFrame {
 ConfigPanel configPanel;
 ControlPanel controlPanel;
 DrawingPanel canvas;

 public MainFrame() {
 super("My Drawing Application");
 init();
 }

private void init() {
     setDefaultCloseOperation(EXIT_ON_CLOSE);

     canvas = new DrawingPanel(this);
     configPanel = new ConfigPanel(this);
     controlPanel=new ControlPanel(this);

      add(canvas,CENTER); 

      add(configPanel,NORTH);

      add(controlPanel,SOUTH); //this gives me a illegal component position error

     pack();

}

}
Tommy Brettschneider :

please try getContentPane().add(controlPanel, SOUTH); instead of add(controlPanel, SOUTH);

do that for every component in the code you provided...

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=349595&siteId=1