Results 1 to 13 of 13

Thread: multiple frames

  1. #1
    jumpman8947 is offline Member
    Join Date
    Feb 2013
    Posts
    38
    Rep Power
    0

    Default multiple frames

    Hi i want to do something that sounds simple but it has been giving me a lot of problems. I want to display two panels inside of a jframe. So far none of the panels show up when i run the program. I can't figure out why at least one of them isn't showing up. here's the code thanks
    Java Code:
    import java.awt.*;
    import java.awt.BorderLayout.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    
    public class twoFrame extends JFrame
    {
    
    	private JFrame big;
    	private JPanel panel1, panel2;
    	private JButton but;
    	private JScrollPane sp;
    	private JTextArea full;
    
    	public twoFrame()
    	{
    		JFrame big = new JFrame();
    
    
    		full = new JTextArea();
    
    		sp = new JScrollPane(full);
    		sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    		sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    		full.setEditable(false);
    
    
    
    		JPanel panel1 = new JPanel();
    		getContentPane().add(panel1);
    		panel1.setPreferredSize(new Dimension (300,300));
    		panel1.setLayout(null);
    		//panel1.setBounds(10,10,300,300);
    		panel1.setVisible(true);
    
    		add(panel1);
    		panel1.add(sp);
    
    
    		JPanel panel2 = new JPanel();
    		getContentPane().add(panel2);
    		panel2.setLayout(null);
    
    
    		big.add(panel1);
    		big.add(panel2);
    		//panel2.add(but);
    		//big.add(but);
    		pack();
    
    		setTitle("Practice Frame");
    		setLocationRelativeTo(null);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		setVisible(true);
    		this.setExtendedState(Frame.MAXIMIZED_BOTH);
    	}
    	public static void main(String[] args)
    	{
    		twoFrame tf = new twoFrame();
    	}
    
    }

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,655
    Rep Power
    5

    Default Re: multiple frames

    You should not be setting the layouts to null, you should be learning how to properly use layout managers so problems such as this (lack of understanding) do not go away - they are prevented entirely.

    Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: multiple frames

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    jumpman8947 is offline Member
    Join Date
    Feb 2013
    Posts
    38
    Rep Power
    0

    Default Re: multiple frames

    I set my layouts null because i choose to do my programs in absolute positioning.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: multiple frames

    Quote Originally Posted by jumpman8947 View Post
    I set my layouts null because i choose to do my programs in absolute positioning.
    Bad choice.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    jumpman8947 is offline Member
    Join Date
    Feb 2013
    Posts
    38
    Rep Power
    0

    Default Re: multiple frames

    why is absolute bad?

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,655
    Rep Power
    5

    Default Re: multiple frames

    Quote Originally Posted by DarrylBurke View Post
    Bad choice.

    db
    Unless you actually want stuff to not work.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    jumpman8947 is offline Member
    Join Date
    Feb 2013
    Posts
    38
    Rep Power
    0

    Default Re: multiple frames

    not to work meaning absolution positioning is harder to program or it's just limited in terms of what you can do?

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,655
    Rep Power
    5

    Default Re: multiple frames

    Both really.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #10
    jumpman8947 is offline Member
    Join Date
    Feb 2013
    Posts
    38
    Rep Power
    0

    Default Re: multiple frames

    so what your saying is, I should switch to a different layout for this task. The reason why i wanted to do this in absolute is because basically all of my other work is in absolute positioning and i just wanted to add dual frames to my previous work. it's not a big deal though i can just change layout formats.

  11. #11
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,655
    Rep Power
    5

    Default Re: multiple frames

    Good. But we're digressing as its not the source of your problem of course, the source of your problem is that you are completely mixing up where you are adding the panels and what you are showing. Looping through the code I can see this:

    Java Code:
    getContentPane().add(panel1); // ok, although you're not properly making use of the BorderLayout of the frame.
    
    add(panel1); // ... what? Again?
    
    big.add(panel1); // and now you add the exact same component to the second frame, but not even to its content pane!
    I would suggest you take the time to browse through the Swing tutorial for a while to get better acquainted with the actual API.

    Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  12. #12
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: multiple frames

    And maybe take a look at the tutorial section on How to Use Split Panes.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  13. #13
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,377
    Rep Power
    5

    Default Re: multiple frames

    I assume you realize that you can have multiple layouts in a GUI. Each JPanel may have its own layout manager and each JPanel in that JPanel may have its own layout manager, etc, etc. It makes for more flexible GUI's when resizing the parent frame. And you can even write your own layout managers if you ever really need to do so. Check out the java tutorials in my signature. They cover layout managers in depth with lots of examples.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 4
    Last Post: 08-01-2012, 03:42 PM
  2. frames
    By giorgi in forum AWT / Swing
    Replies: 2
    Last Post: 05-04-2011, 08:45 PM
  3. Help with multiple frames/classes
    By Celletti in forum AWT / Swing
    Replies: 1
    Last Post: 04-28-2010, 03:18 AM
  4. jsp frames
    By vasug in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 04-16-2010, 05:00 PM
  5. Help regarding Frames
    By ramesh.8189 in forum AWT / Swing
    Replies: 14
    Last Post: 02-15-2009, 08:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •