Question: I am having trouble implementing ActionListener to make an existing lab's buttons interactive. Here is the error I am getting: Exception in thread main java.lang.ExceptionInInitializerError
I am having trouble implementing ActionListener to make an existing lab's buttons interactive. Here is the error I am getting: Exception in thread "main" java.lang.ExceptionInInitializerError
at ControlPanel.
at MainFrame.
at app.main(app.java:11)
Caused by: java.lang.RuntimeException: Uncompilable source code - CenterPanel is not abstract and does not override abstract method actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener
at CenterPanel.
... 3 more
I am trying to use my existing code from prior labs as well as follow the example given by my professor, but I can't figure out what the issue is.
Here is my code:
app.java
public class app {
public static void main(String args[]) {
student st1 = new student("Student", "One", 30);
student st2 = new student("Etudiant", "Two", 25);
student st3 = new student("Student", "Wagner", 28);
student st4 = new student("Jeff", "Goldblum", 66);
group gp = new group("Nittany Lions", st1, st2, st3, st4);
MainFrame mjf = new MainFrame(gp);
}
}
______________________________________________________________________________________________________
group.java
public class group {
String name;
student st1;
student st2;
student st3;
student st4;
group(String g1, student st1, student st2, student st3, student st4) {
this.name = g1;
this.st1 = st1;
this.st2 = st2;
this.st3 = st3;
this.st4 = st4;
}
String getInfo() {
return name;
}
double groupGPA() {
double avgGPA = (st1.gpa + st2.gpa + st3.gpa + st4.gpa) / 4;
return avgGPA;
}
}
_____________________________________________________________________________________________________
student.java
public class student {
String fName;
String lName;
int age;
double gpa;
student(String fName, String lName, int age) {
this.fName = fName;
this.lName = lName;
this.age = age;
this.gpa = semesterGPA();
}
String getName() {
return fName + " " + lName;
}
double semesterGPA() {
double sgpa = Math.random() * 4.0;
return sgpa;
}
}
_____________________________________________________________________________________________________
TopPanel.java
import java.awt.*;
import javax.swing.*;
public class TopPanel extends JPanel {
JButton jb1, jb2, jb3;
String jb3s;
public TopPanel(group group) {
super();
setBackground(Color.blue);
jb1 = new JButton(group.getInfo());
jb2 = new JButton("Group AVG GPA is");
jb3 = new JButton(String.format("%.2f", group.groupGPA()));
add(jb1);
add(jb2);
add(jb3);
}
}
_______________________________________________________________________________________________________
MainFrame.java
import java.awt.*;
import javax.swing.*;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
public class MainFrame extends JFrame {
ControlPanel mjp;
public MainFrame(group group) {
super("Sample Lab");
mjp = new ControlPanel(group);
getContentPane().add(mjp, "Center");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(800, 480);
setVisible(true);
}
}
________________________________________________________________________________________________________
ControlPanel.java
import java.awt.*;
import javax.swing.*;
public class ControlPanel extends JPanel {
CenterPanel cp;
TopPanel tp;
public ControlPanel(group group) {
super();
BorderLayout border = new BorderLayout();
setLayout(border);
setBackground(Color.gray);
tp = new TopPanel(group);
cp = new CenterPanel(group);
add(tp, "North");
add(cp, "Center");
}
}
_________________________________________________________________________________________________________
CenterPanel.java
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class CenterPanel extends JPanel implements ActionListener {
JButton jb1, jb2, jb3, jb4;
public CenterPanel(group group) {
super();
GridLayout grid = new GridLayout(4, 1);
setLayout(grid);
setBackground(Color.pink);
jb1 = new JButton("NAME = " + group.st1.getName() + "|GPA = "
+ String.format("%.2f", group.st1.gpa));
add(jb1);
jb2 = new JButton("NAME = " + group.st2.getName() + "|GPA = "
+ String.format("%.2f", group.st2.gpa));
add(jb2);
jb3 = new JButton("NAME = " + group.st3.getName() + "|GPA = "
+ String.format("%.2f", group.st3.gpa));
add(jb3);
jb4 = new JButton("NAME = " + group.st4.getName() + "|GPA = "
+ String.format("%.2f", group.st4.gpa));
add(jb4);
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
jb4.addActionListener(this);
@Override
public void actionPerformed
(ActionEvent event
)
{
if (event.getSource() == jb1) {
jb1.setText(g1.sd1.getName() + " " + g1.sd1.getGPA());
tp.jb3.setText(g1.averageGPA());
}
if (event.getSource() == jb2) {
jb2.setText(g1.sd2.getName() + " " + g1.sd2.getGPA());
tp.jb3.setText(g1.averageGPA());
}
if (event.getSource() == jb3) {
jb3.setText(g1.sd3.getName() + " " + g1.sd3.getGPA());
tp.jb3.setText(g1.averageGPA());
}
if (event.getSource() == jb4) {
jb4.setText(g1.sd4.getName() + " " + g1.sd4.getGPA());
tp.jb3.setText(g1.averageGPA());
}
}
}
The new requirement of the lab is that you implement the ActionListener in ControlPanel, which is the panel that contains the other two panels.
You have to use the traditional model for listening that you have been using so far: public class ControlPanel extends JPanel implements ActionListener
Thank you for your help!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
