Results 1 to 4 of 4
  1. #1
    CodingNewb_OU is offline Member
    Join Date
    Mar 2010
    Posts
    1
    Rep Power
    0

    Default New to Java, Please Help!!

    Hello all,
    I am new to java and I’m having a very hard time figuring out an assignment. A small team of us worked on this for hours, and while I think we are on the right track, we can’t seem to get it to work.
    The issue I’m having is getting the classes to recognize variables declared in other classes in the same package.
    Here’s the problem:

    “Decouple the ButtonListener class from the PushCounterPanel class and make the ButtonListener class an independent class while maintaining the communication between the two classes.”

    And the given code:
    package PushCounterPanel;

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class PushCounterPanel extends JPanel
    {
    public int count;
    public JButton push;
    public JLabel label;

    public PushCounterPanel ()
    {
    count = 0;
    push = new JButton ("Push Me!");


    label = new JLabel ("Pushes: " + count);

    add(push);
    add(label);

    setPreferredSize (new Dimension (300, 40));
    setBackground (Color.cyan);
    }
    private class ButtonListener implements ActionListener
    {
    public void ActionPerformed (ActionEvent event)
    {
    count++;
    label.setText("Pushes: " + count);

    }
    }

    }

    //******** This is the Main Method************//

    package PushCounterPanel;
    import java.swing.JFrame
    public class PushCounter
    {
    public static void main (String [] args)
    {
    JFrame frame = new JFrame (“Push Counter”);
    Frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    Frame.getContentPane().add(new PushCounterPanel());
    Frame.pack();
    Frame.setVisible(true);
    }
    }

    -------------------
    Any advice at all would be greatly appreciated!

    Thanks in advance!!

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    hi CodingNewb,

    i really don't know how you get your code above even compiled. java is case sensitive. so this block of code would never run

    Java Code:
    JFrame frame = new JFrame (“Push Counter”);
    Frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    Frame.getContentPane().add(new PushCounterPanel());
    Frame.pack();
    Frame.setVisible(true);

    here is one quick&dirty implementation

    save this code in file PushCounterPanel.java
    Java Code:
    package PushCounterPanel;
    
    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.*;
    
    public class PushCounterPanel extends JPanel {
    	public JButton push;
    	public JLabel label;
    
    	public PushCounterPanel() {
    		push = new JButton("Push Me!");
    		
                              label = new JLabel("Pushes: " + 0);
    // the following statement is for "maintaining the communication between the two classes"
    		push.addActionListener(new ButtonListener(label));
    
    		add(push);
    		add(label);
    
    		setPreferredSize(new Dimension(300, 40));
    		setBackground(Color.cyan);
    	}
    }

    save this code in a file ButtonListener.java
    Java Code:
    package PushCounterPanel;
    
    import javax.swing.JLabel;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    class ButtonListener implements ActionListener {
    	
    	static int count;
    	
    	JLabel label;
    	
    	public ButtonListener(JLabel label) {
    		this.label = label;
    	}
    
    	public void actionPerformed(ActionEvent event) {
    		count++;
    		label.setText("Pushes: " + count);
    
    	}
    }

    save this code in a file PushCounter.java
    Java Code:
    package PushCounterPanel;
    
    import javax.swing.JFrame;
    
    public class PushCounter {
    
    	public static void main(String[] args) {
    		JFrame frame = new JFrame("Push Counter");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.getContentPane().add(new PushCounterPanel());
    		frame.pack();
    		frame.setVisible(true);
    	}
    }

    i compiled this files in eclipse and all is ok. the counter-variable is now as a static class variable in the ButtonListener and each button that use this class will share the same value in the counter. if you need a counter per label delete the modifier static from the count variable. have fun.
    Last edited by j2me64; 03-05-2010 at 11:33 AM.

  3. #3
    852000 is offline Member
    Join Date
    Jan 2010
    Posts
    8
    Rep Power
    0

    Default

    the point isn't that,
    you forgotten that code:push.setActionListener(........),
    the code is to rigister your button listener
    hope i can help you

  4. #4
    852000 is offline Member
    Join Date
    Jan 2010
    Posts
    8
    Rep Power
    0

Posting Permissions

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