Do I need to have something in CenterPanel for the action of these buttons? Inst
ID: 3756215 • Letter: D
Question
Do I need to have something in CenterPanel for the action of these buttons?
Instructions:
You will be listening to the 4 buttons with student information. Every time a user clicks on one of the buttons, the GPA will be recalculated using semesterGPA and displayed again.
When a student GPA is recalculated, the group average GPA has to be updated too and displayed again.
Important - #1 - Where to implement the ActionListener?
It is a requirement that you implement the ActionListener in ControlPanel, which is the panel that contains the other two panels.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class ControlPanel extends JPanel implements ActionListener{
CenterPanel cp;
TopPanel tp;
group g1;
public ControlPanel()
{
super();
BorderLayout border = new BorderLayout();
setLayout(border);
setBackground(Color.gray);
student st1 = new student("Joshua", "James", 23);
student st2 = new student("Victoria", "Snyder", 32);
student st3 = new student("Conner", "Smith", 25);
student st4 = new student("Skyler", "Reese", 28);
student[] students = {st1, st2, st3, st4};
group g1 = new group("Nittany Lions");
g1.addStudents(students);
cp = new CenterPanel(g1);
cp.jb1.addActionListener(this);
tp = new TopPanel(g1);
add(tp, "North");
add(cp, "Center");
}
@Override
public void actionPerformed(ActionEvent e)
{
Object obj = e.getSource();
if (obj == cp.jb1) {
cp.jb1.setText("student gpa: " + g1.getStudentGPA(0));
tp.jb3.setText(""+ g1.getAverageGPA());
}
}
}
import java.awt.*;
import javax.swing.*;
public class CenterPanel extends JPanel
{
JButton jb1, jb2, jb3, jb4;
public CenterPanel(group g1)
{
super();
GridLayout grid = new GridLayout(4, 1);
setLayout(grid);
setBackground(Color.pink);
jb1 = new JButton("Name = " + g1.students[0].getName() + " " + g1.students[0].myGPA);
add(jb1);
jb2 = new JButton("Name = " + g1.students[1].getName() + " " + g1.students[1].myGPA);
add(jb2);
jb3 = new JButton("Name = " + g1.students[2].getName() + " " + g1.students[2].myGPA);
add(jb3);
jb4 = new JButton("Name = " + g1.students[3].getName() + " " + g1.students[3].myGPA);
add(jb4);
}
}
Explanation / Answer
1
Set Actions for each buttonsand add actionListner for that object button
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.