Results 1 to 3 of 3
- 10-02-2012, 02:58 AM #1
Member
- Join Date
- Oct 2012
- Posts
- 1
- Rep Power
- 0
why public void clear() had error pls Help mE
hi there
this is my project
y if i run in NEtBeans
got some problem at public void clear, but the program still can run , an output process still can use ,
pls someone help me..
i got presentation on 02-oct-2012 , 12.am-1.pm Malaysian time
///////////////////////////////////
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import javax.swing.*;
public class GpaCalculator2 extends JFrame // within frame of window
implements ActionListener { // for buttons, textfields,...
private static final int GPA = 20;
private static float[] creditsArray = new float[GPA];
private static float[] gradesArray = new float[GPA];
// String
// textfields
private JTextField nameField, creditsField, gradeField, gpaField, statusField;
// buttons
private JButton updateButton, calculateButton, clearButton, exitButton,
helpButton;
// set up GUI
public GpaCalculator2()
{
super( "GPA Calculator" ); // title
Container container = getContentPane(); // frame
container.setLayout( new GridLayout( 6, 4 ) ); // layout within frame
container.add(
new JLabel( "Student Name",SwingConstants.CENTER ) );
nameField = new JTextField( 10 );
container.add( nameField );
nameField.addActionListener( this );
// Credits label and text input field
container.add(
new JLabel( "Class Credits(hours)",SwingConstants.CENTER ) );
creditsField = new JTextField( 10 );
container.add( creditsField );
creditsField.addActionListener( this);
// Grade label and text input field
container.add(
new JLabel( "Grade for class",SwingConstants.CENTER ) );
gradeField = new JTextField( 10 );
container.add( gradeField );
gradeField.addActionListener( this );
// GPA label and text ouput field
container.add(
new JLabel( "Current GPA",SwingConstants.CENTER ) );
gpaField = new JTextField( 10 );
gpaField.setEditable( false );
container.add( gpaField );
// Status of current input
statusField = new JTextField( 15 );
statusField.setEditable( false );
container.add( statusField );
// create and add buttons
calculateButton = new JButton( "CALCULATE" );
updateButton = new JButton( "UPDATE" );
clearButton = new JButton( "CLEAR" );
container.add( updateButton );
updateButton.addActionListener( this );
container.add( calculateButton );
calculateButton.addActionListener( this );
container.add( clearButton );
clearButton.addActionListener( this );
// size of window, as seen by user
setSize( 800, 400 );
setVisible( true );
}
// what happens when an object that has an ActionListener is clicked, inputed,...
public void actionPerformed( ActionEvent event ) {
DecimalFormat precision2 = new DecimalFormat( "0.00" );
// update button listener
if ( event.getSource() == updateButton ) {
if ( !( gradeField.getText().equals( "" ) ) || !( creditsField.getText().equals( "" ) ) ) {
for (int i = 0; i < gradesArray.length; i++) {
if ( ( gradesArray[ i ] == -999 ) && ( creditsArray[ i ] == -999 ) ) {
try {
gradesArray[ i ] = Float.parseFloat( gradeField.getText() );
creditsArray[ i ] = Float.parseFloat( creditsField.getText() );
statusField.setText("StuName : "+(" ")+ " Grade: " + gradeField.getText()+(" ") + " Credits: " + creditsField.getText() );
creditsField.setText( " " );
gradeField.setText( " " );
nameField.setText(" ");
}
catch ( NumberFormatException numberformat ) {
JOptionPane.showMessageDialog( null,
"Type in numbers for both fields",
"ERROR", JOptionPane.ERROR_MESSAGE );
if ( !gradeField.getText().equals( "" ) ) {
System.out.print( " Grade: " + gradeField.getText() );
}
if ( !creditsField.getText().equals( "" ) ) {
System.out.print( " Credits: " + creditsField.getText() );
}
}
catch ( Exception numberformat ) {
JOptionPane.showMessageDialog( null, "Please input a number in both fields","NUMBER FORMAT ERROR", JOptionPane.ERROR_MESSAGE );
}
break;
}
else if ( GPA == gradesArray.length ) {
clear();
}
}
}
}
// calculate button listener
else if ( event.getSource() == calculateButton ) {
float total_credits = 0;
float total_grade = 0;
for ( int i = 0; i < creditsArray.length; i++ ) {
if ( creditsArray[ i ] != -999 ) {
total_credits += creditsArray[ i ];
}
}
for ( int i = 0; i < gradesArray.length; i++ ) {
if ( gradesArray[ i ] != -999 ) {
float gradesTemp = gradesArray[ i ];
float creditsTemp = creditsArray[ i ];
float gpa;
gpa = creditsTemp / total_credits * gradesTemp;
total_grade = total_grade + gpa;
}
}
gpaField.setText( "" + precision2.format( total_grade ) );
}
// clear button listener get input then clear all
else if ( event.getSource() == clearButton ) {
clear();
creditsField.setText("");
gradeField.setText("");
nameField.setText(" ");
}
// Clear all
public void clear() {
for ( int i = 0; i < gradesArray.length; i++ ) {
gradesArray[ i ] = -999;
creditsArray[ i ] = -999;
}
statusField.setText("");
gpaField.setText("");
}
// execute application
public static void main( String args[] )
{
GpaCalculator2 application = new GpaCalculator2();
application.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE );
}
}
-
Re: why public void clear() had error pls Help mE
If your code is producing errors and you need help, it's wise to post the errors so that we know what they are.
Also please wrap your code in code tags so that it is readable. Please read the links in my signature below to see how to do this.
- 10-02-2012, 10:12 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Similar Threads
-
error: Missing method, body, or declare abstract public static void main
By MBD in forum New To JavaReplies: 2Last Post: 09-27-2011, 03:59 PM -
This is an error? public static void main(String args[])
By Jackount in forum New To JavaReplies: 10Last Post: 07-10-2011, 08:37 AM -
What wrong with it?case : public static void main
By bytescode in forum New To JavaReplies: 7Last Post: 02-10-2011, 07:01 AM -
What is the difference between Public Static Void and Public Void?
By whateverme in forum New To JavaReplies: 1Last Post: 12-04-2010, 05:41 PM -
Error: LengthCharAt.java:3: ';' expected public static void main (String[] args)
By antgaudi in forum New To JavaReplies: 9Last Post: 11-22-2008, 11:03 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks