Jump to content

Placing bar into a JPanel with Absolute layout

- - - - -

  • Please log in to reply
4 replies to this topic

#1
vaironl

vaironl

    Programmer

  • Members
  • PipPipPipPip
  • 117 posts
I have a JPanel which needs a Menubar, thought I do not understand how to do this with a JPanel that has a null setLayout. The reason for the nullLayout is because I want to be able to position everything wherever I want.( I know that can be dangerous)\
The problem is here
public Panel()

	{

		setLayout(null);

		JLabel label = new JLabel("Hello");

		label.setSize(40,40); label.setLocation(0, 20);

		add(label);

		MenuBar mb = new MenuBar();

		add(mb); // Right here!

	}

The whole class
import javax.swing.*;

import java.awt.*;


public class Panel extends JPanel{

	/* all drawing methods, fields, labels, and GUI components will

	Placed inside of this panel	

	 */

	public Panel()

	{

		setLayout(null);

		JLabel label = new JLabel("Hello");

		label.setSize(40,40); label.setLocation(0, 20);

		add(label);

		MenuBar mb = new MenuBar();

		add(mb);

	}

	public void paint(Graphics g)

	{

		g.setColor(Color.ORANGE);

		g.fillRect(0, 0, getWidth(), getHeight());

		g.setColor(Color.red);

		g.drawRect(0, 500, 250, 20);// Recipe's Name

		g.drawRect(0, 530, 250, 20);// Author's Name

		g.drawRect(0, 560, 250, 20);// Rating Value

		g.drawRect(0, 590, 250, 20);// Serving Size

		g.setColor(Color.BLACK);

		Graphics2D g2 = (Graphics2D) g;

		g2.setStroke(new BasicStroke(3));

		g2.drawRect(0, 40, 250, getHeight());  

		g2.drawRect(250, 700, getWidth(), getHeight());

		


	}


}



#2
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
Is it a custom Menubar? It is easier if not safer, to add the Menubar to some type of Frame (Frame classes has a setMenubar method).
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#3
vaironl

vaironl

    Programmer

  • Members
  • PipPipPipPip
  • 117 posts

fread said:

Is it a custom Menubar? It is easier if not safer, to add the Menubar to some type of Frame (Frame classes has a setMenubar method).
Wall pardon me for my lack of knowledge but what do you mean by custom?
What I need to do is just add a menubar which can support submenus.
I also tried JMenuBar but I feel like the paint method is painting over it.

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Everything you add to that panel won't be drawn because you overrided the paint method.

#5
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts

Quote

Wall pardon me for my lack of knowledge but what do you mean by custom?
A long time ago I had an assignment to construct a menubar like look and feel without using an actual menu. So I ended up loading some buttons together and making submenus appear when i clicked. Used the graphics class to mimic radion button look and feel. That sort of thing. Ended up pretty crappy but I learned alot.
What I am saying is it is safer to add your MenuBar to a Frame, that way nothing will be painted over it.
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users