Total members 11893 |It is currently Tue Nov 05, 2024 4:26 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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




Author:
Newbie
User avatar Posts: 2
Have thanks: 0 time

Reply the line
Code:

we.addActionListener(Listener(x));


with

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



_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 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:)


Author:
Newbie
User avatar Posts: 2
Have thanks: 0 time

i think because you are using border layout .

Code:
setLayout(new BorderLayout());


_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 4 posts ] 

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









Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com