Learn how to a build a cloud-first strategyRegister Now


Java Question

Posted on 2011-04-24
Medium Priority
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 () {
        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

Accepted Solution

lisfolks earned 2000 total points
ID: 35456681
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.

Author Comment

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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month21 days, 7 hours left to enroll

810 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