Results 1 to 3 of 3
  1. #1
    SaraDiab is offline Member
    Join Date
    Dec 2011
    Location
    mother EARTH
    Posts
    1
    Rep Power
    0

    Question actionlistener no working,, help

    it is giving me no error but still the actionlistener not working
    can u help me pls
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.ImageIcon;
    
    public class plaza extends JFrame
    {
    
         double price;
         int num;
         double tp=0.0;
    	JButton B04,B05,B06,B07;
    	JPanel P01,P03,P04;
    	ImageIcon I01,I02,I03,I04,I05;
    	JLabel L01,L02,L03,L04,L07,L08,L11,L12,L15,L16,L17,L18;
    	public plaza()
    	{
    
    //create objects for the componenets.
    P01=new JPanel();
    P03=new JPanel();
    P04=new JPanel();
    
    
    
    I01=new ImageIcon("Plaza.png");
    I02=new ImageIcon("pic1.jpg");
     I03=new ImageIcon("pic5.jpg");
     I04=new ImageIcon("pic3.jpg");
     I05=new ImageIcon("pic4.jpg");
    
     L01 = new JLabel(I01);
     L02 = new JLabel("How Many People:");
     L03 = new JLabel(I04);
     L04 = new JLabel("Executive Suite For individual use $478 ");
     //$478
     L07 = new JLabel(I05);
     L08 = new JLabel("Room for the business For one  $285");
     //$285
     L11 = new JLabel(I02);
     L12 = new JLabel("Standard Room for two $237 per person");
     // $237
     L15 = new JLabel(I03);
     L16 = new JLabel("King Room for two $310 per person");
    // $110
    
    
     L17 = new JLabel("  $" + tp);
    L18=new JLabel("Price=");
    
    
    
     B04 = new JButton("OK");
     B05 = new JButton("OK");
     B06 = new JButton("OK");
     B07 = new JButton("OK");
    
    
    
     // add the components to pannels
     add(P01, BorderLayout.NORTH);
     P01.add(L01);
    add(P03, BorderLayout.CENTER);
    P03.setLayout(new GridLayout(4,5,10,10));
    //P03.setBackground(new Color(215,198,132));
     P03.add(L03);
     P03.add(L04);
     P03.add(B04).setBackground(new Color(215,198,132));;
     P03.add(L07);
     P03.add(L08);
     P03.add(B05).setBackground(new Color(215,198,132));;
     P03.add(L11);
     P03.add(L12);
     P03.add(B06).setBackground(new Color(215,198,132));;
     P03.add(L15);
     P03.add(L16);
     P03.add(B07).setBackground(new Color(215,198,132));;
     add(P04, BorderLayout.SOUTH);
     P04.setBackground(Color.GRAY);
     P04.add(L18);
     P04.add(L17);
    //P04.add();
    ChangeButton b=new ChangeButton();
    B04.addActionListener(b);
    B05.addActionListener(b);
    B06.addActionListener(b);
    B07.addActionListener(b);
    
    	}
     // add the action listener to the component.
    public class ChangeButton implements ActionListener{
    	public void actionPerformed( ActionEvent e)
    {
    
    if (e.getSource()==B04)
    {
    
    	tp+=478;
    
    
    } else if(e.getSource()==B05){
    
    	tp+=285;
    
    
    }else if(e.getSource()==B06){
    
        num=2;
    	price=237;
    	tp+=(num*price);
    
    }else if( e.getSource()==B07){
    
         num=2;
       	price=310;
    	tp+=(num*price);
    
    }else{
    	System.out.println("Choose again");
    }
    
    }
    }
    
    
    
    
    // call the main class
          public static void main(String[] args)
    	{
            JFrame f = new plaza();
    		JPanel p =new JPanel();
    
    		f.setTitle("The Plaza Hotel");
    		f.setSize(800,800);
    		f.setVisible(true);     //set frame visible
    		p.setVisible(true);   //set panel visible
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
    }
    Last edited by Norm; 12-27-2011 at 02:33 AM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: actionlistener no working,, help

    not working
    Please explain what you mean.

    Add a println first statement in the actionPerformed method to see if the listener is being called.
    Last edited by Norm; 12-27-2011 at 02:36 AM.

  3. #3
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: actionlistener no working,, help

    You have only set values in the actionListener.

    I believe it IS working but you think it is not because you have not set up any Observers to relay the changed information back to your View.

    Either use setter methods and set up Observers following the Model-View method or manually type what form fields or JLabels you want updated after new values are set in your action listener.

Similar Threads

  1. Please help with actionlistener
    By ADustedEwok in forum New To Java
    Replies: 5
    Last Post: 12-08-2011, 10:04 PM
  2. Replies: 2
    Last Post: 01-04-2011, 04:30 AM
  3. JButton actionlistener not working, very strange
    By alacn in forum New To Java
    Replies: 4
    Last Post: 08-05-2010, 02:25 AM
  4. ActionListener NOT working
    By zed420 in forum New To Java
    Replies: 3
    Last Post: 12-20-2009, 03:40 PM
  5. Replies: 8
    Last Post: 05-28-2008, 07:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •