Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Applet: Calculated variable can't be displayed

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Applet: Calculated variable can't be displayed

    Hello guys,
    i have started learning java lately, and a tutorial said creating a dice program would be a good idea. After i have done this i kinda wanted to "advance" a little. So I have no syntax errors at all, but the program just doesnt spit out anything. I would greatly appreciate help. (Some variables are in german, but i dont think that will stop anyone from helping me! :) )
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
     
    /**
     * Klasse randomnumberco
     * @author (iTTerror)
     * @version (1)
     */
    public class randomnumberco extends Applet implements ActionListener
    {
        Button knopf = new Button("Würfeln!");
        int x = 0;
        int[] c = new int[6];
        Label ausgabe = new Label();
        Label zahl1 = new Label();
        Label zahl2 = new Label();
        Label zahl3 = new Label();
        Label zahl4 = new Label();
        Label zahl5 = new Label();
        Label zahl6 = new Label();
        public void init()
        {
               add(knopf);
               knopf.addActionListener(this);
               add(ausgabe);
               add(zahl1);
               add(zahl2);
               add(zahl3);
               add(zahl4);
               add(zahl5);
               add(zahl6);
               c[0] = 0;
               c[1] = 0;
               c[2] = 0;
               c[3] = 0;
               c[4] = 0;
               c[5] = 0;
         }
           public void actionPerformed(ActionEvent e)
           {
               x = (int) (Math.random()*7);      
               while(x==1)
               {
                   c[0]=c[0]++;
                }
                while(x==2)
               {
                   c[1]=c[1]++;
                }
                while(x==3)
               {
                   c[2]=c[2]++; 
                }
                while(x==4)
               {
                   c[3]=c[3]++;
                }
                while(x==5)
               {
                   c[4]=c[4]++;
                }
                while(x==6)
                {
                   c[5]=c[5]++;
                }
                ausgabe.setText("" + x);
                zahl1.setText("Die Zahl 1 wurde"+c[0]+"mal gewürfelt");
                zahl2.setText("Die Zahl 2 wurde"+c[1]+"mal gewürfelt");
                zahl3.setText("Die Zahl 3 wurde"+c[2]+"mal gewürfelt");
                zahl4.setText("Die Zahl 4 wurde"+c[3]+"mal gewürfelt");
                zahl5.setText("Die Zahl 5 wurde"+c[4]+"mal gewürfelt");
                zahl6.setText("Die Zahl 6 wurde"+c[5]+"mal gewürfelt");
            }   
    }
    Last edited by iTTerror; May 12th, 2012 at 01:18 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Applet: Calculated variable can't be displayed

    Where are you expecting to see anything?
    Add some printlns to the code to show where the execution flow goes and what the values of variables are as they are used and set.

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    iTTerror (May 12th, 2012)

  4. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Applet: Calculated variable can't be displayed

    There is nothing immediately noticablely wrong. I mean, as long as that button is pressed, I don't see why the setText() lines wouldn't run. The only thing I'm not sure of is whether or not JLabels revalidate themselves. If not, that would be your problem. Someone else might know for sure how JLabels react to getting their text changed.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  5. #4
    Junior Member
    Join Date
    May 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Applet: Calculated variable can't be displayed

    Quote Originally Posted by aussiemcgr View Post
    There is nothing immediately noticablely wrong. I mean, as long as that button is pressed, I don't see why the setText() lines wouldn't run. The only thing I'm not sure of is whether or not JLabels revalidate themselves. If not, that would be your problem. Someone else might know for sure how JLabels react to getting their text changed.
    The button appears, but when i click it, nothing happens. :/
    Quote Originally Posted by Norm View Post
    Where are you expecting to see anything?
    I was expecting to show the calculated dice value in the Label ausgabe.
    In the labels zahl1 through 6, should display how many times each number has been "rolled".
    Quote Originally Posted by Norm View Post
    Add some printlns to the code to show where the execution flow goes and what the values of variables are as they are used and set.
    I haven't used this in java it self yet, just in java script. I will try that out, thank you!

Similar Threads

  1. Won't display calculated fahrenheit and celsius when running.
    By smithmar in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 17th, 2012, 09:00 PM
  2. Replies: 5
    Last Post: November 16th, 2011, 11:22 AM
  3. [SOLVED] Refreshing Variables Displayed on a JPanel
    By StandbyExplosion in forum What's Wrong With My Code?
    Replies: 6
    Last Post: August 24th, 2011, 06:14 PM
  4. Getting a red Oval to displayed on screen
    By warnexus in forum What's Wrong With My Code?
    Replies: 8
    Last Post: January 13th, 2011, 08:27 PM
  5. Certain Chinese Characters not displayed properly.
    By kerwintang in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: August 20th, 2009, 08:23 AM