Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-28-2007, 07:08 AM
Member
 
Join Date: Dec 2007
Posts: 5
juju is on a distinguished road
Another problem in JApplet :S
Hi all,

I created a JFrame and I want to convert it to JApplet, I tried many times
and it didn't work with me, can someone provide me with the way, or any beneficial web sites.

Here's the question, I'm tryin to solve:

Write a Java Applet to find the sum of the digits of a number ( 243 = 2 + 4 + 3 = 9 )
and reverse order of the digits of a number ( 243 = 342 ) .
Write your program using Java events. You will get number from KB
and when you will press enter key, your applet has to display the sum of the digits
and reverse order of the digits in separate fields

And this is my solution:

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;

public class SumDigitFrame extends JFrame
{
private JTextField TextField1;
private JLabel label1;
private JTextField TextFieldSum, TextFieldReverse;

public SumDigitFrame(){
super("Sum Digits of Integer");
setLayout(new FlowLayout());

TextField1=new JTextField(6);
label1=new JLabel("Enter an Integer number:");
label1.setVerticalAlignment(SwingConstants.TOP);
add(label1);
add(TextField1);

TextFieldSum=new JTextField("",6);
TextFieldReverse=new JTextField("",7);
add(new JLabel("Sum of Digits:"));
add(TextFieldSum);
add(new JLabel("Reverse of Intege Entered:"));
add(TextFieldReverse);
TextHandler handler=new TextHandler();
TextField1.addActionListener(handler);
}//end of constructor

private class TextHandler implements ActionListener
{
public void actionPerformed(ActionEvent event){
int sum=0,result=1;
String reverse="";
int value=Integer.parseInt(event.getActionCommand());
while(result!=0){
result=value%10;
sum+=result;
reverse+=String.format("%d",result);
result=value/10;
value=result;
}//end of while
TextFieldSum.setText(String.format("%d",sum));
TextFieldReverse.setText(reverse);
}//end of actionPerformed
}//end of TextHandler

public static void main(String args[]){
SumDigitFrame sumDigitFrame=new SumDigitFrame();
sumDigitFrame.setDefaultCloseOperation(JFrame.EXIT _ON_CLOSE);
sumDigitFrame.setSize(300,120);
sumDigitFrame.setVisible(true);
}
}//end of class SumDigitFrame



thanks,
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-29-2007, 04:57 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,029
hardwired is on a distinguished road
Code:
// <applet code="SumDigitApplet" width="300" height="120"></applet> import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JLabel; import javax.swing.JApplet; import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.JOptionPane; import javax.swing.SwingConstants; public class SumDigitApplet extends JApplet { private JTextField TextField1; private JLabel label1; private JTextField TextFieldSum, TextFieldReverse; public void init() { setLayout(new FlowLayout()); TextField1=new JTextField(6); label1=new JLabel("Enter an Integer number:"); label1.setVerticalAlignment(SwingConstants.TOP); add(label1); add(TextField1); TextFieldSum=new JTextField("",6); TextFieldReverse=new JTextField("",7); add(new JLabel("Sum of Digits:")); add(TextFieldSum); add(new JLabel("Reverse of Intege Entered:")); add(TextFieldReverse); TextHandler handler=new TextHandler(); TextField1.addActionListener(handler); } private class TextHandler implements ActionListener { public void actionPerformed(ActionEvent event) { int sum=0,result=1; String reverse=""; int value=Integer.parseInt(event.getActionCommand()); while(result!=0){ result=value%10; sum+=result; reverse+=String.format("%d",result); result=value/10; value=result; } TextFieldSum.setText(String.format("%d",sum)); TextFieldReverse.setText(reverse); } } public static void main(String args[]){ JApplet applet = new SumDigitApplet(); JFrame f = new JFrame("Sum Digits of Integer"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(applet); f.setSize(300,120); f.setLocation(300,200); applet.init(); f.setVisible(true); } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-30-2007, 08:46 PM
Member
 
Join Date: Dec 2007
Posts: 5
juju is on a distinguished road
Thank you million times hardwired for helping me...
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Database to JApplet Preethi New To Java 0 03-26-2008 06:18 AM
Arabic text in JApplet Bedoor Advanced Java 0 01-15-2008 01:52 PM
help with converting to JApplet Simmy AWT / Swing 2 08-09-2007 09:45 AM
JApplet and html paty Java Applets 1 08-02-2007 06:41 PM
How to display an image in JApplet fred Java Applets 1 07-24-2007 03:02 AM


All times are GMT +3. The time now is 05:54 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org