Reputation: 695
I'm learning GUIs, and trying to place two panels side by side in a frame. Without giving you all of my code (it's for an assignment) can anyone give me any pointers as to why the below isn't working? (ControlPanel just doesn't display) I followed the tutorials, but can't see where I'm going wrong?
JFrame frame = new JFrame("Software");
MainPanel m = new MainPanel();
ControlPanel c = new ControlPanel(frame);
frame.getContentPane().add(m, BorderLayout.WEST);
frame.getContentPane().add(c, BorderLayout.EAST);
public class MainPanel extends JPanel {
.......
public MainPanel(){
super();
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setFocusable(true);
requestFocus();
}
}
public ControlPanel(JFrame frame) {
super();
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setFocusable(true);
requestFocus();
}
}
Edit:
Rest of code:
JFrame frame = new JFrame("Software");
MainPanel m = new MainPanel();
ControlPanel c = new ControlPanel(frame);
frame.getContentPane().add(c, BorderLayout.WEST);
frame.getContentPane().add(m, BorderLayout.EAST);
frame.setLayout(new BorderLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(m);
frame.pack();
frame.setVisible(true);
Upvotes: 0
Views: 1527
Reputation: 3200
Check this
JFrame frame = new JFrame("Software");
MainPanel m = new MainPanel();
ControlPanel c = new ControlPanel(frame);
//getContentPane layout to BorderLayout
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(c, BorderLayout.WEST);
frame.getContentPane().add(m, BorderLayout.EAST);
//you don't need it for now
//frame.setLayout(new BorderLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setContentPane(m);
frame.pack();
frame.setVisible(true);
Edit: Layout of ContentPane of JFrame returns JFlowLayout so thats why the below part doesn't work
.getContentPane().add(c, BorderLayout.WEST);
frame.getContentPane().add(m, BorderLayout.EAST);
Upvotes: 2
Reputation: 17524
In your code, you are replacing the content panel of the JFrame
with your instance of MainPanel
:
JFrame frame = new JFrame("Software");
MainPanel m = new MainPanel();
ControlPanel c = new ControlPanel(frame);
frame.getContentPane().add(c, BorderLayout.WEST);
frame.getContentPane().add(m, BorderLayout.EAST);
frame.setLayout(new BorderLayout());// this has no use
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(m);// here the content pane becomes the MainPanel
frame.pack();
frame.setVisible(true);
So as you already add things to the content pane, there is no need to reset it (also, I moved the setLayout
to the content pane, instead of the JFrame
)
JFrame frame = new JFrame("Software");
MainPanel m = new MainPanel();
ControlPanel c = new ControlPanel(frame);
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(c, BorderLayout.WEST);
frame.getContentPane().add(m, BorderLayout.EAST);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
Upvotes: 4
Reputation: 6077
You need to set the layout of your frame to BorderLayout.
Use this:
frame.setlayout(new BorderLayout());
Moreover, if your frame is divided into exactly two equal halves, you may also use a GridLayout.
frame.setLayout(new GrigLayout(1,2)); // If the panels are side-by-side.
or
frame.setLayout(new GrigLayout(2,1)); //If the panels are vertically adjacent.
Upvotes: 4