jframe help

help making two panels in one jframe and lables

Page 1 of 1

4 Replies - 2046 Views - Last Post: 18 April 2010 - 07:13 PM Rate Topic: -----

#1 Guest_jessica*


Reputation:

jframe help

Posted 18 April 2010 - 10:31 AM

import java.awt.*;
import javax.swing.*;

public class template2
{
//sets up a main panel that other components can be added to

	public static void main(String[] args)
	{
		JFrame frame = new JFrame("Template");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		// sets up main panel
		JPanel mainPanel =  new JPanel();
		mainPanel.setBackground( new Color(220,250,0) );
		mainPanel.setPreferredSize( new Dimension(100, 100) );
		JLabel label1 = new JLabel ("one");
		//sets up 2nd panel
		JPanel subPanel2 = new JPanel();
		subPanel2.setPreferredSize(new Dimension(400,100) );
		mainPanel.add(subPanel2);
		JLabel label2 = new JLabel ("two");
		
		
		//add main panel to the content pane for this frame
		frame.getContentPane().add(mainPanel);
		frame.pack();
		frame.setVisible(true);
	}
}


So far I have everything compiling but I am suppose to make two panels where the jframe is split in half. Beyond that I need one panel to say "one" and one to say "Two". If anyone could help me out I'd really appreciate it.

Is This A Good Question/Topic? 0

Replies To: jframe help

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2696
  • View blog
  • Posts: 10,556
  • Joined: 15-July 08

Re: jframe help

Posted 18 April 2010 - 10:45 AM

Well, add the labels to each panel and then add both panels to the JFrame.
        public static void main(String[] args)
        {
                JFrame frame = new JFrame("Template");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                
                // sets up main panel
                JPanel mainPanel =  new JPanel();
                mainPanel.setBackground( new Color(220,250,0) );
                mainPanel.setPreferredSize( new Dimension(100, 100) );
                JLabel label1 = new JLabel ("one");
                mainPanel.add(label1);

                //sets up 2nd panel
                JPanel subPanel2 = new JPanel();
                subPanel2.setPreferredSize(new Dimension(400,100) );
                JLabel label2 = new JLabel ("two");
                subPanel2.add(subPanel2); 
                
                
                //add main panel to the content pane for this frame
                frame.getContentPane().add(mainPanel);
                frame.add(subPanel2);
                frame.pack();
                frame.setVisible(true);
        }



After Java 2, you can just call add(), and no need for getContentPane().add().
Was This Post Helpful? 0
  • +
  • -

#3 Guest_jessica*


Reputation:

Re: jframe help

Posted 18 April 2010 - 11:14 AM

I don't see what your saying that I haven't already done. I put the labels for one and two in there they just don't show up. I don't know how to split the frame into two panels. I have two panels just doesn't split the frame in the middle it's like one box inside the other.
Was This Post Helpful? 0

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2696
  • View blog
  • Posts: 10,556
  • Joined: 15-July 08

Re: jframe help

Posted 18 April 2010 - 02:44 PM

Oops...I seem to have forgotten that a JFrame has BorderLayout by default, which means that if you try to add more than one thing to it, it won't work. I would use a BoxLayout here. Because you do use BoxLayout, you should use getContnentPane().

    public static void main(String[] args)
    {
            JFrame frame = new JFrame("Template");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().setLayout(
            		new BoxLayout(frame.getContentPane(), BoxLayout.X_AXIS)
            );                
            // sets up main panel
            JPanel mainPanel =  new JPanel();
            mainPanel.setBackground( new Color(220,250,0) );
            mainPanel.setPreferredSize( new Dimension(100, 100) );
            JLabel label1 = new JLabel ("one");
            mainPanel.add(label1);

            //sets up 2nd panel
            JPanel subPanel2 = new JPanel();
            subPanel2.setPreferredSize(new Dimension(400,100) );
            JLabel label2 = new JLabel ("two");
            subPanel2.add(label2); 
            
            
            //add main panel to the content pane for this frame
            frame.add(mainPanel);
            frame.add(subPanel2);
            frame.setVisible(true);
    }


Was This Post Helpful? 0
  • +
  • -

#5 Guest_jessica*


Reputation:

Re: jframe help

Posted 18 April 2010 - 07:13 PM

thank you so much. it works perfectly now.your the best.
Was This Post Helpful? 0

Page 1 of 1