twc
Posts: 129
Nickname: twc
Registered: Feb, 2004
|
|
Re: Passing a parameter to Constructor error
|
Posted: Apr 14, 2004 11:48 AM
|
|
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)
|
|