Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: My GUI is EWWIE please help!

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    1
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default My GUI is EWWIE please help!

    Im taking Java at my local CC and so far i am so lost. My teacher knows what she is doing as far as programming but she is a horrible teacher, no one in the class understands the material and she will not explain herself or try to teach another way. With that said i have a program i would like help with or corrected if someone wouldn't mind. My code below i cant get to run and also i would like my program to be more organized with the buttons in a certain order and size. Thanks for your time

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

    public class Robert_Bachman_JFrame extends JFrame
    {
    private static final int WIDTH = 550; private static final int HEIGHT = 430;
    private ExitButtonHandler ebHandler; private ClearButtonHandler cbHandler;
    private JLabel testL, numOfGradeL, averageL, TopGradeL;
    private JButton enterGradeB, clearB, exitB;
    private JTextField testTF, numOfGradeTF, averageTF, TopGradeTF;
    private EnterGradeButtonHandler esbHandler;

    double totalGrade; int topGrade; int numOfGrade;


    public Robert_Bachman_JFrame()
    {
    //Title
    setTitle("Test Scores");

    // Text fields
    averageTF = new JTextField(10);
    testTF = new JTextField(10);
    TopGradeTF = new JTextField(10);
    numOfGradeTF = new JTextField(10);


    // labels
    TopGradeL = new JLabel("Top score: ", SwingConstants.RIGHT);
    numOfGradeL = new JLabel("Number of scores: ", SwingConstants.RIGHT);
    averageL = new JLabel("Average score: ", SwingConstants.RIGHT);

    testL = new JLabel("Test score: ", SwingConstants.RIGHT);

    // Clear button
    cbHandler = new ClearButtonHandler();
    clearB = new JButton("Clear");
    clearB.addActionListener(cbHandler);

    //Enter score button
    enterGradeB = new JButton("Enter score");
    enterGradeB.addActionListener(esbHandler);
    esbHandler = new EnterGradeButtonHandler();

    //Get container
    Container pane = getContentPane();

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

    //layout
    pane.setLayout(new GridLayout(6, 3));

    //Set size
    setSize(WIDTH, HEIGHT);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    //Place components
    pane.add(testL); pane.add(testTF);
    pane.add(numOfGradeL); pane.add(numOfGradeTF);
    pane.add(averageL); pane.add(averageTF);
    pane.add(TopGradeL); pane.add(TopGradeTF);
    pane.add(enterGradeB); pane.add(clearB);
    pane.add(exitB);

    //Disable text
    numOfGradeTF.setEditable(false);
    averageTF.setEditable(false);
    TopGradeTF.setEditable(false);
    }


    private class ClearButtonHandler implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    testTF.setText("");
    topGrade = 0;
    averageTF.setText("");
    numOfGradeTF.setText("");
    numOfGrade = 0;
    TopGradeTF.setText("");
    testTF.requestFocusInWindow();

    }
    }
    private class EnterGradeButtonHandler implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    double test, average;
    DecimalFormat oneDecimal = new DecimalFormat("0.0");

    test = Double.parseDouble(testTF.getText());
    totalGrade += test;
    numOfGrade++;
    average = (totalGrade / numOfGrade);
    topGrade = (int) larger(topGrade, test);

    numOfGradeTF.setText("" + numOfGrade);
    averageTF.setText("" + oneDecimal.format(average));
    TopGradeTF.setText("" + topGrade);

    testTF.requestFocusInWindow();
    testTF.selectAll();
    }
    public double larger(double topGrade, double grade)
    {
    if (topGrade >= grade)
    return topGrade;
    else
    return grade;
    }

    }

    private class ExitButtonHandler implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    System.exit(0);
    }
    }

    public class New_Class
    {

    public void main(String[] args)
    {
    New_Class A5Object = new New_Class();
    }
    }

    }


  2. #2
    Junior Member
    Join Date
    Nov 2011
    Location
    china
    Posts
    12
    My Mood
    Sad
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: My GUI is EWWIE please help!

    your main method shouldn`t in your Internal class
    and you should new Robert_Bachman_JFrame in your main method

  3. The Following User Says Thank You to jcloud For This Useful Post:

    castiel_1977 (December 2nd, 2011)

  4. #3
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: My GUI is EWWIE please help!

    Quote Originally Posted by jcloud View Post
    your main method shouldn`t in your Internal class
    and you should new Robert_Bachman_JFrame in your main method
    Just give him a hint.

    @castiel: Yes your program will hopefully compile but it will never show what you want. Reason is simple, you are not trying to call the drawing components anywhere in your whole program. As stated by jCloud, you are simply instantiating the object of the class which can't do anything except helping you to run main(), (specifically the code).
    New_Class A5Object = new New_Class();
    What do you think, this line will do?

  5. The Following User Says Thank You to Mr.777 For This Useful Post:

    castiel_1977 (December 2nd, 2011)

  6. #4
    Junior Member
    Join Date
    Nov 2011
    Location
    china
    Posts
    12
    My Mood
    Sad
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: My GUI is EWWIE please help!

    @Mr.777 I want to give him a hit , but I don`t know how to say .
    by the way,My English is very poor