Results 1 to 10 of 10

Thread: Can't Get GUI to align right (java)

  1. #1
    Banned
    Join Date
    May 2005
    Posts
    12

    Post Can't Get GUI to align right (java)

    I am making a simple GUI in java that computes my Grade Point Average so I can use it at the end of the quarter. If I can get this program to work it would help me out and would make my life a bit easier. (plus I want to impress a friend)

    My question is that I don't know how to align my panels correctly. I want them to look something like this hideous drawing:
    Code:
    ---------------------------------------------------------
    |Title                                                   |
    |--------------------------------------------------------|
    |        Class 1              |         Class 2          |
    |Grade?    Credit hour        | Grade?    Credit hour    | 
    |  o B      |   o 3           |                          |
    |  o C      |   o 4           |                          |
    |  o D      |   o 5           |                          |
    |--------------------------------------------------------|
    |        Class 4              |         Class 5          |
    |                             |                          |
    |                             |                          |
    |                             |                          |
    ---------------------------------------------------------|
    |   Calc    Clear             [ txt box]                 |
    ----------------------------------------------------------
    I only know how to align this with
    getContentPane().setLayout(new BorderLayout());
    getContentPane().add(p1, BorderLayout.NORTH);
    getContentPane().add(p2, BorderLayout.CENTER);
    getContentPane().add(p3, BorderLayout.SOUTH);
    And this simply will not work.

    This may be a bit hard to decipher but this is my code. Keep in mind that it is not finished, I just wanted to get the structure down before I start adding everything else.
    Code:
    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.*;
    
    public class GPAcalc extends JFrame implements ActionListener
    {
      //Declare Radio buttons and the necessary buttons and text fields
      JRadioButton jRBn2,jRBa,jRBb,jRBc,jRBd,jRB2a,jRB2b,jRB2c,jRB2d,jRB3a,jRB3b,jRB3c,jRB3d,jRB4a,jRB4b,jRB4c,jRB4d;
      JTextField jTF1;
      JButton jB1,jB2;
    
      /*The main method **/
      public static void main(String[] args)
      {
        GPAcalc frame = new GPAcalc();
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
      } // End of main method
    
    
      public GPAcalc()
      {
          setTitle("GPA Calculator");
    
         JPanel p1 = new JPanel();
          ButtonGroup bg = new ButtonGroup();
    
         //add the Radio Buttons to a Panel
          p1.setLayout(new GridLayout(5,1));
          p1.add(new JLabel("Class 1"));
          p1.add(jRBa = new JRadioButton("A",true));
          p1.add(jRBb = new JRadioButton("B"));
          p1.add(jRBc = new JRadioButton("C"));
          p1.add(jRBd = new JRadioButton("D"));
          bg.add(jRBa);
          bg.add(jRBb);
          bg.add(jRBc);
          bg.add(jRBd);
    
    
          JPanel p4 = new JPanel();
          ButtonGroup bg1 = new ButtonGroup();
    
          p4.setLayout(new GridLayout(4,1));
          p4.add(jRBn2 = new JRadioButton("2"));
          bg1.add(jRBn2);
    
          //add the textfield to a panel
          JPanel p2 = new JPanel();
          p2.setLayout(new FlowLayout());
          p2.add(new JLabel("The Button Selected is"));
          p2.add(jTF1 = new JTextField(20));
          jTF1.setEditable(false);
    
          //add the buttons to a panel
          JPanel p3 = new JPanel();
          p3.setLayout(new FlowLayout());
          p3.add(jB1 = new JButton("Button?"));
          p3.add(jB2 = new JButton("Clear"));
    
          // Set FlowLayout for the frame and add panels to the frame
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(p1, BorderLayout.NORTH);
        getContentPane().add(p2, BorderLayout.CENTER);
        getContentPane().add(p3, BorderLayout.SOUTH);
    
    
        // Register listener
        jB1.addActionListener(this);
        jB2.addActionListener(this);
        //Without the above two lines the buttons will not work
      } // End of Constructor
    
      public void actionPerformed(ActionEvent e)
        {
       int a1=0,a2=0,a3=0,a4=0;
       int b1=0,b2=0,b3=0,b4=0;
       int c1=0,c2=0,c3=0,c4=0;
       int d1=0,d2=0,d3=0,d4=0;
       int two1=0,two2=0,two3=0,two4=0;
       int thr1=0,thr2=0,thr3=0,thr4=0;
       int four1=0,four2=0,four3=0,four4=0;
       int five1=0,five2=0,five3=0,five4=0;
    
          if (e.getSource() == jB1)
          {
    
            // Get the radio button selected
            if (jRBa.isSelected())
              {
                      a1 = 4;
             }
            else if (jRB2a.isSelected())
            {
                      a2 = 4;
              }
              else if (jRB3a.isSelected())
            {
                      a3 = 4;
              }
              else if (jRB4a.isSelected())
                      {
                      a4 = 4;
                        }
    
    
            // Set result in TextField jTF1
            //jTF1.setText(x);   FIX THIS!
          }
          if(e.getSource() == jB2)
          {
                          jTF1.setText(" ");
           }
        }//End of ActionPerformed
      }//End of class
    I hope someone out there can help me with this.
    Thank you
    -Any

  2. #2
    Senior Member
    Join Date
    Nov 2001
    Posts
    1,255

    Re: Can't Get GUI to align right (java)

    Originally posted here by Any
    I am making a simple GUI in java that computes my Grade Point Average so I can use it at the end of the quarter. If I can get this program to work it would help me out and would make my life a bit easier. (plus I want to impress a friend)

    My question is that I don't know how to align my panels correctly. I want them to look something like this hideous drawing:
    [...]
    I hope someone out there can help me with this.
    For the basic layout you want, you have a couple of approaches. For simplicity's sake, you could go with a BorderLayout, but that kinda limits where you can go, so I would go with a GridLayout with two columns and four rows (for now). What I'd end up doing would be:
    Code:
    ------------------------------------------------------
    |                        A                            |
    |-----------------------------------------------------|
    |           B             |             C             |
    |-------------------------|---------------------------|
    |           D             |             E             |
    |-----------------------------------------------------|
    |                         F                           |
    ------------------------------------------------------
    A and F have a column width property of 2, and would contain the strings, buttons, etc, you want.
    B, C, D, and E will contain JPanels that then have the data you want in them nested. Once you get enough Kung foo with Java's gui system, you'll be able to basically use GridBagLayout to do everything you want.

    Sun's provided a LOAD of tutorials to promote the use of Java, and one you might find especially useful is the GUI tutorial.
    http://java.sun.com/docs/books/tutor...ing/index.html
    Specifically, the section on laying out components.

    Edit: BTW, give yourself some sanity and make some radio button arrays instead of useing jrbA,B,C,D, etc... You should probably also be giving your list of A, B, C, and D a default value, like C or D. Otherwise you could have situations occur where you are calculating a value from none of the radbuttons. Might be even better to add an "N/A" radio button, and make that the default option, so you can check for it in calculations and ignore the entire sets.
    Chris Shepherd
    The Nelson-Shepherd cutoff: The point at which you realise someone is an idiot while trying to help them.
    \"Well as far as the spelling, I speak fluently both your native languages. Do you even can try spell mine ?\" -- Failed Insult
    Is your whole family retarded, or did they just catch it from you?

  3. #3
    Banned
    Join Date
    May 2005
    Posts
    12
    Thank you for that, I decided to say screw the radio buttons and go with textfields. And i think I will use your layout here in the end.

    My last question, what all has to be done to convert this to an applet?

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class calculator extends JFrame implements ActionListener
    {
      //Declare three text fields
      private JTextField jtfgrade1, jtfgrade2, jtfgrade3, jtfgrade4, jtfResults;
      private JTextField jtfcredit1, jtfcredit2,jtfcredit3,jtfcredit4, jtftot;
      private JButton jbtCalc, jbtClear; //Declare "Add" button
    
      //Main Method
      public static void main(String[] args)
      {
        calculator frame = new calculator();
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
      }
    
      public calculator()
      {
        setTitle("Payment Calculator");
        //Use panel p1 to group text fields
        JPanel p1 = new JPanel();
        p1.setLayout(new GridLayout(5,1));
        p1.setBackground(Color.blue);
        p1.add(new JLabel(" Class 1 Grade"));
        p1.add(jtfgrade1 = new JTextField(1));
        jtfgrade1.setBackground(Color.red);
        p1.add(new JLabel(" Class 1 Credits"));
        p1.add(jtfcredit1 = new JTextField(1));
        p1.add(new JLabel(" Class 2 Grade"));
        p1.add(jtfgrade2 = new JTextField(1));
        p1.add(new JLabel(" Class 2 Credits"));
        p1.add(jtfcredit2 = new JTextField(1));
        p1.add(new JLabel("Class 3 Grade"));
        p1.add(jtfgrade3 = new JTextField(1));
        p1.add(new JLabel(" Class 3 Credits"));
        p1.add(jtfcredit3 = new JTextField(1));
        p1.add(new JLabel(" Class 4 Grade"));
        p1.add(jtfgrade4 = new JTextField(1));
        p1.add(new JLabel(" Class 4 Credits"));
        p1.add(jtfcredit4 = new JTextField(1));
        p1.add(new JLabel("Total GRADED Credits"));
        p1.add(jtftot = new JTextField(1));
        p1.add(new JLabel(" Your GPA is "));
        p1.add(jtfResults = new JTextField(1));
        jtfResults.setEditable(false);
    
        JPanel p3 = new JPanel();
        p3.setLayout(new FlowLayout(FlowLayout.CENTER));
        p3.setBackground(Color.blue);
        p3.add(new JLabel("When you enter the grade use these conversions:"));
        p3.add(new JLabel(" A = 4 - B = 3 - C = 2 - D = 1"));
    
        //Use panel p2 for the button
        JPanel p2 = new JPanel();
        p2.setLayout(new FlowLayout());
        p2.setBackground(Color.blue);
        p2.add(jbtCalc = new JButton("Calculate"));
        p2.add(jbtClear = new JButton("Clear"));
        jbtCalc.setBackground(Color.blue);
        jbtCalc.setForeground(Color.red);
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(p1, BorderLayout.CENTER);
        getContentPane().add(p2, BorderLayout.SOUTH);
        getContentPane().add(p3, BorderLayout.NORTH);
        //Register Listener
        jbtCalc.addActionListener(this);
        jbtClear.addActionListener(this);
      }
      public void actionPerformed(ActionEvent e)
      {
        if (e.getSource() == jbtCalc)
        {
          //Get int values from text fields and use trim() to trim extra space in field
          double grade1 = (Double.parseDouble(jtfgrade1.getText().trim()));
          double grade2 = (Double.parseDouble(jtfgrade2.getText().trim()));
          double grade3 = (Double.parseDouble(jtfgrade3.getText().trim()));
          double grade4 = (Double.parseDouble(jtfgrade4.getText().trim()));
          double credit1 = (Double.parseDouble(jtfcredit1.getText().trim()));
          double credit2 = (Double.parseDouble(jtfcredit2.getText().trim()));
          double credit3 = (Double.parseDouble(jtfcredit3.getText().trim()));
          double credit4 = (Double.parseDouble(jtfcredit4.getText().trim()));
          double total_credits = (Double.parseDouble(jtftot.getText().trim()));
          double result = ((grade1 * credit1)+(grade2 * credit2)+(grade3 * credit3)
              +(grade4 * credit4))/total_credits;
          //Set results in the TextField jtfResults
          jtfResults.setText(String.valueOf(result));
        }
        if (e.getSource() == jbtClear)
        {
          jtfResults.setText(" ");
          jtfgrade1.setText(" ");
          jtfgrade2.setText(" ");
          jtfgrade3.setText(" ");
        }
        }
      }

  4. #4
    AntiOnline n00b
    Join Date
    Feb 2004
    Posts
    666
    Hi,

    Nothign Much really Just Extend JApplet Instead of the JFrame ....... Applet Dosen't have that Void Main() Method get Rid of that ....... that's Probabelly all you will need .




    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    
    // <applet code=calc height=300 width=300></applet>
    
    public class calc extends JApplet implements ActionListener
    {
    
    
      //Declare three text fields
      private JTextField jtfgrade1, jtfgrade2, jtfgrade3, jtfgrade4, jtfResults;
      private JTextField jtfcredit1, jtfcredit2,jtfcredit3,jtfcredit4, jtftot;
      private JButton jbtCalc, jbtClear; //Declare "Add" button
    
    
    
       public void init()
      {
        
        //Use panel p1 to group text fields
        JPanel p1 = new JPanel();
        p1.setLayout(new GridLayout(5,1));
        p1.setBackground(Color.blue);
        p1.add(new JLabel(" Class 1 Grade"));
        p1.add(jtfgrade1 = new JTextField(1));
        jtfgrade1.setBackground(Color.red);
        p1.add(new JLabel(" Class 1 Credits"));
        p1.add(jtfcredit1 = new JTextField(1));
        p1.add(new JLabel(" Class 2 Grade"));
        p1.add(jtfgrade2 = new JTextField(1));
        p1.add(new JLabel(" Class 2 Credits"));
        p1.add(jtfcredit2 = new JTextField(1));
        p1.add(new JLabel("Class 3 Grade"));
        p1.add(jtfgrade3 = new JTextField(1));
        p1.add(new JLabel(" Class 3 Credits"));
        p1.add(jtfcredit3 = new JTextField(1));
        p1.add(new JLabel(" Class 4 Grade"));
        p1.add(jtfgrade4 = new JTextField(1));
        p1.add(new JLabel(" Class 4 Credits"));
        p1.add(jtfcredit4 = new JTextField(1));
        p1.add(new JLabel("Total GRADED Credits"));
        p1.add(jtftot = new JTextField(1));
        p1.add(new JLabel(" Your GPA is "));
        p1.add(jtfResults = new JTextField(1));
        jtfResults.setEditable(false);
    
        JPanel p3 = new JPanel();
        p3.setLayout(new FlowLayout(FlowLayout.CENTER));
        p3.setBackground(Color.blue);
        p3.add(new JLabel("When you enter the grade use these conversions:"));
        p3.add(new JLabel(" A = 4 - B = 3 - C = 2 - D = 1"));
    
        //Use panel p2 for the button
        JPanel p2 = new JPanel();
        p2.setLayout(new FlowLayout());
        p2.setBackground(Color.blue);
        p2.add(jbtCalc = new JButton("Calculate"));
        p2.add(jbtClear = new JButton("Clear"));
        jbtCalc.setBackground(Color.blue);
        jbtCalc.setForeground(Color.red);
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(p1, BorderLayout.CENTER);
        getContentPane().add(p2, BorderLayout.SOUTH);
        getContentPane().add(p3, BorderLayout.NORTH);
        //Register Listener
        jbtCalc.addActionListener(this);
        jbtClear.addActionListener(this);
      }
    
      public void actionPerformed(ActionEvent e)
      {
        if (e.getSource() == jbtCalc)
        {
          //Get int values from text fields and use trim() to trim extra space in field
          double grade1 = (Double.parseDouble(jtfgrade1.getText().trim()));
          double grade2 = (Double.parseDouble(jtfgrade2.getText().trim()));
          double grade3 = (Double.parseDouble(jtfgrade3.getText().trim()));
          double grade4 = (Double.parseDouble(jtfgrade4.getText().trim()));
          double credit1 = (Double.parseDouble(jtfcredit1.getText().trim()));
          double credit2 = (Double.parseDouble(jtfcredit2.getText().trim()));
          double credit3 = (Double.parseDouble(jtfcredit3.getText().trim()));
          double credit4 = (Double.parseDouble(jtfcredit4.getText().trim()));
          double total_credits = (Double.parseDouble(jtftot.getText().trim()));
          double result = ((grade1 * credit1)+(grade2 * credit2)+(grade3 * credit3)
              +(grade4 * credit4))/total_credits;
          //Set results in the TextField jtfResults
          jtfResults.setText(String.valueOf(result));
        }
        if (e.getSource() == jbtClear)
        {
          jtfResults.setText(" ");
          jtfgrade1.setText(" ");
          jtfgrade2.setText(" ");
          jtfgrade3.setText(" ");
        }
        }
      }



    Peace

  5. #5
    Banned
    Join Date
    May 2005
    Posts
    12
    Thank you
    Can you make the textfields have a default value of zero when it starts up? I got the .class file and when I go to put in on a website i use the following simple site.
    Code:
    <html>
    <head>
    <title>Calculator</title>
    </head>
    <body>
    <applet code=calculator height=300 width=300></applet>
    </body>
    </html>
    All i get is Applet Calculator noninited. This is my applet.

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class calculator extends JApplet implements ActionListener
    {
      //Declare three text fields
      private JTextField jtfgrade1, jtfgrade2, jtfgrade3, jtfgrade4, jtfResults;
      private JTextField jtfcredit1, jtfcredit2,jtfcredit3,jtfcredit4, jtftot;
      private JButton jbtCalc, jbtClear; //Declare "Add" button
    
      //Main Method
      //public static void main(String[] args)
      //{
        //calculator frame = new calculator();
        //frame.pack();
        //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //frame.setVisible(true);
      //}
    
      public void init()
      {
        //setTitle("Payment Calculator");
        //Use panel p1 to group text fields
        JPanel p1 = new JPanel();
        p1.setLayout(new GridLayout(5,1));
        p1.setBackground(Color.black);
        p1.add(new JLabel("<html><font color=white>Class 1 Grade</font></html>"));
        p1.add(jtfgrade1 = new JTextField(1));
        p1.add(new JLabel("<html><font color=white>Class 1 Credits</font></html>"));
        p1.add(jtfcredit1 = new JTextField(1));
        p1.add(new JLabel("<html><font color=white>Class 2 Grade</font></html>"));
        p1.add(jtfgrade2 = new JTextField(1));
        p1.add(new JLabel("<html><font color=white>Class 2 Credits</font></html>"));
        p1.add(jtfcredit2 = new JTextField(1));
        p1.add(new JLabel("<html><font color=white>Class 3 Grade</font></html>"));
        p1.add(jtfgrade3 = new JTextField(1));
        p1.add(new JLabel("<html><font color=white>Class 3 Credits</font></html>"));
        p1.add(jtfcredit3 = new JTextField(1));
        p1.add(new JLabel("<html><font color=white> Class 4 Grade</font></html>"));
        p1.add(jtfgrade4 = new JTextField(1));
        p1.add(new JLabel("<html><font color=white>Class 4 Credits</font></html>"));
        p1.add(jtfcredit4 = new JTextField(1));
        p1.add(new JLabel("<html><font color=white>Total <u>GRADED</u> Credits</font></html>"));
        p1.add(jtftot = new JTextField(1));
        p1.add(new JLabel("<html><font color=white>Your GPA is </font></html>"));
        p1.add(jtfResults = new JTextField(1));
        jtfResults.setEditable(false);
    
        JPanel p3 = new JPanel();
        p3.setLayout(new FlowLayout(FlowLayout.CENTER));
        p3.setBackground(Color.black);
        p3.add(new JLabel("<html><center><font color=rgb(195,138,45)>When entering grades use these conversions
     A = 4 - B = 3 - C = 2 - D = 1</font></center></html>"));
       // p3.add(new JLabel(" A = 4 - B = 3 - C = 2 - D = 1"));
    
        //Use panel p2 for the button
        JPanel p2 = new JPanel();
        p2.setLayout(new FlowLayout());
        p2.setBackground(Color.black);
        p2.add(jbtCalc = new JButton("<html><font color=white>Calculate</font></html>"));
        p2.add(jbtClear = new JButton("<html><font color=white>Clear</font></html>"));
        jbtCalc.setBackground(Color.black);
        jbtCalc.setForeground(Color.red);
        jbtClear.setBackground(Color.black);
        jbtClear.setForeground(Color.red);
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(p1, BorderLayout.CENTER);
        getContentPane().add(p2, BorderLayout.SOUTH);
        getContentPane().add(p3, BorderLayout.NORTH);
        //Register Listener
        jbtCalc.addActionListener(this);
        jbtClear.addActionListener(this);
      }
      public void actionPerformed(ActionEvent e)
      {
        if (e.getSource() == jbtCalc)
        {
          //Get int values from text fields and use trim() to trim extra space in field
          double grade1 = (Double.parseDouble(jtfgrade1.getText().trim()));
          double grade2 = (Double.parseDouble(jtfgrade2.getText().trim()));
          double grade3 = (Double.parseDouble(jtfgrade3.getText().trim()));
          double grade4 = (Double.parseDouble(jtfgrade4.getText().trim()));
          double credit1 = (Double.parseDouble(jtfcredit1.getText().trim()));
          double credit2 = (Double.parseDouble(jtfcredit2.getText().trim()));
          double credit3 = (Double.parseDouble(jtfcredit3.getText().trim()));
          double credit4 = (Double.parseDouble(jtfcredit4.getText().trim()));
          double total_credits = (Double.parseDouble(jtftot.getText().trim()));
          double result = ((grade1 * credit1)+(grade2 * credit2)+(grade3 * credit3)
              +(grade4 * credit4))/total_credits;
          //Set results in the TextField jtfResults
          jtfResults.setText(String.valueOf(result));
        }
        if (e.getSource() == jbtClear)
        {
          jtfResults.setText(" ");
          jtfgrade1.setText(" ");
          jtfgrade2.setText(" ");
          jtfgrade3.setText(" ");
        }
        }
      }

  6. #6
    AntiOnline n00b
    Join Date
    Feb 2004
    Posts
    666
    Hi,

    Can you make the textfields have a default value of zero when it starts up?
    Yes ... Just When you Inshlise the TextBox ............ new JTextField("1");

    And when I go to add the applet to a site, all I have saved is the .java file, and im using Jbuilder and can't find out how to compile to get a .class file. Only option I can find is 'Run Project'
    I Still Live in the Dark Ages ........ so i don't know how ......... I still use the Good Old NOtepad and the Dos Prompt Sorry Still haven't reached the JBuilder Ages

    Umm But Seriously You need to Pick some book and read a bit ..these are very basic Things you should know some of the Default Constructors of TextBox and Buttons .


    Peace

    [Edit]

    Oo and the Above Code you gave Works Fine for me

  7. #7
    Banned
    Join Date
    May 2005
    Posts
    47
    I disregarded most of the thread because I'm sure chsh pointed you in the right direction but .class files are created when your .java is compiled. So check in the same directory as where you have your .java file or if you have a new project, look in in the bin directory of that project or the compiled files... I don't know which JBuilder uses.

  8. #8
    Banned
    Join Date
    May 2005
    Posts
    12
    i found the .class file, im now trying to get it posted on a site, but for some reason it will not work

  9. #9
    Custom User
    Join Date
    Oct 2001
    Posts
    503
    Another layout to try after you get tired of GridLayout is BoxLayout. It's not quite as complicated as GridBagLayout, but you can get virtually the same results (usually by nesting a few).

    ac

  10. #10
    Banned
    Join Date
    May 2005
    Posts
    47
    Looking over your code... and compiling it and trying to run it gave me a boatload of errors so I thought I'd help you out a bit and do some coding at 3:30 AM. Whoo hoo for orange juice and Along Came Polly. The code is very simple and if you don't understand any part of it, do ask, I'll explain. Included below is a copy of the code and a screen shot of the working program. FAIR WARNING! I don't know if I used your calucation right..? Your program gave me errors when it came to the calculation but I think I scratched it together correctly.

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.applet.Applet;
    
    public class calculator extends JApplet implements ActionListener{
        
        private double result; 
        
        private JTextField[] gradesTF = new JTextField[4];
        private JTextField[] creditsTF = new JTextField[4];;
        
        private JButton calculateB = new JButton("Calculate");
        private JButton clearB = new JButton("Clear");
        
        private JLabel[] gradesL = new JLabel[4];
        private JLabel[] creditsL = new JLabel[4];
        private JLabel finalResultL = new JLabel("Result: ");
        
        public calculator() {
            JPanel containerPanel = new JPanel();
            JPanel inPanel = new JPanel();
            JPanel outPanel = new JPanel();
            
            inPanel.setLayout(new GridLayout(4, 4, 2, 2));
            for(int x = 0; x &lt; 4; x++) {
                gradesL[x] = new JLabel("Course " + (x + 1) + " Grade: ");
                inPanel.add(gradesL[x]);
                gradesTF[x] = new JTextField(3);
                inPanel.add(gradesTF[x]);
                creditsL[x] = new JLabel("Course " + (x + 1) + " Credits: ");
                inPanel.add(creditsL[x]);
                creditsTF[x] = new JTextField(3);
                inPanel.add(creditsTF[x]);
            }
            calculateB.addActionListener(this);
            clearB.addActionListener(this);
            
            outPanel.add(calculateB);
            outPanel.add(clearB);
            outPanel.add(finalResultL);
            
            getContentPane().add(containerPanel);
            containerPanel.add(inPanel);
            containerPanel.add(outPanel);
        }
        
        public void init() {
            calculator calc = new calculator();
        }
        
        public void actionPerformed(ActionEvent e) {
            double totalCredits = 0;
            if(e.getSource() == calculateB) {
                for(int x = 0; x &lt; 4; x++) {
                    result += (Double.parseDouble(gradesTF[x].getText()) * Double.parseDouble(creditsTF[x].getText()));
                    totalCredits += Double.parseDouble(creditsTF[x].getText());
                }
                result = result/totalCredits;
                finalResultL.setText("Result: " + result);
            }
            
            if(e.getSource() == clearB) {
                for(int x = 0; x &lt; 4; x++) {
                    gradesTF[x].setText("");
                    creditsTF[x].setText("");
                }
            }
        }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •