Results 1 to 3 of 3
  1. #1
    juju is offline Member
    Join Date
    Dec 2007
    Posts
    5
    Rep Power
    0

    Unhappy 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,

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java 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);
        }
    }

  3. #3
    juju is offline Member
    Join Date
    Dec 2007
    Posts
    5
    Rep Power
    0

Similar Threads

  1. Database to JApplet
    By Preethi in forum New To Java
    Replies: 0
    Last Post: 03-26-2008, 06:18 AM
  2. Arabic text in JApplet
    By Bedoor in forum Advanced Java
    Replies: 0
    Last Post: 01-15-2008, 01:52 PM
  3. help with converting to JApplet
    By Simmy in forum AWT / Swing
    Replies: 2
    Last Post: 08-09-2007, 09:45 AM
  4. JApplet and html
    By paty in forum Java Applets
    Replies: 1
    Last Post: 08-02-2007, 06:41 PM
  5. How to display an image in JApplet
    By fred in forum Java Applets
    Replies: 1
    Last Post: 07-24-2007, 03:02 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
  •