Results 1 to 3 of 3
  1. #1
    rcj66 is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Default Converting java application to applet

    hello everyone,

    i wrote a simple currency conversion program for one of my classes and now i just wanted to see if i could make it into an applet but i'm having some trouble...basically all it does when i try to run it in firefox is give me a blank black box..here's the html tag that i'm using

    Java Code:
    <APPLET code="Currency.class" WIDTH="200" HEIGHT="40"> </APPLET>

    here's my actual program...keep in mind that this was an application and this is the first time i attempt to make it into an applet...if i'm way wrong please don't laugh lol...i can post the original program if anyone wants me to, thanks...

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.JApplet;
    import javax.swing.SwingUtilities;
    import javax.swing.JLabel;
    
    
    
    public class Currency extends JApplet
    {
    
    		
    	JPanel panel;
    
    
    	JLabel dollarsL, outputNameL, outputL, blankL, blank2L;
    	JTextField dollarsTF;
    	JButton euroB, poundB, yenB;
    
    	EuroButtonHandler ebHandler;
    	PoundButtonHandler pbHandler;
    	YenButtonHandler ybHandler;
    
    
    	public void init()
    	{
    
    
    		Currency c= new Currency();
    		c.init();
    
    		JPanel Jpanel= new JPanel();
    		
    		panel.setSize(500, 100);
    
    
    		panel.setLayout(new GridLayout(3,3));
    
    		dollarsL= new JLabel("Dollar Amount: ", SwingConstants.CENTER);
    		outputNameL= new JLabel("Conversion: ", SwingConstants.CENTER);
    		outputL= new JLabel("");
    		blankL = new JLabel();
    		blank2L= new JLabel();
    
    		dollarsTF= new JTextField(10);
    
    		euroB= new JButton("Euro");
    		poundB= new JButton("British Pounds");
    		yenB= new JButton("Japanese Yen");
    
    
    		panel.add(dollarsL);
    		panel.add(dollarsTF);
    		panel.add(blankL);
    		panel.add(outputNameL);
    		panel.add(outputL);
    		panel.add(blank2L);
    		panel.add(euroB);
    		panel.add(poundB);
    		panel.add(yenB);
    
    		panel.setVisible(true);
    
    
    		ebHandler = new EuroButtonHandler();
    		pbHandler= new PoundButtonHandler();
    		ybHandler= new YenButtonHandler();
    
    		euroB.addActionListener(ebHandler);
    		poundB.addActionListener(pbHandler);
    		yenB.addActionListener(ybHandler);
    
    	}
    
    	//converts dollars to euros
    	public class EuroButtonHandler implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			int input = Integer.parseInt(dollarsTF.getText());
    			double euro = input * 0.733945;
    
    			outputL.setText("" + euro);
    
    		}
    	}
    
    	//converts dollars to pounds
    	public class PoundButtonHandler implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			int input = Integer.parseInt(dollarsTF.getText());
    			double pound= input * 0.637227;
    
    			outputL.setText("" + pound);
    		}
    	}
    
    	//converts dollars to yen
    	public class YenButtonHandler implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			int input= Integer.parseInt(dollarsTF.getText());
    			double yen= input * 89.9200;
    
    			outputL.setText("" + yen);
    		}
    	}
    
    
    }//end class Currency

  2. #2
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    366
    Rep Power
    6

    Default

    Hello and welcome to the forum.

    First you can find Applet tutorial here on this forum:

    http://www.java-forums.org/java-tuto...ogramming.html

    Before you test your code in browser
    make sure it works in your IDE (Netbrans, Eclipse...)

    If things go wrong you will find error in Console.

    Your code has:

    Java Code:
    java.lang.StackOverflowError
    error.

    TIP:
    So next time you have problem and you ask for help
    be sure you post copy of this error here so people have a place to start
    when they analyze your problem.

    Considering code:

    You are using JApplet not Applet.
    There are differences between them.

    With JApplet:
    " components go in the "content pane", not directly in the frame.
    Changing other properties (layout manager, background color, etc.) also apply to the content pane.
    Access content pane via getContentPane, or if you want to replace it with your container..."

    Am posting you code back with SOME errors fixed
    but it's not over, i'n leaving it to you to learn more about Applets and
    fix it all the way



    import java.awt.Color;
    import java.awt.Container;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import javax.swing.JApplet;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.SwingConstants;

    Java Code:
    public class Currency extends JApplet
    {
    
    
    	JPanel panel;
    
    
    	JLabel dollarsL, outputNameL, outputL, blankL, blank2L;
    	JTextField dollarsTF;
    	JButton euroB, poundB, yenB;
    
    	EuroButtonHandler ebHandler;
    	PoundButtonHandler pbHandler;
    	YenButtonHandler ybHandler;
    
    
    	public void init()
    	{
    
    		//0. this you have to use for Japplet
    		Container content = getContentPane();
    		content.setBackground(Color.white);
    
    		//1. this was stackoverflow error about
    		//Currency c= new Currency();
    		//c.init();
    
    
    		//2. remove this coz you have your "panel"
    		//JPanel Jpanel= new JPanel();
    
    		panel= new JPanel();
    
    		panel.setSize(500, 100);
    
    
    		panel.setLayout(new GridLayout(3,3));
    
    		dollarsL= new JLabel("Dollar Amount: ", SwingConstants.CENTER);
    		outputNameL= new JLabel("Conversion: ", SwingConstants.CENTER);
    		outputL= new JLabel("");
    		blankL = new JLabel();
    		blank2L= new JLabel();
    
    		dollarsTF= new JTextField(10);
    
    		euroB= new JButton("Euro");
    		poundB= new JButton("British Pounds");
    		yenB= new JButton("Japanese Yen");
    
    
    		panel.add(dollarsL);
    		panel.add(dollarsTF);
    		panel.add(blankL);
    		panel.add(outputNameL);
    		panel.add(outputL);
    		panel.add(blank2L);
    		panel.add(euroB);
    		panel.add(poundB);
    		panel.add(yenB);
    
    		panel.setVisible(true);
    
    
    		ebHandler = new EuroButtonHandler();
    		pbHandler= new PoundButtonHandler();
    		ybHandler= new YenButtonHandler();
    
    		euroB.addActionListener(ebHandler);
    		poundB.addActionListener(pbHandler);
    		yenB.addActionListener(ybHandler);
    
    		//3. this is important!
    		content.add(panel);
    
    	}
    
    	//converts dollars to euros
    	public class EuroButtonHandler implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			int input = Integer.parseInt(dollarsTF.getText());
    			double euro = input * 0.733945;
    
    			outputL.setText("" + euro);
    
    		}
    	}
    
    	//converts dollars to pounds
    	public class PoundButtonHandler implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			int input = Integer.parseInt(dollarsTF.getText());
    			double pound= input * 0.637227;
    
    			outputL.setText("" + pound);
    		}
    	}
    
    	//converts dollars to yen
    	public class YenButtonHandler implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			int input= Integer.parseInt(dollarsTF.getText());
    			double yen= input * 89.9200;
    
    			outputL.setText("" + yen);
    		}
    	}
    
    
    }//end class Currency

    So go back to beginning, use some basic examples
    test them and move on to your code. Once it works in IDE
    test it in browser.

    Good luck!

  3. #3
    rcj66 is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Default

    Quote Originally Posted by FON View Post
    Hello and welcome to the forum.

    First you can find Applet tutorial here on this forum:

    http://www.java-forums.org/java-tuto...ogramming.html

    Before you test your code in browser
    make sure it works in your IDE (Netbrans, Eclipse...)

    If things go wrong you will find error in Console.

    Your code has:

    Java Code:
    java.lang.StackOverflowError
    error.

    TIP:
    So next time you have problem and you ask for help
    be sure you post copy of this error here so people have a place to start
    when they analyze your problem.

    Considering code:

    You are using JApplet not Applet.
    There are differences between them.

    With JApplet:
    " components go in the "content pane", not directly in the frame.
    Changing other properties (layout manager, background color, etc.) also apply to the content pane.
    Access content pane via getContentPane, or if you want to replace it with your container..."

    Am posting you code back with SOME errors fixed
    but it's not over, i'n leaving it to you to learn more about Applets and
    fix it all the way



    import java.awt.Color;
    import java.awt.Container;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import javax.swing.JApplet;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.SwingConstants;

    Java Code:
    public class Currency extends JApplet
    {
    
    
    	JPanel panel;
    
    
    	JLabel dollarsL, outputNameL, outputL, blankL, blank2L;
    	JTextField dollarsTF;
    	JButton euroB, poundB, yenB;
    
    	EuroButtonHandler ebHandler;
    	PoundButtonHandler pbHandler;
    	YenButtonHandler ybHandler;
    
    
    	public void init()
    	{
    
    		//0. this you have to use for Japplet
    		Container content = getContentPane();
    		content.setBackground(Color.white);
    
    		//1. this was stackoverflow error about
    		//Currency c= new Currency();
    		//c.init();
    
    
    		//2. remove this coz you have your "panel"
    		//JPanel Jpanel= new JPanel();
    
    		panel= new JPanel();
    
    		panel.setSize(500, 100);
    
    
    		panel.setLayout(new GridLayout(3,3));
    
    		dollarsL= new JLabel("Dollar Amount: ", SwingConstants.CENTER);
    		outputNameL= new JLabel("Conversion: ", SwingConstants.CENTER);
    		outputL= new JLabel("");
    		blankL = new JLabel();
    		blank2L= new JLabel();
    
    		dollarsTF= new JTextField(10);
    
    		euroB= new JButton("Euro");
    		poundB= new JButton("British Pounds");
    		yenB= new JButton("Japanese Yen");
    
    
    		panel.add(dollarsL);
    		panel.add(dollarsTF);
    		panel.add(blankL);
    		panel.add(outputNameL);
    		panel.add(outputL);
    		panel.add(blank2L);
    		panel.add(euroB);
    		panel.add(poundB);
    		panel.add(yenB);
    
    		panel.setVisible(true);
    
    
    		ebHandler = new EuroButtonHandler();
    		pbHandler= new PoundButtonHandler();
    		ybHandler= new YenButtonHandler();
    
    		euroB.addActionListener(ebHandler);
    		poundB.addActionListener(pbHandler);
    		yenB.addActionListener(ybHandler);
    
    		//3. this is important!
    		content.add(panel);
    
    	}
    
    	//converts dollars to euros
    	public class EuroButtonHandler implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			int input = Integer.parseInt(dollarsTF.getText());
    			double euro = input * 0.733945;
    
    			outputL.setText("" + euro);
    
    		}
    	}
    
    	//converts dollars to pounds
    	public class PoundButtonHandler implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			int input = Integer.parseInt(dollarsTF.getText());
    			double pound= input * 0.637227;
    
    			outputL.setText("" + pound);
    		}
    	}
    
    	//converts dollars to yen
    	public class YenButtonHandler implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			int input= Integer.parseInt(dollarsTF.getText());
    			double yen= input * 89.9200;
    
    			outputL.setText("" + yen);
    		}
    	}
    
    
    }//end class Currency

    So go back to beginning, use some basic examples
    test them and move on to your code. Once it works in IDE
    test it in browser.

    Good luck!
    awesome! thank you very much...i learned a lot from your post and i am going to read up on JApplets vs. Applets now...It's strange though because i wasn't getting an error when i was compiling it. I use TextPad because it's simple and it was compiling flawlessly but there was obviously something wrong because it wouldn't work when i ran it...anyway, thanks for the help!

Similar Threads

  1. Replies: 3
    Last Post: 12-04-2009, 01:10 PM
  2. Converting java application to J2ME
    By coldvoice05 in forum NetBeans
    Replies: 0
    Last Post: 10-10-2009, 05:32 PM
  3. Converting simple Java Application to Applet
    By jrohde in forum Java Applets
    Replies: 1
    Last Post: 07-25-2009, 08:48 PM
  4. Replies: 1
    Last Post: 03-27-2009, 06:47 PM
  5. Converting an App to Applet
    By josephdcoleman in forum New To Java
    Replies: 1
    Last Post: 02-21-2009, 08:07 AM

Posting Permissions

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