Java Question

Posted on 2011-04-24
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.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 () {
        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));
        jTextField1.setPreferredSize(new java.awt.Dimension(65, 20));
        jLabel3.setText("APR amount =");
//            jLabel3.setPreferredSize(new java.awt.Dimension(75, 16));
        jTextField2.setPreferredSize(new java.awt.Dimension(65, 20));
        // 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));
        jTextField3.setPreferredSize(new java.awt.Dimension(65, 20));
        jLabel5.setText("Enter APR amount:");
//            jLabel5.setPreferredSize(new java.awt.Dimension(75, 16));
        jTextField4.setPreferredSize(new java.awt.Dimension(65, 20));
            // 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");
        container.add(resultArea, BorderLayout.CENTER);
        container.add(jButton1, BorderLayout.SOUTH);
   public void actionPerformed(ActionEvent e)
                  // 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;

                  // This applet subtracts the smaller number from the larger number.
                  if (a>b)
                        temp = a+b;
                        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



<TITLE>Unit 5 IP Add</TITLE>



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




Any Help would b appreciated...
Question by:drivenit
    LVL 9

    Accepted Solution

    First, in your HTML code, I see there is no space between the CODE and WIDTH. Change this:
    <APPLET CODE="Add5.class"WIDTH="400" HEIGHT="100">
    <APPLET CODE="Add5.class" WIDTH="400" HEIGHT="100">

    Next, under the init() method, you're setting the size of the applet to 800 x 600. But, in the HTML, you're setting the size to 400 x 100.  You might try playing with that to see if it fixes the problem.

    I ran your code in Eclipse using its AppletViewer, and everything showed up fine, so I think your code is good otherwise.
    LVL 1

    Author Comment

    The resize in HTML worked thanks a bunch don't know how i overlooked that..

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Multiple issues with Java 14 37
    fix34  challenge 9 70
    fetching the full url inside controller 2 31
    Modeling a class in java 5 23
    INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
    Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
    Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
    Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now