Results 1 to 10 of 10
Thread: Frame with 2 Panels
- 03-28-2011, 04:53 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 28
- Rep Power
- 0
Frame with 2 Panels
I am trying to create a frame with 2 panels each panel containing three buttons. The panel and frame layout is FlowLayout. When I compile and run I am getting a Red panel with visible buttons 4, 5, 6 and nothing else. I am missing the green panel and Buttons 1-3. I know I am missing something but not sure what it is. Could someone please help me to fixing my code. Thank you for your help.
Java Code:import javax.swing.*; import java.awt.*; public class TwoPanels extends Frame { private static JButton btn1; private static JButton btn2; private static JButton btn3; private static JButton btn4; private static JButton btn5; private static JButton btn6; private static JFrame f; public static void main(String[] args) { JFrame f = new JFrame ("Frame with FlowLayout"); JPanel p1 = new JPanel (new FlowLayout()); JButton btn1=new JButton("Button 1"); JButton btn2=new JButton ("Button 2"); JButton btn3=new JButton ("Button 3"); JPanel p2 = new JPanel (new FlowLayout()); JButton btn4=new JButton("Button 4"); JButton btn5=new JButton ("Button 5"); JButton btn6=new JButton ("Button 6"); p1.add(btn1); p1.add(btn2); p1.add(btn3); p2.add(btn4); p2.add(btn5); p2.add(btn6); f.setSize(600,300); f.setLocation (400,200); f.getContentPane().add(p1); f.getContentPane().add(p2); p1.setBackground(Color.GREEN); p2.setBackground(Color.RED); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//close frame upon f.setVisible(true); } }
-
The JFrame uses BorderLayout by default, and since you never change the JFrame's layout, it remains BorderLayout. When adding components to this JFrame without specifying a BorderLayout constant, they will be added by default into the BorderLayout.CENTER position, and the last component added this way will cover all other components added.
Better to make the JFrame use a different layout such as a GridLayout(1, 0) or a BoxLayout, or whatever.
- 03-28-2011, 06:17 AM #3
OT nitpick: actually, the last component added is at the bottom, not the top, of the stacking order. In most cases, any previously added component isn't seen because it has its default size of [0, 0].When adding components to this JFrame without specifying a BorderLayout constant, they will be added by default into the BorderLayout.CENTER position, and the last component added this way will cover all other components added.
Play with this ;)dbJava Code:import java.awt.Color; import java.awt.Point; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.*; public class BorderLayoutOverlay { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new BorderLayoutOverlay().makeUI(); } }); } public void makeUI() { MouseAdapter listener = new MouseAdapter() { Point p = null; @Override public void mousePressed(MouseEvent e) { p = e.getLocationOnScreen(); } @Override public void mouseDragged(MouseEvent e) { JComponent c = (JComponent) e.getSource(); Point l = c.getLocation(); Point here = e.getLocationOnScreen(); c.setLocation(l.x + here.x - p.x, l.y + here.y - p.y); p = here; } }; JLabel label1 = new JLabel("Drag me!"); label1.setOpaque(true); label1.setBounds(250, 250, 100, 100); label1.setBackground(Color.CYAN); label1.addMouseListener(listener); label1.addMouseMotionListener(listener); JLabel label2 = new JLabel("I'm at the back!"); JFrame frame = new JFrame(); frame.add(label1); frame.add(label2); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(500, 500); frame.setLocationRelativeTo(null); frame.setVisible(true); } }
- 03-28-2011, 06:52 AM #4
Member
- Join Date
- Mar 2011
- Posts
- 28
- Rep Power
- 0
I'm stuck using the FlowLayout manager for this program. Basically instructions are:
1. Create a frame and set its layout to FlowLayout
2. Create 2 panels and add them to the frame
3. Each panel contains three buttons. The panel uses FlowLayout
Then once I have completed this set, do it again but this time with BorderLayout & GridLayout.
- 03-28-2011, 06:56 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 28
- Rep Power
- 0
This is my simple frame
Java Code:import javax.swing.*; import java.awt.*; public class Frame { private static JFrame frame; public static void main(String[] args) { JFrame frame = new JFrame ("Frame"); frame.setLayout(new FlowLayout()); frame.setSize(450,300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//close frame u frame.setVisible(true); } }
- 03-28-2011, 07:18 AM #6
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
I don't see the part where you create the panels and the buttons in your latest code.
Also (very important) look at DB's example above to see the structure that you should use for your program.
- 03-28-2011, 07:35 AM #7
Member
- Join Date
- Mar 2011
- Posts
- 28
- Rep Power
- 0
This is probably a code mess and not perfect in the code layout. I am new at Java and trying to figure this out and receiving different information (google) is confusing.
1. Create a frame and set its layout to FlowLayout
2. Create 2 panels and add them to the frame
3. Each panel contains three buttons. The panel uses FlowLayout
Java Code://frame with FLowLayout without any content import javax.swing.*; import java.awt.*; public class Frame { private static JFrame frame; public static void main(String[] args) { JFrame frame = new JFrame ("Frame"); frame.setLayout(new FlowLayout()); frame.setSize(450,300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//close frame u frame.setVisible(true); } }
[code]
//code for 2 panels and 3 buttons
//this is where the p1 with buttons 1-3 and green background is not visible when i compile and run.
javax.swing.*;
import java.awt.*;
public class TwoPanels extends Frame
{
private static JButton btn1;
private static JButton btn2;
private static JButton btn3;
private static JButton btn4;
private static JButton btn5;
private static JButton btn6;
public static void main(String[] args)
{
super("Frame");
JPanel p1 = new JPanel (new FlowLayout());
JButton btn1=new JButton("Button 1");
JButton btn2=new JButton ("Button 2");
JButton btn3=new JButton ("Button 3");
p1.add(btn1);
p1.add(btn2);
p1.add(btn3);
p1.setBackground(Color.GREEN);
JPanel p2 = new JPanel (new FlowLayout());
JButton btn4=new JButton("Button 4");
JButton btn5=new JButton ("Button 5");
JButton btn6=new JButton ("Button 6");
p2.add(btn4);
p2.add(btn5);
p2.add(btn6);
p2.setBackground(Color.RED);
f.getContentPane().add(p1);
f.getContentPane().add(p2);
f.setSize(765,690);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//close frame upon
f.setVisible(true);
}
}
- 03-28-2011, 07:51 AM #8
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Yeah, that's why I said above that
"Also (very important) look at DB's example above to see the structure that you should use for your program."
You see all those static variables you have everywhere are not very good. Darryl.Burke has already posted an example (complete with a main method) which you can use as a template for your swing programs so that they are structured correctly.
- 01-10-2013, 06:02 AM #9
Member
- Join Date
- Jan 2013
- Posts
- 1
- Rep Power
- 0
Re: Frame with 2 Panels
Try working with this code.. u ll get 2 pannels in a frame with 3 buttons on each..
You can mention the layout for ur frame as this [ f.setLayout(new GridLayout());].. hope u got wt u wanted..-.gif)
import javax.swing.*;
import java.awt.*;
public class TwoPanels extends Frame
{
/**
*
*/
private static final long serialVersionUID = 1L;
public static void main(String[] args)
{
JFrame f = new JFrame ("Frame with FlowLayout");
//JFrame q = new JFrame ("Frame with FlowLayout");
JPanel p2 = new JPanel (new FlowLayout());
JButton btn4=new JButton("Button 4");
JButton btn5=new JButton ("Button 5");
JButton btn6=new JButton ("Button 6");
JPanel p1 = new JPanel (new FlowLayout());
JButton btn1=new JButton("Button 1");
JButton btn2=new JButton ("Button 2");
JButton btn3=new JButton ("Button 3");
p1.add(btn1);
p1.add(btn2);
p1.add(btn3);
p2.add(btn4);
p2.add(btn5);
p2.add(btn6);
f.setLayout(new GridLayout());
f.setSize(600,300);
f.setLocation (400,200);
f.getContentPane().add(p1);
f.setSize(600,300);
f.setLocation (400,200);
f.getContentPane().add(p2);
p1.setBackground(Color.GREEN);
p2.setBackground(Color.RED);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//close frame upon
f.setVisible(true);
//q.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//close frame upon
// q.setVisible(true);
}
}
- 01-10-2013, 06:49 AM #10
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,606
- Rep Power
- 5
Re: Frame with 2 Panels
@narmadha, please note the date on the original post - I would presume the original poster has moved on after almost two years. Further, note this is a technical forum - using abbreviations such as 'ur' and 'wt' is discourage and often not only makes your post harder to understand but can also lead to confusion. Lastly, I recommend reading the following:
The Problem with Spoon-feeding
I am locking this thread.
Similar Threads
-
Need some help with panels inside panels
By kakefjes in forum AWT / SwingReplies: 0Last Post: 03-17-2011, 11:36 AM -
How to set Jpanel in the center of frame when Increase the size of frame
By justbeller in forum AWT / SwingReplies: 4Last Post: 01-18-2011, 08:22 AM -
Java slave Frame access to its owner main frame problem
By cagdaseckin in forum New To JavaReplies: 0Last Post: 12-10-2010, 10:40 AM -
how to add 2 panels in a panel inside a frame
By clydedoris in forum New To JavaReplies: 4Last Post: 07-31-2010, 09:11 AM -
Problem in adding Multiple Panels at the Specific positon on frame
By SANDY_INDIA in forum AWT / SwingReplies: 7Last Post: 07-09-2008, 12:06 AM


LinkBack URL
About LinkBacks


Bookmarks