• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem with ActionListener

 
larsson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a class which creates an object called Game. Game is a superclass which extends JLabel and implements ActionListener. I declare actionPerformed as abstract in Game. I then have a class called Maths which is Game's subclass ie. class Maths extends Game
Here I declare my actionPerformed function.
I'm getting a compilation error at the header for the class Game ie. at the line
"abstract class Game extends JLabel implements ActionListener"
The error reads :
H:\jbproject\Countdown\SRC\countdown>c:\jdk1.3\bin\javac Countdown.java
.\Game.java:20: actionPerformed(java.awt.event.ActionEvent) in Game cannot imple
ment actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListene
r; attempting to assign weaker access privileges; was public
abstract class Game extends JLabel implements ActionListener
^
1 error
Could anyone help me with this very annoying error!!!
Cheers.
 
Thomas Suer
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How exactly did you declare the actionPerformed() method? You must use the same signature with the same access rights, that means it must be declared as public.
What I am wondering is why do you implement the method in your abstract class Game when you code it in the inherited class Maths?
Good luck
Tom
 
larsson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I declare the actionPerformed method in Game as
: abstract public void actionPerformed(ActionEvent e);
To answer your question if I put class Maths extends Game implements ActionListener (so the header of game is just : class Game extends JLabel and there's no declaration of ActionPerformed) i get the exact same error, only it occurs in Maths.java (at the line public class Maths extends Game implements ActionListener). I have no idea what "attempting to assign weaker access privileges" means. All my methods and classes are public.
Cheers for helping
Colm
 
Wilfried LAURENT
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by larsson:

"abstract class Game extends JLabel implements ActionListener"
The error reads :
... attempting to assign weaker access privileges; was public
abstract class Game extends JLabel implements ActionListener
^

You say that your class are all public but it seems looking at the code that Game is package protected. But normally it should not change a thing.
W.
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi larsson,
Your problem is that you are not understanding what abstract is doing for you. By making your class Game abstract you are telling the compiler that it is not a complete class and the subclasses are required to complete it. Knowing this you should be able to do the following:

The above code tells the compiler that Game doesn't actually implement the ActionListener but its' subclasses will.
Regards,
Manfred.
 
Wilfried LAURENT
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyway it should have worked. Larsson (you should re-register with a adequate UserName ....), can you provide us with your code?
W.
 
Chantal Ackermann
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys,
thats more a reply to the last posting. i'm sorry but i have to ask this. why isn't javaranch validating the user names? aren't there any programmers that can implement this function into the backend??
chantal
 
larsson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I'll re-register with my correct name (Colm Carberry). I only joined javaranch the other day and I'd no idea you should pick a proper username.
I'm still getting the same error (attempting to assign weaker access priveleges with ActionListener). Here is my code. Cheers for the help.
Countdown is my main program. I've only really started this program.

/**
* Title: Software Engineerying Project
* Description:
* Copyright: Copyright (c) 2001
* Company:
* @author Colm Carberry 98485008
* @version 1.0
*/

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.net.*;
import java.util.*;

public class Countdown
{
public Countdown()
{
}
public static void main(String[] args)
{
try
{

UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}//endtry
catch (Exception e)
{
System.out.println(e.toString());
}//endcatch
JFrame game1 = new JFrame("Maths");
game1.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Game maths = new Maths();

game1.getContentPane().add(maths, BorderLayout.CENTER);
game1.pack();
game1.setSize(1024,768);
game1.setVisible(true);
}//endmain
}//endClass

/**
* Title: Software Engineerying Project
* Description:
* Copyright: Copyright (c) 2001
* Company:
* @author Colm Carberry
* @version 1.0
*/
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.util.*;
import java.util.Timer;
abstract public class Game extends JPanel implements ActionListener
{
int score = 0;
String answer;
int timeUp=0;
Timer timer;
public Game()
{
setBackground(Color.white);
ImageIcon carol = new ImageIcon("carol.gif");
add(new JLabel("Countdown"));

add(new JLabel(carol));

}
//abstract void choose(); //#1
void Clock() //probably put in interface later
{
timer = new Timer();
timer.schedule(new Task(), 30*1000); //30 second timer
}
abstract int validateAnswer(String answer);
abstract String getAnswer();
//abstract void actionPerformed(ActionEvent e);

int getScore()
{
return score;
}
class Task extends TimerTask //inner class
{
public void run()
{
timer.cancel(); //Terminate the timer thread
add(new JLabel("Time's up!"));

//timeUP
//answer = getAnswer();
//score = validateAnswer(answer);
}
}

}//endClass

/**
* Title: Software Engineerying Project
* Description:
* Copyright: Copyright (c) 2001
* Company:
* @author Colm Carberry 98485008
* @version 1.0
*/
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.util.*;

public class Maths extends Game
{
JButton numButton;
JLabel welcome,choose,timerStarted;
JComboBox chooseLarge;
JTextField[] t = { new JTextField("One",5), new JTextField("Two",5), new JTextField("Three",5),
new JTextField("Four",5), new JTextField("Five",5), new JTextField("Six",5) };
JTextField targ;
JTextArea res;
String[] large = {"zero","one", "two", "three","four"};//user is allowd 0-4 ln
int numLarge;
static int nums[];
public Maths()
{
super();

//Initialise numLarge to 2 because this is the most common choice
numLarge = 2;

welcome = new JLabel("Welcome to the Maths Game");
choose = new JLabel("How many large numbers do you want?");
timerStarted = new JLabel("Clock has started - u have 30 seconds to think of the answer!");
numButton = new JButton("Get Random Numbers");
numButton.setActionCommand("numbers");
numButton.addActionListener(this);

targ = new JTextField("Target",5);

res = new JTextArea("Result",9,20);
chooseLarge = new JComboBox(large);
chooseLarge.setSelectedIndex(2);
chooseLarge.setActionCommand("combo");
chooseLarge.addActionListener(this);




//add items to Panel
add(welcome);
add(choose);
add(chooseLarge);

for(int i =0;i<6;i++)
{
add(t[i]); //will probably get rid of t eventually
}//endfor
add(targ);
add(numButton);

//res will probably go
add(res);
//add(buttonSolve);
res.setEditable(false);

}//endConstructor
void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("numbers"))
{
//get random numbers
int[] nums = choose(numLarge);
for (int i=0;i<6;i++)
{
String hold = new String();
hold +=nums[i];
t[i].setText(hold);
}//endfor
int target = getTarget();
String hold = new String();
hold +=target;
targ.setText(hold);
Clock();
add(timerStarted);
}//endif
if(e.getActionCommand().equals("combo"))
{
JComboBox temp = (JComboBox)e.getSource();
String choice = (String)temp.getSelectedItem();
if(choice.equals("zero"))
{
numLarge = 0;
}

else if(choice.equals("one"))
{
numLarge = 1;
}
else if(choice.equals("two"))
{
numLarge = 2;
}
else if(choice.equals("three"))
{
numLarge = 3;
}
else if(choice.equals("four"))
{
numLarge = 4;
}
}//endif

}//endMethod
int[] choose(int numL)
{
int numbers[] = new int[6];
for(int i=0;i<6;i++)
{
if(i <numL)>
{
numbers[i] = getBigNumber();
}
else
{
numbers[i] = getSmallNumber();
}
}//endfor
return numbers;
}//endMethod
int getBigNumber()
{
double big;
big = ((Math.random() *100)%4);
int big2 = (int) big;
big2 = (big2+1)*25;

return big2;
}//endMethod
int getSmallNumber()
{
double small;
small = ((Math.random()*100)%9);
int small2 = (int) small;

return (small2+1);
}//endMethod
int getTarget()
{
double tg = ((Math.random()*1000)%899);
int tg2 = (int) tg;
return (tg2+101);
}//endMethod

String getAnswer()
{
return "hello";
}
int validateAnswer(String ans)
{
System.out.println("validateAnswer");
return 1;
}
}//endClass
//package countdown;

 
Colm Carberry
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I solved my error.
This error is reported when the keyword public is missing in the actionPerformed method's header.

 
Wilfried LAURENT
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's exactely what Thomas told you in the second message....
W.
 
dan moore
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry but that was quite funny
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic