Next Generation Emulation banner

1 - 20 of 23 Posts

·
ゲート オーペン!
Joined
·
911 Posts
Discussion Starter · #1 ·
well I'm learning java windowed apps, so well I was just practicing by making a calendar (an assigment the book told me to lol XD) and well I dont get any errors in the out but the window wont show. (and yes I have set 'setVisible(true);') here's the code that caused the problem
this part is pretty much just to add the days(I left some comments about each line or so):

Code:
        //days 1 - 28
        JLabel days[] = new JLabel[28];
        int t= 0; // /**
        int i = 1;// * varaiables to be used
        int c = 0;// *
        int p = 1;// */
        do {        
          i++;//normal loop	
          days[i] = new JLabel(""+i);//this statement makes the 28 days, since i = 1, it goes from 1-29
          do {       
            c++;  //moves the each day by one cell
            if(i<=7){ //but if 7 days passed 
              c = 0; //it goes back to 0, and starts counting again       
               do{ 
                 p++;  //goes one up everytime 7 days passed  
                 if(p>=5){
                 	if(c>=6){
                 		//stop all loops
                        break;
                        
                 	}
                 }  
                   buildConstraints(constraints,p,c,1,1,100,100);
                 }while(c <= 0); //everytime 7 days passed 
                }
              }while(c<7);//just checking that c never gets to 7 since the max is 6 cells      
              constraints.fill = GridBagConstraints.NONE;
              constraints.anchor = GridBagConstraints.CENTER;
              grid.setConstraints(days[i], constraints);//adds one day
              pane.add(days[i]);   //adds the days
              
             }while(i<=28); //this is going to occur until we reach day 28
the rest of the code doesn't matter since it was working b4 I added the days code (yes the window was showing).
 

·
NextGenerationGaymulation
Joined
·
2,775 Posts
hmmm

Doesn't it get stuck in the while loop for C? I can never see it going below zero, I only checked quickly though. ( break only stops one loop )

ee, never mind it should... :D but hmm those whiles are confusing, you don't need the while ( C < 7 ) do you? and something is seriusly wrong with the format... :D a good structure is a must while programming.
 

·
ゲート オーペン!
Joined
·
911 Posts
Discussion Starter · #3 ·
ChankastRules said:
Doesn't it get stuck in the while loop for C? I can never see it going below zero, I only checked quickly though. ( break only stops one loop )

ee, never mind it should... :D but hmm those whiles are confusing, you don't need the while ( C < 7 ) do you? and something is seriusly wrong with the format... :D a good structure is a must while programming.
yes I need the (c <7) since its a grid 0,1,2,3,4,5,6 x spots right if I get to 7 then I'll mess up the layout, the format is pretty good I think :p
 

·
NextGenerationGaymulation
Joined
·
2,775 Posts
well, to each his own :D I'm ussually good at helping with programming but all those while's and the indenting is giving me a headache :D what is wrong with for anyhow?
 

·
ゲート オーペン!
Joined
·
911 Posts
Discussion Starter · #5 ·
ChankastRules said:
well, to each his own :D I'm ussually good at helping with programming but all those while's and the indenting is giving me a headache :D what is wrong with for anyhow?
well I had it with for loops in the begging xD, but it didn't compile for some reason oO so I change it with do..while(long!!!!!!!!!!!!!!!!!!!!!!!!!!! process lol j/k just annoying :p)
 

·
Premium Member
Joined
·
6,338 Posts
Why don't you just make a 7 by x (where x is the number of days / 7) GridLayout, and add the days in one by one (using a for, while, whatever you want)?
 

·
ゲート オーペン!
Joined
·
911 Posts
Discussion Starter · #7 ·
well because I made it that way already I was trying to do with GridBagLayout just for practice
 

·
Premium Member
Joined
·
6,338 Posts
With GridBagLayout all you need to do is set the grid width to GridBagConstraints.REMAINDER every seven lines. The logic shouldn't change that much from GridLayout.
 

·
ゲート オーペン!
Joined
·
911 Posts
Discussion Starter · #9 · (Edited)
well I just solved the problem I reduced the code by 30% lol

Code:
       //days 1 - 28
        JLabel days[] = new JLabel[32];
        int i = 0;
        int p = -1;//well I'm not sure why but I had to make it -1 in order to make this work
        int c = 2;
        do{//just reaching the 31 days
        i++;
        p++;//moving it sideways
        if(p == 7){//when it gets to the end
        	p = -1;//back to the beggining
        	c++;//and it goes down one row 
        	continue;//and it goes back to counting
        }
        System.out.println("i: "+i+" p: "+p+" c: "+c);
        days[i] = new JLabel(""+i);
        buildConstraints(constraints,p,c,1,1,100,100);
        constraints.fill = GridBagConstraints.NONE;
        constraints.anchor = GridBagConstraints.CENTER;
        grid.setConstraints(days[i], constraints);
        pane.add(days[i]);
        }while(i != 31);
 

·
Premium Member
Joined
·
8,201 Posts
shadow89 said:
//well I'm not sure why but I had to make it -1 in order to make this work
thats normal...you increment it before you create the label
you could start at 0 and after creating the label THEN increment it. (thats how it shoudl be IMO, makes it more understandable)

you would then have to move the "if" after the incrmentation which is after the creation of the label. seems you have everything reversed :p
 

·
ゲート オーペン!
Joined
·
911 Posts
Discussion Starter · #11 ·
Chrono Archangel said:
thats normal...you increment it before you create the label
you could start at 0 and after creating the label THEN increment it. (thats how it shoudl be IMO, makes it more understandable)

you would then have to move the "if" after the incrmentation which is after the creation of the label. seems you have everything reversed :p
OH lol well it was just a trial-and-error when I got there lol, but hey it worked but thnx for the explanation :p
 

·
NextGenerationGaymulation
Joined
·
2,775 Posts
oo no my eyes!

do{
____ i++
____ .
____ .
} while( i != 31 )

trust me you'll like yourself better with proper formating of code.
 

·
ゲート オーペン!
Joined
·
911 Posts
Discussion Starter · #13 ·
ohhhhhhh for the love of god my format is good u shouldn;t make a big deal out of that xD
 

·
Premium Member
Joined
·
3,093 Posts
No, your format doesn't allow to take an idea of the blocks of code in a single view. You actually have to llok for brackets to know where control sentences lay. The formatting suggested by CR would be hundreds of times better. I personally go for
Code:
...
do
{
     some stuff;
     some other;
     last one;
}
while (some condition);
...
for(;;)
{
     work forever;
}
...
But anyway, the point is to be able to have an idea of the logical structure visually. Your code formatting doesn't allow that.
 

·
ゲート オーペン!
Joined
·
911 Posts
Discussion Starter · #15 ·
ehh well ok is there a program that does the same thing as flash like it organizes it? with the actionscript ????? I´m using Jcreator to write my java, but do u recommend anything that would do it should I just do it by myself xD
 

·
Premium Member
Joined
·
8,201 Posts
shadow89 said:
ehh well ok is there a program that does the same thing as flash like it organizes it? with the actionscript ????? I´m using Jcreator to write my java, but do u recommend anything that would do it should I just do it by myself xD
eclipse is much better then JCreator IMO and its also free.
it does automatic indentations i beleive to some extent...(been a while since ive opened it)
 

·
Premium Member
Joined
·
6,338 Posts
Writing organized code just takes some practice and getting used to. I generally try not to rely on specialized editors as I find it hinders my coding more than helps it.

As for coding tips, I would generally avoid using do-while loops (use while and for loops instead). When you know the range of data you should always use a for loop. The program you're making for example can be done with a simple for loop.
 

·
ゲート オーペン!
Joined
·
911 Posts
Discussion Starter · #18 · (Edited)
hmm I see thnx for the tips eheh is hard to get used to it since is practicly my first language in programming xD, btw could u specify me which eclipse is because I'm not sure xD
 

·
Premium Member
Joined
·
6,338 Posts
Well, first-time programming languages are always hectic. I remember programming in Turing when I was in high school. I'd be pretty embarrassed to revisit the code I wrote back then:emb:. It's a learning process though and you can only get better with time.
 
1 - 20 of 23 Posts
Top