sirio sirio - 1 month ago 10
Java Question

Cannot call constructor component from ActionPerformed

When executing

bottomLabel.setVisible(true);
I get an unresolved compilation problem:

The type Assignment1 must implement the inherited abstract method ActionListener.actionPerformed(ActionEvent)


It is like that I should create a new variable as it is not readable from the constructor. Where is my mistake? Here is the code simplified as much as possible:

public class Assignment1 extends JFrame implements ActionListener{

//declare variables
int x= 101;
int low = 0;
int high = 100;
int guess = (high + low) / 2;
int counter = 0;

private static final long serialVersionUID = 1L;

//main method
//...some code...


//constructor
public Assignment1(){
//...some code...

//declare buttons
JButton correct = new JButton("correct!");

//add buttons
//...some code...

//declare TextField and Labels
JTextField numberTextField = new JTextField(20);
JLabel topLabel = new JLabel("T");
JLabel bottomLabel = new JLabel("G");

//add TextField and Labels and position them on the layout
//...some code...
bottomLabel.setBounds(110, 300, 400, 20);
add(bottomLabel);
bottomLabel.setVisible(false);

//add ActionListener to each button
//...some code...
correct.addActionListener(this);

}

@Override
//define ActionPerformed when an Event is parsed
public void actionPerformed(ActionEvent e) {
String buttonClicked = e.getActionCommand();

if(buttonClicked.equals("Yes, correct!")){
System.out.println("correct");
bottomLabel.setVisible(true);
}

}
}


Here's the full stack trace:

at Assignment1.Assignment1.actionPerformed(Assignment1.java:12)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Answer

bottomLabel is visible only inside the constructor, as it's not declared at class-level.

Declare it at class-level (as you did with x, low, high, etc) to make it work:

public class Assignment1 extends JFrame implements ActionListener {

    // declare variables
    int x = 101;
    int low = 0;
    int high = 100;
    int guess = (high + low) / 2;
    int counter = 0;
    JLabel bottomLabel;  // <==


    public Assignment1() {
        // [...] other assignments
        bottomLabel = new JLabel("Game Over, your number is NN, i got it in N times. Wanna play again?");
        // [...] rest of class
Comments