Reputation: 1
I am making a GUI that calculates GPA and returns the GPA required in current classes in order to obtain a target GPA. I have the user input all the previous/current/future classes they take and I have several buttons that input this information into separate variables that I will access later for my calculations. However, none of my buttons execute anything. Can someone please explain to me what might be wrong with my code? I've tried two different methods (both featured in the example) and neither of them have worked.
import java.awt.BorderLayout;
import java.awt.ComponentOrientation;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Array;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class GUI extends JFrame implements ActionListener {
private FlowLayout flowlayout = new FlowLayout();
private GridLayout gridlayout = new GridLayout(6, 6);
private JLabel instructions;
private JLabel enteredclasses;
private JLabel status1, status2, status3, status4, status5, status6;
private JTextField classinput1, classinput2, classinput3, classinput4, classinput5, classinput6;
private JTextField hoursinput1, hoursinput2, hoursinput3, hoursinput4, hoursinput5, hoursinput6;
private JComboBox<String> gradeinput1, gradeinput2, gradeinput3, gradeinput4, gradeinput5, gradeinput6;
private JPanel panel2 = new JPanel();
private JButton add1, add2, add3, add4, add5, add6;
private JButton remove1, remove2, remove3, remove4, remove5, remove6;
private String[] grades = {"Select Grade (optional)", "A", "A-", "B+", "B", "B-", "C+", "C", "C-", "D+", "D", "D-", "F"};
private double gpa1, gpa2, gpa3, gpa4, gpa5, gpa6;
private ArrayList<Array> matrix;
private String[] row1 = new String[3];
private String[] row2 = new String[3];
private String[] row3 = new String[3];
private String[] row4 = new String[3];
private String[] row5 = new String[3];
private String[] row6 = new String[3];
public GUI(String name) {
super(name);
}
public void addComponentsToPanel(Container pane) {
JPanel panel1 = new JPanel();
panel1.setLayout(flowlayout);
flowlayout.setAlignment(FlowLayout.LEADING);
instructions = new JLabel("Input the correct information below.");
panel1.add(instructions);
panel2.setLayout(gridlayout);
//creates row 1
addUserInput(classinput1, hoursinput1);
addGrades(gradeinput1);
addButton(add1, "first");
removeButton(remove1, "second");
addStatus(status1);
//creates row 2
addUserInput(classinput2, hoursinput2);
addGrades(gradeinput2);
addButton(add2, "third");
removeButton(remove2, "fourth");
addStatus(status2);
//creates row 3
addUserInput(classinput3, hoursinput3);
addGrades(gradeinput3);
addButton(add3, "fifth");
removeButton(remove3, "sixth");
addStatus(status3);
.........
private static void createAndShowGUI() {
GUI frame = new GUI("GPA Calculator and Planner Design");
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.addComponentsToPanel(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
public void addUserInput(JTextField name, JTextField credithours) {
name = new JTextField("Course Name (optional)");
credithours = new JTextField("Credit Hours");
panel2.add(name);
panel2.add(credithours);
}
public void addGrades(JComboBox<String> choices) {
choices = new JComboBox<String>(grades);
panel2.add(choices);
}
public double convertGrade(String grade) {
String[] grades = {"Select Grade (optional)", "A", "A-", "B+", "B", "B-", "C+", "C", "C-", "D+", "D", "D-", "F"};
int index = 0;
for (int i = 0; i < 13; i++) {
if (grade.equals(grades[i])) {
index = i;
}
}
double[] gradeconversions = {0.0 , 4.0, 3.67, 3.3, 3.0, 2.67, 2.33, 2.0, 1.67, 1.33, 1.0, .67, 0.0};
return gradeconversions[index];
}
public void addButton(JButton b, String s) {
b = new JButton("ADD");
b.setActionCommand(s);
b.addActionListener(this);
panel2.add(b);
}
public void removeButton(JButton b, String s) {
b = new JButton("REMOVE");
b.setActionCommand(s);
b.addActionListener(this);
panel2.add(b);
}
public void addStatus(JLabel status) {
status = new JLabel("Status: -");
panel2.add(status);
}
public void addClasses() {
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == add1) {
row1[0] = classinput1.getText();
row1[1] = hoursinput1.getText();
row1[2] = (String) gradeinput1.getSelectedItem();
status1.setText("Status: Added");
}
if (e.getActionCommand().equals("third")) {
row2[0] = classinput1.getText();
row2[1] = hoursinput1.getText();
row2[2] = (String) gradeinput1.getSelectedItem();
status2.setText("Status: Added");
}
.....
Upvotes: 0
Views: 621
Reputation: 1260
Your actionPerformed
method if (e.getSource() == add1)
never going to be true
since JButton add1
is always null
, because addButton()
method never initialize variable add1
Therefore you have to declare JButton add1 = new JButton();
as a class variable and change addButton
method as follows
public void addButton(JButton b, String s) {
b.setText("ADD");
b.setActionCommand(s);
b.addActionListener(this);
panel2.add(b);
}
Apply same changes to removeButton
method as well
Upvotes: 2