The Artima Developer Community
Sponsored Link

Java Answers Forum
Passing a parameter to Constructor error

1 reply on 1 page. Most recent reply: Apr 14, 2004 11:48 AM by twc

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Ja Ja Binks

Posts: 14
Nickname: ds99bwood9
Registered: Apr, 2004

Passing a parameter to Constructor error Posted: Apr 14, 2004 7:37 AM
Reply to this message Reply
Advertisement
Hi, I have a cannot resolve symbol error when I pass the parameter (ActionListener listener) to the Constructor for a GUI Vcr interface that I am developing. Can anyone see where I am going wrong?

PROGRAMMINGLISTING.JAVA

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

class ProgrammeListing extends JFrame implements ActionListener
{




public ProgrammeListing(ActionListener listener)
{
setLocation(20,20);
setSize(800,600);


JPanel myPanel = new JPanel();
myPanel.setLayout(null);
setContentPane(myPanel);

JButton exitButton = new JButton("Exit");
exitButton.setBounds(20,40,70,30);
exitButton.setBackground(Color.red);
exitButton.addActionListener(this);
myPanel.add(exitButton);

setVisible(true);
} //constructor

public boolean validSelection()
{
}

public int getDay()
{
}

public int getMonth()
{
}

public String getChannel()
{
}

public int getStartHour()
{
}

public int getStartMin()
{
}

public int getStopHour()
{
}

public int getStopMin()
{
}

public String getTitle()
{
}


public void actionPerformed(ActionEvent e)
{
if (e.getSource() instanceof JButton)
{
System.out.println("Closing window");
System.exit(0);
}
}
}


and....

VCR.JAVA

class Vcr
{
public static void main(String args[])
{
ProgrammeListing vcr = new ProgrammeListing();
} // main
} // class Vcr

I am aware this may be a very basic error but my java isn't so great and I am looking to develop it, seeing as though my degree is Computer Science...


twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: Passing a parameter to Constructor error Posted: Apr 14, 2004 11:48 AM
Reply to this message Reply
The main problem is that you are not sending the ActionListener as an argument in the VCR class. However, you do not need the ActionListener as a parameter so the easiest thing to do is to change your constructor heading to the following.
public ProgrammeListing()


However, let me suggest a more OO way to handle events. Notice the use of an inner class to handle the event.

PROGRAMMINGLISTING.JAVA
 
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
 
class ProgrammeListing extends JFrame
{
  public ProgrammeListing()
  {
    setLocation(20,20);
    setSize(800,600);
 
    JPanel myPanel = new JPanel();
    myPanel.setLayout(null);
    setContentPane(myPanel);
 
    JButton exitButton = new JButton("Exit");
    exitButton.setBounds(20,40,70,30);
    exitButton.setBackground(Color.red);
    exitButton.addActionListener(new ExitListener());
    myPanel.add(exitButton);
 
    setVisible(true);
  } //constructor
 
  //snipped empty methods for brevity
 
  class ExitListener implements ActionListener//inner class
  {
    public void actionPerformed(ActionEvent e)
    {
       System.out.println("Closing window");
       System.exit(0);
    }
  }
}


BTW, JFrame's have a setDefaultCloseOperation() method that will control the close button on the frame. (The X button on Windows and Mac's)

Flat View: This topic has 1 reply on 1 page
Topic: HELP with depreciation method!!! Previous Topic   Next Topic Topic: URGENT - PRIME CHECKER ??

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use