View Single Post
Old 03-27-07, 06:56 PM   #1
six_storm
Registered User
 
six_storm's Avatar
 
Join Date: Jun 2004
Location: Nashvegas
Posts: 11,575
Send a message via Skype™ to six_storm
Default For Those Who Know JAVA

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:

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.
six_storm is offline   Reply With Quote