School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
Welcome to Dream.In.Code
Become an Expert!

Join 340,158 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 4,068 people online right now. Registration is fast and FREE... Join Now!



y menubar cant hv function?

y menubar cant hv function? Rate Topic: -----

#1 yooxygen  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: Members
  • Posts: 15
  • Joined: 20-January 08


Dream Kudos: 0

Post icon  Posted 06 November 2008 - 02:32 AM

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

public class MenuTest extends JApplet implements ActionListener
{
	JMenu menuFile,menuEdit,menuColor;
	JPanel p1,p2;
	JMenuItem item,item1;
	JMenuItem red,green,blue,pink,black;
	
	public void init()
	{
		Container pane = getContentPane();
		pane.setBackground (Color.white);
		pane.setLayout (new GridLayout(2,1));
		
		JPanel p1= new JPanel();
		p1.setBackground(Color.white);
		p1.setLayout(new BorderLayout());
		
		JPanel p2= new JPanel();
		p2.setBackground(Color.blue);
		p2.setLayout(new FlowLayout());
		
		menuFile = new JMenu("File");
		item = new JMenuItem("Quit");
		item.addActionListener(this);
		menuFile.add(item);
		
		menuEdit = new JMenu("Edit");
		item1 = new JMenuItem("Erase");
		item1.addActionListener(this);
		menuEdit.add(item1);
		
		menuColor = new JMenu("Color");
		
		red = new JMenuItem("Red");
		red.addActionListener(this);
		menuColor.add(red);
		
		green = new JMenuItem("Green");
		green.addActionListener(this);
		menuColor.add(green);
		
		blue = new JMenuItem("Blue");
		blue.addActionListener(this);
		menuColor.add(blue); 
		
		pink = new JMenuItem("Pink");
		pink.addActionListener(this);
		menuColor.add(pink); 
		
		black = new JMenuItem("Black");
		black.addActionListener(this);
		menuColor.add(black);
		
		
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		menuBar.add(menuFile);
		menuBar.add(menuEdit);
		menuBar.add(menuColor);
		
		p1.add(menuBar,"North");
		pane.add(p1);
		pane.add(p2);
	}
	
	
	public void actionPerformed(ActionEvent e)
	{
		
		if(e.getSource()==red)
		{
			p2.setBackground(Color.red);
		}
		
		 else if(e.getSource()==green)
		{
			p1.setBackground(Color.green);
		}
		
		 else if(e.getSource()==blue)
		{
			p1.setBackground(Color.blue);
		}
		
		 else if(e.getSource()==pink)
		{
			p1.setBackground(Color.pink);
		}
		
		 else if(e.getSource()==black)
		{
			p1.setBackground(Color.black);
		}
	}   
}
		



i hv write the code n this cant work when i wan to chg the backgorund color...can help me c which is uncorrect statement...thx :^:
Was This Post Helpful? 0
  • +
  • -


#2 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • Icon
  • View blog
  • Group: Contributors
  • Posts: 2,391
  • Joined: 16-August 08


Dream Kudos: 75

Posted 06 November 2008 - 07:56 AM

When you refer to the panels, p1,p2 in the actionPerformed method you ae referring to the class variables, which are null because in the init() method you redeclare them as a local variable:\
        JPanel p1= new JPanel();
        p1.setBackground(Color.white);
        p1.setLayout(new BorderLayout());
        
        JPanel p2= new JPanel();
        p2.setBackground(Color.blue);
        p2.setLayout(new FlowLayout());



Remove the JPanel from before and it will work:
        p1= new JPanel();
        p1.setBackground(Color.white);
        p1.setLayout(new BorderLayout());
        
        p2= new JPanel();
        p2.setBackground(Color.blue);
        p2.setLayout(new FlowLayout());


Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month