PDA

View Full Version : For Those Who Know JAVA


six_storm
03-27-07, 05:56 PM
I got a small problem. I'm writing a program that uses a Java GUI and some pop up dialog boxes. The only problem is that the program will compile but when executed, nothing happens. I'm using JGRASP and it works with my other programs. Here's the code:

// Jonathan Andrew Scott
// CSCI 1015
// Program Assignment 6
// Our First GUI Java Program

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Grades2 extends JFrame
{
// Initializes all variables
private JLabel totalQuizLabel1, totalQuizLabel2, totalProgLabel1, totalProgLabel2, midtermLabel1, midtermLabel2, likeHaveLabel, currentLabel, needFinalLabel;
private JTextField tq1TF, tq2TF, tp1TF, tp2TF, mt1TF, mt2TF, lhTF, cTF, nfTF;
private JButton calculateB, exitB, quizB, progB;

private CalculateButtonHandler cbHandler;
private ExitButtonHandler ebHandler;
private QuizButtonHandler qbHandler;
private ProgramButtonHandler pbHandler;

// What happens when you click the "Calculate" button
private class CalculateButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
double quiz1, quiz2, prog1, prog2, mid1, mid2, likeToHave, currentGrade, needFinalGrade, totalPos, totalAct;

quiz1 = Double.parseDouble(tq1TF.getText());
quiz2 = Double.parseDouble(tq2TF.getText());
prog1 = Double.parseDouble(tp1TF.getText());
prog2 = Double.parseDouble(tp2TF.getText());
mid1 = Double.parseDouble(mt1TF.getText());
mid2 = Double.parseDouble(mt2TF.getText());
likeToHave = Double.parseDouble(lhTF.getText());

totalPos = quiz2 + prog2 + mid2;
totalAct = quiz1 + prog1 + mid1;
needFinalGrade = ((likeToHave / 100) * totalPos) - totalAct;
currentGrade = (totalAct / totalPos) * 100;

nfTF.setText(String.format("%.2f", needFinalGrade));
cTF.setText(String.format("%.2f", currentGrade));
}
}

// What happens when you click the "Quiz" Button

private class QuizButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String num1str, num2str;
double num1, num2, quizAvg;

num1str = JOptionPane.showInputDialog("Enter your actual quiz score or type '-999' to terminate.");
num1 = Double.parseDouble(num1str);

num2str = JOptionPane.showInputDialog("Enter the total possible points for this quiz.");
num2 = Double.parseDouble(num2str);

quizAvg = average(num1, num2);
}
}

// What happens when you click the "Program" Button

private class ProgramButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String num1str, num2str;
double num1, num2, progAvg;

num1str = JOptionPane.showInputDialog("Enter your actual program score or type '-999' to terminate.");
num1 = Double.parseDouble(num1str);

num2str = JOptionPane.showInputDialog("Enter the total possible points for this program.");
num2 = Double.parseDouble(num2str);

progAvg = average(num1, num2);
}
}
// What happens when you click the "Exit" button
private class ExitButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}

public void Grades()
{
setTitle("Calculate Your Grade"); // Sets the title of the window

// Initializes the text fields
tq1TF = new JTextField(10);
tq2TF = new JTextField(10);
tp1TF = new JTextField(10);
tp2TF = new JTextField(10);
mt1TF = new JTextField(10);
mt2TF = new JTextField(10);
lhTF = new JTextField(10);
cTF = new JTextField(10);
nfTF = new JTextField(10);

// Quiz Button Properties
quizB = new JButton("Quiz Scores");
qbHandler = new QuizButtonHandler();
quizB.addActionListener(qbHandler);

// Program Button Properties
progB = new JButton("Program Scores");
pbHandler = new ProgramButtonHandler();
progB.addActionListener(pbHandler);

// Calculate Button Properties
calculateB = new JButton("Calculate");
cbHandler = new CalculateButtonHandler();
calculateB.addActionListener(cbHandler);

// Exit Button Properties
exitB = new JButton("Exit");
ebHandler = new ExitButtonHandler();
exitB.addActionListener(ebHandler);

totalQuizLabel1 = new JLabel("Enter your total quiz scores: ", SwingConstants.RIGHT);
totalQuizLabel2 = new JLabel("Enter the total quiz scores: ", SwingConstants.RIGHT);
totalProgLabel1 = new JLabel("Enter your total program scores: ", SwingConstants.RIGHT);
totalProgLabel2 = new JLabel("Enter the total program scores: ", SwingConstants.RIGHT);
midtermLabel1 = new JLabel("Enter your midterm score: ", SwingConstants.RIGHT);
midtermLabel2 = new JLabel("Enter the total midterm score: ", SwingConstants.RIGHT);
likeHaveLabel = new JLabel("Enter the grade you would like to have: ", SwingConstants.RIGHT);
currentLabel = new JLabel("Your current grade: ", SwingConstants.RIGHT);
needFinalLabel = new JLabel("Grade you need on the final: ", SwingConstants.RIGHT);

Container pane = getContentPane();
pane.setLayout(new GridLayout(13, 2)); // Layout of the Window in (Column, Rows) format
// Add panes to the window
pane.add(quizB);
pane.add(progB);
pane.add(totalQuizLabel1);
pane.add(tq1TF);
pane.add(totalQuizLabel2);
pane.add(tq2TF);
pane.add(totalProgLabel1);
pane.add(tp1TF);
pane.add(totalProgLabel2);
pane.add(tp2TF);
pane.add(midtermLabel1);
pane.add(mt1TF);
pane.add(midtermLabel2);
pane.add(mt2TF);
pane.add(likeHaveLabel);
pane.add(lhTF);
pane.add(currentLabel);
pane.add(cTF);
pane.add(needFinalLabel);
pane.add(nfTF);
pane.add(calculateB);
pane.add(exitB);

setSize(600, 600); // Size of the window
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE); // Close when the window is "X'd" out

}

public static double average(double act, double pos)
{
double avg;
avg = act / pos;
return avg;
}

public static void main(String[] args)
{
Grades2 gradeObject = new Grades2();
}
}

Any suggestions would be greatly appreciated. I just don't understand why I can't get anything to happen! Thanks.

Head_slinger
03-27-07, 07:35 PM
Hi i havnt tested this but just a suggestion.

Instead of calling setVisible(true) in the Constructor for Grades2 Do it in the main method.


Grades2 gradeObject = new Grades2();

gradeObject.setVisible(true);

rhink
03-28-07, 08:24 PM
The class is called Grades2 but your "constructor" is called "Grades". In other words, your constructor is never getting called. Name the class "Grades" or your constructor "Grades2".

six_storm
03-29-07, 09:57 AM
The class is called Grades2 but your "constructor" is called "Grades". In other words, your constructor is never getting called. Name the class "Grades" or your constructor "Grades2".

I've changed that and added a few other things. Just like before, it compiles right but when the program runs, nothing happens. I think I'm just going to turn it in because I have a major history test tomorrow and I need to concentrate on that. Thanks for the help guys!!!