We help IT Professionals succeed at work.

Java Question

drivenit
drivenit asked
on
Medium Priority
474 Views
Last Modified: 2013-11-23
Hey All

I'm so new to Java and feel somewhat ignorant when it comes to code. this should be easy.
i have the fallowing code and am just trying to duplicate the fields. I have created the text and the test fields and the action button. but i'm just trying to duplicate and it only shows the two top jLabel's and jtextfields so when i browse to the html file i only c two labels and boxes. what am i dong wrong??

here is the code


/*      This is an example of a Java Swing Applet
 *      The first three import statements, java.awt.*,
 *      java.awt.event.*, and javax.swing.* must be included
 *      in the creation of any applet.  The remaining four
 *      are used for some of the math, input/output, language
 *      and utility functions that are used.  Not all of them
 *      are used in this applet,
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.math.*;
import java.util.*;
import java.io.*;
import java.lang.*;

/*      Notice this is
 *      how the calculation gets started from the button
 */

public class Add5 extends JApplet implements ActionListener
      {
            // Variables declaration. For this applet, not all
            // declared labels and textfields are used
          private javax.swing.JPanel jPanel2;
            private javax.swing.JPanel jpanel3;
          private javax.swing.JLabel jLabel2;
          private javax.swing.JTextField jTextField1;
          private javax.swing.JLabel jLabel3;
          private javax.swing.JTextField jTextField2;            
          private javax.swing.JLabel jLabel4;
          private javax.swing.JTextField jTextField3;
          private javax.swing.JLabel jLabel5;
          private javax.swing.JTextField jTextField4;
          private javax.swing.JButton jButton1;
          private javax.swing.JButton jButton2;
          private javax.swing.JButton jButton3;
            private javax.swing.JTextArea resultArea;
          private Container container;
          // End of variables declaration
 
    /** Initialization method that will be called after the applet is loaded
     *  into the browser.  Change the setSize(x,y) values to resize the applet
       *  when using appletviewer
     */
    public void init () {
        initComponents();    
        setSize(800, 600);        
    }

    private void initComponents() {
       
        container = getContentPane();
            // The .setLayout creates the different layouts available.  Please consult
            // This applet uses BorderLayout()
        container.setLayout( new BorderLayout() );
         
            // Creating instances of each item  
            jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jTextField4 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();

        resultArea = new javax.swing.JTextArea();
            // End Creation
       
        // Set text on labels, preferred size can be optional on labels,
            // size should/must be used on text fields.
            // Then each individual item is added to a panel.
        jLabel2.setText("Loan Amount = ");
//      jLabel2.setPreferredSize(new java.awt.Dimension(75, 16));
        jPanel2.add(jLabel2);
       
        jTextField1.setText("");
        jTextField1.setPreferredSize(new java.awt.Dimension(65, 20));
        jPanel2.add(jTextField1);
       
        jLabel3.setText("APR amount =");
//            jLabel3.setPreferredSize(new java.awt.Dimension(75, 16));
        jPanel2.add(jLabel3);
       
        jTextField2.setText("");
        jTextField2.setPreferredSize(new java.awt.Dimension(65, 20));
        jPanel2.add(jTextField2);
       
        // Set text on labels, preferred size can be optional on labels,
            // size should/must be used on text fields.
            // Then each individual item is added to a panel.
        jLabel4.setText("Please Enter Loan Amount: ");
//      jLabel4.setPreferredSize(new java.awt.Dimension(95, 16));
        jPanel2.add(jLabel4);
       
        jTextField3.setText("");
        jTextField3.setPreferredSize(new java.awt.Dimension(65, 20));
        jPanel2.add(jTextField3);
       
        jLabel5.setText("Enter APR amount:");
//            jLabel5.setPreferredSize(new java.awt.Dimension(75, 16));
        jPanel2.add(jLabel5);
       
        jTextField4.setText("");
        jTextField4.setPreferredSize(new java.awt.Dimension(65, 20));
        jPanel2.add(jTextField4);
            // now the jPanel2 object is set in the borderlayout NORTH position.
            // you must use NORTH, SOUTH, EAST, WEST or CENTER.
            // In this case, only NORTH, SOUTH and CENTER are used.
        container.add( jPanel2, BorderLayout.NORTH);
        jButton1.setText("Click Here To whatever");
            jButton1.addActionListener(this);
        container.add(resultArea, BorderLayout.CENTER);
        container.add(jButton1, BorderLayout.SOUTH);
    }
   
   public void actionPerformed(ActionEvent e)
      {
      try
            {
                  // The text fields pass strings, so you must create integers out of the field values.
                  int a=Integer.parseInt(jTextField1.getText()), b=Integer.parseInt(jTextField2.getText()), temp;

                  resultArea.setText("");
                  
                  // This applet subtracts the smaller number from the larger number.
                  if (a>b)
                        temp = a+b;
                  else
                        temp = b+a;
                        resultArea.setText("" + temp);
            }
            catch (Exception ex)
            {
                  // If anything other than two integers are input, print out the error message
                  resultArea.setText("Input two integers please!");
            }      
      }
}
The code to call the Applet

<HTML>

<HEAD>

<TITLE>Unit 5 IP Add</TITLE>

</HEAD>

<BODY>

Here is the output of my program:
<APPLET CODE="Add5.class"WIDTH="400" HEIGHT="100">

</APPLET>

</BODY>

</HTML>

Any Help would b appreciated...
Comment
Watch Question

Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
The resize in HTML worked thanks a bunch don't know how i overlooked that..
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.