Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a Java Expert!

Join 244,296 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 856 people online right now. Registration is fast and FREE... Join Now!




y menubar cant hv function?

 
Reply to this topicStart new topic

y menubar cant hv function?

yooxygen
6 Nov, 2008 - 02:32 AM
Post #1

New D.I.C Head
*

Joined: 20 Jan, 2008
Posts: 15


My Contributions
CODE

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 icon_up.gif

User is offlineProfile CardPM
+Quote Post


BigAnt
RE: Y Menubar Cant Hv Function?
6 Nov, 2008 - 07:56 AM
Post #2

May Your Swords Stay Sharp
Group Icon

Joined: 16 Aug, 2008
Posts: 2,384



Thanked: 95 times
Dream Kudos: 75
My Contributions
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:\
java

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:
java

p1= new JPanel();
p1.setBackground(Color.white);
p1.setLayout(new BorderLayout());

p2= new JPanel();
p2.setBackground(Color.blue);
p2.setLayout(new FlowLayout());

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 04:42PM

Live Java Help!

Be Social

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

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month