//The Calculate and exit button does not appear every time i run the program... HELP PLEASE....

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



public class Assign_1 extends JFrame
{

    private static final long serialVersionUID = 1L;
    private JLabel quizOneL,quizTwoL,quizThreeL,quizFourL,AverageScoreL;
    private JTextField quizOneTF,quizTwoTF,quizThreeTF,quizFourTF,AverageScoreTF;
    private JTextField weightOneTF,weightTwoTF,weightThreeTF,weightFourTF;
    private JButton calculateB, exitB;

    private CalculateButtonHandler cbHandler;
    private ExitButtonHandler ebHandler;

    private static final int WIDTH = 400;
    private static final int HEIGHT = 300;

    public Assign_1()
    {


            quizOneL = new JLabel("Quiz One: ", SwingConstants.RIGHT);
           quizTwoL = new JLabel("Quiz Two: ", SwingConstants.RIGHT);
           quizThreeL = new JLabel("Quiz Three: ", SwingConstants.RIGHT);
           quizFourL = new JLabel("Quiz Four: ", SwingConstants.RIGHT);
           AverageScoreL = new JLabel ("Average Score: ", SwingConstants.RIGHT);


           quizOneTF = new JTextField(10);
           quizTwoTF = new JTextField(10);
           quizThreeTF = new JTextField(10);
           quizFourTF = new JTextField(10);
           weightOneTF = new JTextField(10);
           weightTwoTF = new JTextField(10);
           weightThreeTF = new JTextField(10);
           weightFourTF = new JTextField(10);
           AverageScoreTF = new JTextField (10);

           calculateB = new JButton ("Calculate");
           cbHandler = new CalculateButtonHandler();
           calculateB.addActionListener(cbHandler);

           exitB = new JButton ("Quit");
           ebHandler = new ExitButtonHandler();
           exitB.addActionListener(ebHandler);

           setTitle ("Weighted Average Computation");

           Container pane = getContentPane ();

           pane.setLayout(new GridLayout (6,4));


           pane.add(quizOneL);
           pane.add(quizOneTF);
           pane.add(weightOneTF);
           pane.add(quizTwoL);
           pane.add(quizTwoTF);
           pane.add(weightTwoTF);
           pane.add(quizThreeL);
           pane.add(quizThreeTF);
           pane.add(weightThreeTF);
           pane.add(quizFourL);
           pane.add(quizFourTF);
           pane.add(weightFourTF);
           pane.add(AverageScoreL);
           pane.add(AverageScoreTF);

           setSize(WIDTH, HEIGHT);
           setVisible (true);
           setDefaultCloseOperation(EXIT_ON_CLOSE);
           }


    private class CalculateButtonHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            double quizOne,quizTwo,quizThree,quizFour,AverageScore;

            quizOne = Double.parseDouble(quizOneTF.getText());
            quizTwo = Double.parseDouble(quizTwoTF.getText());
            quizThree = Double.parseDouble(quizThreeTF.getText());
            quizFour = Double.parseDouble(quizFourTF.getText());
            AverageScore = (quizOne+quizTwo+quizThree+quizFour)/4;

            AverageScoreTF.setText("" + AverageScore);


        }
    }


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


    public static void main(String[] args)
    {
            Assign_1 rectObject = new Assign_1();
    }
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

add buttons same as txtfields or labels

i dont get it... how do you do it?

//...
        // pane.setLayout(new GridLayout(6, 4));
        pane.setLayout(new GridLayout(0, 3));// use this kind of GridLayout-constructor , define 3 columns

        pane.add(quizOneL);
        pane.add(quizOneTF);
        pane.add(weightOneTF);
        pane.add(quizTwoL);
        pane.add(quizTwoTF);
        pane.add(weightTwoTF);
        pane.add(quizThreeL);
        pane.add(quizThreeTF);
        pane.add(weightThreeTF);
        pane.add(quizFourL);
        pane.add(quizFourTF);
        pane.add(weightFourTF);
        pane.add(AverageScoreL);
        pane.add(AverageScoreTF);
        pane.add(new JLabel());//as empty cell
        pane.add(new JLabel());//as empty cell
        pane.add(calculateB);
        pane.add(exitB);

        setSize(WIDTH, HEIGHT);
        //...
This article has been dead for over six months. Start a new discussion instead.