Go Back   nV News Forums > Software Forums > Software Development

Newegg Daily Deals

Reply
 
Thread Tools
Old 03-27-07, 05: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
Old 03-27-07, 07:35 PM   #2
Head_slinger
DEP Hater :)
 
Head_slinger's Avatar
 
Join Date: Jul 2002
Location: Newcastle, UK
Posts: 277
Send a message via ICQ to Head_slinger
Default Re: For Those Who Know JAVA

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);
__________________
AMD Athlon 64 3500+
1gb Corsair XMS3200C2PT
Leadtek 6800GT
Audigy 2
ASUS A8V

Windows XP Prof./SuSe 8.2
Head_slinger is offline   Reply With Quote
Old 03-28-07, 08:24 PM   #3
rhink
Registered User
 
Join Date: Jul 2005
Posts: 434
Default Re: For Those Who Know JAVA

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".
rhink is offline   Reply With Quote
Old 03-29-07, 09:57 AM   #4
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 Re: For Those Who Know JAVA

Quote:
Originally Posted by rhink
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!!!
six_storm is offline   Reply With Quote
Reply


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


Similar Threads
Thread Thread Starter Forum Replies Last Post
Google (partially) loses suit to Oracle over use of Java API's ViN86 Mobile Devices And Smartphones 3 05-17-12 10:25 AM
Judge Declines To Rule That Google Pillaged Java News Archived News Items 0 05-10-12 07:00 AM
BAH! I'm being driven insane by Java Virtual Machine Kruno General Software 9 09-01-02 09:59 PM

All times are GMT -5. The time now is 03:57 PM.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright 1998 - 2014, nV News.