Total members 9951 | Gratitudes |It is currently Sat Feb 11, 2012 9:48 am Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 4 posts ] 
Author Question
 Question subject: Need help with generic listeners
PostPosted: Sat Jan 24, 2009 2:04 am 
Offline
Newbie
User avatar

Joined: Sat Jan 24, 2009 1:28 am
Posts: 2
Has thanked: 0 time
Have thanks: 0 time

Need help for my comp-sci class, I thought I had the code done, but I did something wrong and it won't compile. It must end up looking like this:
Image

Driver:

Code:
   import javax.swing.JFrame;
   public class Driver09
   {
      public static void main(String[] args)
      {
         JFrame frame = new JFrame("Sum the Series");
         frame.setSize(200, 200);
         frame.setLocation(200, 100);
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setContentPane(new Panel09());
         frame.setVisible(true);
      }
   }



Code:
import javax.swing.*;
   import java.awt.*;
   import java.awt.event.*;
    public class Panel09 extends JPanel
   {
      private JLabel label;
      private double total;
      private JButton we;
       public Panel09()
      {
         setLayout(new BorderLayout());
         total = 0.0;
     
         JPanel panel = new JPanel();
         panel.setLayout(new GridLayout(2, 3, 10, 0));
         add(panel, BorderLayout.CENTER);
         addButton(panel, "+1.00", 1.0);
         addButton(panel, "+0.10", 0.1);
         addButton(panel, "+0.01", 0.01);
         addButton(panel, "-1.00", -1.0);
         addButton(panel, "-0.10", -0.1);
         addButton(panel, "-0.01", -0.01);
       
     
     
         label = new JLabel("$0.00");
         label.setFont(new Font("Serif", Font.BOLD, 30));
         label.setHorizontalAlignment(SwingConstants.CENTER);
         add(label, BorderLayout.NORTH);
      }
       private void addButton(JPanel panel, String s, double x)
      {
     
     
         we = new JButton();
         we.addActionListener(Listener(x));
         add(we);
             
     
     
      }
       private class Listener implements ActionListener
      {
         private double myX;
          public Listener(double x)
         {
            myX = x;
         }
          public void actionPerformed(ActionEvent e)
         {
            double s = Double.parseDouble(label.getText());
            s = s + myX;
            label.setText("$:"+s);
         
         
         }
      }
   }



COMPILE ERROR:

Panel09.java:37: cannot find symbol
symbol : method Listener(double)
location: class Panel09
we.addActionListener(Listener(x));
^
1 error


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: Need help with generic listeners
PostPosted: Sat Jan 24, 2009 10:51 am 
Offline
Mastermind
User avatar

Joined: Tue Mar 27, 2007 10:55 pm
Posts: 2103
Location: Earth
Has thanked: 39 time
Have thanks: 56 time
Reply the line
Code:

we.addActionListener(Listener(x));


with

Code:
we.addActionListener(new Listener(x));


_________________
Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: Need help with generic listeners
PostPosted: Tue Jan 27, 2009 2:57 am 
Offline
Newbie
User avatar

Joined: Sat Jan 24, 2009 1:28 am
Posts: 2
Has thanked: 0 time
Have thanks: 0 time
thank you so much, now it compiles
btw:i think my code has more problems, cause now it just gives me a big button that does nothing..so any more help with that would be greatly appreciated:)


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: Need help with generic listeners
PostPosted: Tue Jan 27, 2009 10:13 am 
Offline
Mastermind
User avatar

Joined: Tue Mar 27, 2007 10:55 pm
Posts: 2103
Location: Earth
Has thanked: 39 time
Have thanks: 56 time
i think because you are using border layout .

Code:
setLayout(new BorderLayout());

_________________
Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 4 posts ] 
Quick reply


  


 Similar topics
 Topic title   Forum   Author   Comments 
 Generic Algorithm  C-C++  mileloader  0
 generic queue  C-C++  mileloader  0

All times are UTC [ DST ]


Users browsing similar posts

Users browsing this forum: No registered users and 3 guests



Jump to:  
Previous Question | Next Question 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team