2 Replies - 141 Views - Last Post: 13 September 2012 - 05:18 AM Rate Topic: -----

#1 cam0988  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 13-September 12

GUI basics

Posted 13 September 2012 - 04:56 AM

Hi,
Have been studying java for the first half of the year and am now just getting started on GUI.
Having a little trouble understanding how to add a JPanel to a JFrame from an external class.

class one
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class DrawingFrame extends JFrame{
	
	protected BottomPanel bottom;

	public DrawingFrame() {

		bottom = new BottomPanel();

		JPanel p1 = new JPanel();
		p1.setBackground(Color.BLACK);

		add(p1, BorderLayout.NORTH);
		add(bottom, BorderLayout.EAST);

	}

	public static void main(String[] args) {
		DrawingFrame frame = new DrawingFrame();
		frame.setTitle("Assesed Lab GUI");
		frame.setSize(960, 700);
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}
}



class 2
import java.awt.Color;

import javax.swing.JButton;
import javax.swing.JPanel;


public class BottomPanel extends JPanel{
	
	public BottomPanel(){
	
	JPanel bottom = new JPanel();
	bottom.setBackground(Color.BLUE);
	bottom.setVisible(true);
	
	
	}

}



so as is the panel created in the first class and added displays in North however I am unable to add the JPanel bottom to East. It compiles and runs but remains blank everywhere except in the North panel.

Any help would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: GUI basics

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 996
  • View blog
  • Posts: 2,212
  • Joined: 05-April 11

Re: GUI basics

Posted 13 September 2012 - 05:07 AM

Your BottomPanel class is already a JPanel, you do not need to make a new JPanel inside of your BottomPanel (unless you want to add it to BottomPanel's container).

The JPanel bottom in BottomPanel is doing nothing right now. The panel is being created and waiting on being garbage collected because it is not added to a container.

Since BottomPanel already is a JPanel, this is what you could do
import java.awt.Color;

import javax.swing.JButton;
import javax.swing.JPanel;


public class BottomPanel extends JPanel{
	
	public BottomPanel(){
	    setBackground(Color.BLUE);
            setPreferredSize(new Dimension(200, 200));
	}

}


Was This Post Helpful? 1
  • +
  • -

#3 cam0988  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 13-September 12

Re: GUI basics

Posted 13 September 2012 - 05:18 AM

Of course,
what a silly mistake,
thankyou so much for your help, been frustrating me all evening.

Cheers
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1