New to JLabel and JTextArea...


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: New to JLabel and JTextArea...

  1. #1
    Join Date
    Jul 2006
    Posts
    1

    Post New to JLabel and JTextArea...

    Hey guys,

    I can't seem to make my file work. I'm trying to use JLabel and JTextArea to show a group of student's average with an equivalent letter grade. The user will be inputting the student name, id and 2 test scores. I'm getting "invalid method declaration", "illegal start of expression", etc. Please help me debug!

    Thanks!
    [CODE]
    import java.awt.*;
    import java.awt.event.*;
    import java.text.*;
    import javax.swing.*;
    import javax.swing.border.*;

    public class StudentGrades2 extends JFrame
    {
    // JPanel for user inputs
    private JPanel inputGradeJPanel;

    // JLabel and JTextField for student name
    private JLabel studentNameJLabel;
    private JTextField studentNameJTextField;

    // JLabel and JTextField for student ID
    private JLabel studentIDJLabel;
    private JTextField studentIDJTextField;

    // JLabel and JTextField for test1 score
    private JLabel test1JLabel;
    private JTextField test2sJTextField;

    // JLabel and JTextField for test2s score
    private JLabel test2sJLabel;
    private JTextField test2sJTextField;

    // JButton to calculate student grade
    private JButton submitGradesJButton;

    // JLabel and JTextArea to display student grade
    private JLabel displayJLabel;
    private JTextArea displayJTextArea;

    // JButton to show student letter grade
    private JButton submitGradesJButton;

    // JLabel and JTextArea to display student letter grade
    private JLabel displayJLabel;
    private JTextArea displayJTextArea;

    // initialize number of students to zero
    private int studentCount = 0;

    // constants
    private final int number_of_tests = 2;
    private final int maximum_students = 10;
    private final int test1_test = 0;
    private final int test2_test = 1;

    // one-dimensional array to store student names
    private String studentNames[] = new String[ maximum_students ];

    // two-dimensional array to store student grades
    private int studentGrades[][] =
    new int[ maximum_students ][ number_of_tests ];

    // DecimalFormat for two digits of precision
    private DecimalFormat twoDigits = new DecimalFormat( "0.00" );

    // no-argument constructor
    public StudentGrades();
    {
    createUserInterface();
    }

    // create and position GUI components; register event handlers
    private void createUserInterface()
    {
    Container contentPane = getContentPane();

    contentPane.setLayout( null );

    // set up inputGradeJPanel
    inputGradeJPanel = new JPanel();
    inputGradeJPanel.setBounds( 16, 16, 208, 218 );
    inputGradeJPanel.setBorder(
    new TitledBorder( "Input Grade" ) );
    inputGradeJPanel.setLayout( null );
    contentPane.add( inputGradeJPanel );

    // set up studentNameJLabel
    studentNameJLabel = new JLabel();
    studentNameJLabel.setBounds( 8, 32, 90, 23 );
    studentNameJLabel.setText( "Student Name: " );
    inputGradeJPanel.add( studentNameJLabel );

    // set up studentNameJTextField
    studentNameJTextField = new JTextField();
    studentNameJTextField.setBounds( 104, 32, 88, 21 );
    studentNameJTextField.setHorizontalAlignment(
    JTextField.RIGHT );
    inputGradeJPanel.add( studentNameJTextField );

    // set up studentIDJLabel
    studentNameJLabel = new JLabel();
    studentNameJLabel.setBounds( 8, 32, 90, 23 );
    studentNameJLabel.setText( "Student ID: " );
    inputGradeJPanel.add( studentNameJLabel );

    // set up studentIDJTextField
    studentNameJTextField = new JTextField();
    studentNameJTextField.setBounds( 104, 32, 88, 21 );
    studentNameJTextField.setHorizontalAlignment(
    JTextField.RIGHT );
    inputGradeJPanel.add( studentIDJTextField );

    // set up test1JLabel
    test1JLabel = new JLabel();
    test1JLabel.setBounds( 8, 74, 60, 23 );
    test1JLabel.setText( "Midterm: " );
    inputGradeJPanel.add( test1JLabel );

    // set up test1JTextField
    test1JTextField = new JTextField();
    test1JTextField.setBounds( 136, 74, 56, 21 );
    test1JTextField.setHorizontalAlignment( JTextField.RIGHT );
    inputGradeJPanel.add( test1JTextField );

    // set up test2JLabel
    test2JLabel = new JLabel();
    test2JLabel.setBounds( 8, 74, 60, 23 );
    test2JLabel.setText( "Finals: " );
    inputGradeJPanel.add( test2JLabel );

    // set up test2JTextField
    test2JTextField = new JTextField();
    test2JTextField.setBounds( 136, 74, 56, 21 );
    test2JTextField.setHorizontalAlignment( JTextField.RIGHT );
    inputGradeJPanel.add( test2JTextField );

    // set up submitGradesJButton
    submitGradesJButton = new JButton();
    submitGradesJButton.setBounds( 72, 182, 120, 24 );
    submitGradesJButton.setText( "Submit Grades" );
    inputGradeJPanel.add( submitGradesJButton );
    submitGradesJButton.addActionListener(

    new ActionListener() // anonymous inner class
    {
    // event handler called when submitGradesJButton
    // is clicked
    public void actionPerformed( ActionEvent event )
    {
    submitGradesJButtonActionPerformed( event );
    }

    } // end anonymous inner class

    ); // end call to addActionListener

    // set up displayJLabel
    displayJLabel = new JLabel();
    displayJLabel.setBounds( 240, 16, 150, 23 );
    displayJLabel.setText( "Grade:" );
    contentPane.add( displayJLabel );

    // set up displayJTextArea
    displayJTextArea = new JTextArea();
    displayJTextArea.setBounds( 240, 48, 402, 184 );
    displayJTextArea.setEditable( false );
    contentPane.add( displayJTextArea );

    // set properties of application's window
    setTitle( "Student Grades for CIS190 Online Programming" ); // set title bar string
    setSize( 670, 308 ); // set window size
    setVisible( true ); // display window

    } // end method createUserInterface

    // convert a number to a letter grade
    private String convertToLetterGrade( double grade )
    {
    if ( grade >= 90 )
    {
    return "A";
    }
    else if ( grade >= 80 )
    {
    return "B";
    }
    else if ( grade >= 70 )
    {
    return "C";
    }
    else if ( grade >= 60 )
    {
    return "D";
    }
    else
    {
    return "F";
    }

    } // end method convertToLetterGrade

    // calculate and display the student and class average
    private void submitGradesJButtonActionPerformed(
    ActionEvent event )
    {
    // get user input
    String nameOfStudent = studentNameJTextField.getText();
    String studentID = studentIDJTextField.getText();
    int test1 = Integer.parseInt( test1JTextField.getText() );
    int test2 = Integer.parseInt( test2JTextField.getText() );

    // add user input to arrays
    studentNames[ studentCount ] = nameOfStudent;
    studentID [studentID ] = studentID;
    studentGrades[ studentCount ][ test1_test ] = test1;
    studentGrades[ studentCount ][ test2_test ] = test2;

    // clear other JTextFields for new data
    studentNameJTextField.setText( "" );
    studentIDJTextField.setText( "" );
    test1JTextField.setText( "" );
    test2JTextField.setText( "" );

    } // end method submitGradesJButtonActionPerformed

    // display student grades and averages as numbers
    private void displayNumericGrades()
    {
    // add a header to displayJTextArea
    displayJTextArea.setText(
    "Name\t ID\t Midterm\t Finals\t Average\t Grade\n" );

    int studentTotal = 0; // store the student's total grades

    for ( int student = 0; student < studentCount; student++ )
    {
    // display student names
    displayJTextArea.append( studentNames[ student ] + "\t" );

    studentTotal = 0; // initialize the student's total grades

    for ( int test = 0; test < number_of_test; test++ )
    {
    { // display student ID
    displayJTextArea.append( studentID[ ID ] + "\t" );

    studentTotal = 0; // store the student's ID

    for ( int test = 0; test < number_of_test; test++ )
    {
    // append each test grade to displayJTextArea
    displayJTextArea.append(
    studentGrades[ student ][ test ] + "\t" );

    // add the test grade to the student's total
    studentTotal += studentGrades[ student ][ test ];

    } // end inner for

    // calculate the student average and display it
    double studentAverage =
    ( double ) studentTotal / number_of_test;
    displayJTextArea.append(
    twoDigits.format( studentAverage ) + "\n" );

    // add a header to displayJTextArea
    displayJTextArea.setText(
    "Name\tID\ttest1 \ttest2 \tAverage\t Grade\n" );

    int studentTotal = 0; // store the student's total grades

    for ( int student = 0; student < studentCount; student++ )
    {
    // display student names
    displayJTextArea.append( studentNames[ student ] + "\t" );

    studentTotal = 0; // initialize the student's total grades

    for ( int test = 0; test < number_of_test; test++ )
    {
    // append each test grade to displayJTextArea
    displayJTextArea.append( convertToLetterGrade(
    studentGrades[ student ][ test ] ) + "\t" );

    }
    }

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    You have to check your parenthesis usage (not paired properly), several of your variables are not declared and the constructor method's name is not the same as the class' name.
    eschew obfuscation

Similar Threads

  1. help with a Car Dealer program
    By kyuu in forum Java
    Replies: 3
    Last Post: 05-08-2008, 06:00 AM
  2. Replies: 7
    Last Post: 03-07-2006, 04:34 PM
  3. Unable to bring down the size of a panel
    By raghavan20 in forum Java
    Replies: 1
    Last Post: 12-22-2005, 04:11 PM
  4. JScrollPane with JTextArea
    By ngo004 in forum Java
    Replies: 1
    Last Post: 12-05-2005, 05:40 PM
  5. appending to JLabel's text
    By Saphiroth in forum Java
    Replies: 3
    Last Post: 03-07-2005, 10:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center