Results 1 to 3 of 3
Thread: Help with overlapping JPanels
- 03-09-2009, 04:24 PM #1
Help with overlapping JPanels
I am trying to write a calculator for my final and am having trouble with my 2 panels(panel of numbers and operations, panel for output) overlapping. Am I just not correctly adding them to the frame or am I missing something with GridLayout or what? Here is the code I'm having issues with
JFrame frame = new JFrame("Calculator"); TextField textField = new TextField(); JPanel outputPanel = new JPanel(new GridLayout(1,4)); outputPanel.setSize(300,20); textField.setSize(300, 20); textField.setEditable(false); textField.setText("0"); outputPanel.add(textField); frame.add(outputPanel); addButtons(frame);
JPanel inputPanel = new JPanel(new GridLayout(4,4,5,5)); JButton button7 = new JButton("7"); button7.setText("7"); inputPanel.add(button7); //...rest of buttons frame.add(inputPanel);Liberty has never come from the government.
Liberty has always come from the subjects of government.
The history of liberty is the history of resistance.
The history of liberty is a history of the limitation of governmental power, not the increase of it.
- 03-09-2009, 08:46 PM #2Senior Member
- Join Date
- Jun 2008
- Rep Power
The JFrames contentPane uses BorderLayout per default, and simply calling add(Component) on a BorderLayout container is the same as calling add(Component, BorderLayout.CENTER). So, you are adding both panels to the same spot. You need to call add(Component, BorderLayout.North) or SOUTH, or EAST, or WEST, for one of the components.
- By JetsYanks in forum New To JavaReplies: 8Last Post: 12-25-2009, 03:11 PM
- By hitmen in forum Java AppletsReplies: 3Last Post: 03-06-2009, 06:33 AM
- By keerthi_y19 in forum New To JavaReplies: 4Last Post: 08-26-2008, 10:30 PM
- By Mastergeek666 in forum AWT / SwingReplies: 1Last Post: 01-19-2008, 01:32 AM
- By Soda in forum New To JavaReplies: 3Last Post: 12-08-2007, 06:54 PM