The eror says: llegal static declaration in inner class Question1.Question1Listener

modifier 'static' is only allowed in constant variable declarations

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author 1001334386
 */
import java.awt.*;
import javax.swing.*;
import java.awt.Event.*;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Question1 extends JFrame{

    JLabel txt1 = new JLabel("Number 1");
    JTextArea txtarea1 = new JTextArea();
    JLabel txt2 = new JLabel("Number 2");
    JTextArea txtarea2 = new JTextArea();
    JLabel resulttxt = new JLabel("Result");
    JTextArea result = new JTextArea ();

    JButton add = new JButton("Add");
    JButton subtract = new JButton("Subtract");
    JButton multiply = new JButton("Multiply");
    JButton divide = new JButton("Divide");




public Question1(){
JPanel panel = new JPanel(new GridLayout(2,5,5,5));
panel.add(txt1);
panel.add(txtarea1);
panel.add(txt2);
panel.add(txtarea2);
panel.add(resulttxt);
panel.add(result);
panel.add(add);
panel.add(subtract);
panel.add(multiply);
panel.add(divide);

Question1Listener q1listener = new Question1Listener();
    add.addActionListener(q1listener);
    subtract.addActionListener(q1listener);
    multiply.addActionListener(q1listener);
    divide.addActionListener(q1listener);

}



public class Question1Listener implements ActionListener{
public void actionPerformed(ActionEvent e){
    if (e.getSource() == add){
        String a = txt1.getText();
        int a1 = Integer.parseInt(a);
        String b = txt2.getText();
        int b1 = Integer.parseInt(b);
        int c = a1+b1;

    }

    else if (e.getSource()== subtract){
        String a = txt1.getText();
        int a1 = Integer.parseInt(a);
        String b = txt2.getText();
        int b1 = Integer.parseInt(b);
        int c = a1-b1;
    }

    else if (e.getSource()== multiply){
        String a = txt1.getText();
        int a1 = Integer.parseInt(a);
        String b = txt2.getText();
        int b1 = Integer.parseInt(b);
        int c = a1*b1;
    }

    else if (e.getSource()== divide){
        String a = txt1.getText();
        int a1 = Integer.parseInt(a);
        String b = txt2.getText();
        int b1 = Integer.parseInt(b);
        int c = a1*b1;
    }
}        




public static void main (String args []){
Question1 frame = new Question1();
frame.setLocationRelativeTo(null);
frame.setSize(500,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
}
    }
}

the error show in line 96 public static void main
the error is llegal static declaration in inner class Question1.Question1Listener

Looks like you messed up your brackets.
At line 66 you are still in the QuestionListener inner class, wheras I expect you inetnded to close that class definition first and have main as a method of the Question1 class.

ps: Always use your IDE's formatting to indent your code correctly. That's the quickest way to see errors lik ethis.

pps I tagged this Java for you - always tag with the language so the right people read it.

Edited 8 Months Ago by JamesCherrill

This article has been dead for over six months. Start a new discussion instead.