Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

Need help with generic listeners

Sat Jan 24, 2009 2:04 am

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



Re: Need help with generic listeners

Sat Jan 24, 2009 10:51 am

Reply the line
Code:

we.addActionListener(Listener(x));


with

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



Re: Need help with generic listeners

Tue Jan 27, 2009 2:57 am

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

Re: Need help with generic listeners

Tue Jan 27, 2009 10:13 am

i think because you are using border layout .

Code:
setLayout(new BorderLayout());


Post a reply
  Related Posts  to : Need help with generic listeners
 Two Listeners- Mutli- Action Listeners on Text Area     -  
 generic queue     -  
 Generic Algorithm     -