Java Forum / General / August 2007
increment letters
glennsnoise@hotmail.com - 31 Jul 2007 21:03 GMT Hi. I'm designing a Wheel-of-Fortune kind of layout using JButtons with ActionListeners to guess letters for words (kind of like Hangman, but using JButtons). I've set up a for loop that gives me 26 JButtons, but right now, all 26 buttons display "A". Can anyone tell me how to add an increment function to this so that each button will display a different letter of the alphabet?
Here is what I have so far:
import javax.swing.*; import java.awt.*;
public class GhostGUITwo extends JFrame { public static void main(String args[]) { new GhostGUITwo(); } //FRAME public GhostGUITwo() { this.setSize(800,600); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setTitle("Ghost"); this.setLocationRelativeTo(null); GhostGUITwoPanel panel = new GhostGUITwoPanel(); this.getContentPane().add(panel); this.setVisible(true); } //PANEL public class GhostGUITwoPanel extends JPanel { public GhostGUITwoPanel() {
for (int i = 0; i < 26; i++) this.add(new JButton("A")); } } //add JLabel and ActionListener //JButton ClickMe adds corresponding letter to JLabel }
Twisted - 31 Jul 2007 21:16 GMT On Jul 31, 4:03 pm, glennsno...@hotmail.com wrote:
> this.add(new JButton("A")); Try
add(new JButton(""+'A'+i));
This works in C; less sure about Java. There's also:
add(new JButton(LETTERS[i]));
with elsewhere in the class the field
private static final String[] LETTERS = {"A","B","C",...,"Z"};
with the ... filled in in the obvious manner.
The array method is more amenable to any subsequent i18n conversions.
Roedy Green - 31 Jul 2007 21:25 GMT >add(new JButton(""+'A'+i)); > >This works in C; less sure about Java. There's also: That won't work. The problem is + promotes 'A' to an int. You then effectively have ""+ 65 which will generate the string "65".
There are a number of related gotchas. See http://mindprod.com/jgloss/gotchas.html#CONCATENATION
 Signature Roedy Green Canadian Mind Products The Java Glossary http://mindprod.com
Twisted - 01 Aug 2007 00:58 GMT On Jul 31, 4:25 pm, Roedy Green <see_webs...@mindprod.com.invalid> wrote:
> >add(new JButton(""+'A'+i)); > > >This works in C; less sure about Java. There's also: > > That won't work. The problem is + promotes 'A' to an int. You then > effectively have ""+ 65 which will generate the string "65". Yeah, I figured it might need a cast in there somewhere.
Joshua Cranmer - 31 Jul 2007 21:47 GMT > On Jul 31, 4:03 pm, glennsno...@hotmail.com wrote: >> this.add(new JButton("A")); [quoted text clipped - 4 lines] > > This works in C; less sure about Java. There's also: The correct form is add(new JButton(""+(char)('A'+i)));
What you wrote would (I think) give you "A0","A1", etc.
Roedy Green - 31 Jul 2007 21:22 GMT > Can anyone tell me how to >add an increment function to this so that each button will display a >different letter of the alphabet? just use a for loop:
for ( char letter= 'A'; letter <= 'Z'; letter++ )
In Unicode you can count on the letters being in order and contiguous. see http://mindprod.com/jgloss/unicode.html
 Signature Roedy Green Canadian Mind Products The Java Glossary http://mindprod.com
Twisted - 01 Aug 2007 00:59 GMT On Jul 31, 4:22 pm, Roedy Green <see_webs...@mindprod.com.invalid> wrote:
> On Tue, 31 Jul 2007 13:03:56 -0700, glennsno...@hotmail.com wrote, > quoted or indirectly quoted someone who said : [quoted text clipped - 6 lines] > > for ( char letter= 'A'; letter <= 'Z'; letter++ ) Java lets you use something other than "int" as a loop counter? I thought it mostly just errored out at compile time if you tried that type of thing, and most code I've seen to generate character values numerically either adds a count to a char or looks up characters from an array.
JackT - 01 Aug 2007 01:13 GMT > > just use a for loop: > > for ( char letter= 'A'; letter <= 'Z'; letter++ ) > > Java lets you use something other than "int" as a loop counter? Indeed.
You can even use Objects. For example, before Java 5, the standard iterator pattern is this:
for(Iterator i = employeeList.iterator(); i.hasNext(); ) { Employee x = (Employee) i.next(); ... }
Twisted - 01 Aug 2007 02:47 GMT > You can even use Objects. For example, before Java 5, > the standard iterator pattern is this: [snip]
Yes, of course I'm familiar with that. I just seem to recall it balking on most primitive types other than "int", or at least throwing off a torrent of warnings. C compilers usually also warn, at least on use of char as a loop index. Which was exactly what was suggested here.
And I'm fairly sure Java does choke on just about anything but "int" for an array index, although that's moot if you don't use the loop counter as an array index or do cast it to int. It definitely doesn't permit longs as array indices.
JackT - 01 Aug 2007 02:57 GMT > Yes, of course I'm familiar with that. I just seem to recall it > balking on most primitive types other than "int", or at least throwing > off a torrent of warnings. You got that wrong.
> C compilers usually also warn, at least on > use of char as a loop index. You got that wrong also.
C code that deals with JPEG or GIF often uses "unsigned char" as array index to iterate through the 0..255 color range of each chroma, for example.
And portable C code should use "size_t" (rather than "int") to iterate through memory. size_t is defined by C90 as being able to hold the largest object that could be allocated, so sizeof(size_t)<sizeof(int) on some platforms, and sizeof(size_t)>=sizeof(int) on others.
> And I'm fairly sure Java does choke on just about anything but "int" > for an array index No one mentioned this. You like dragging discussions all over the place, often leading to +100 posts. Yeesh.
Twisted - 01 Aug 2007 03:06 GMT [snip an assortment of utter crap]
> No one mentioned this. You like dragging discussions all > over the place, often leading to +100 posts. Yeesh. You, meanwhile, apparently like violently attacking other posters without provocation, and making a formerly civil and technical discussion personal and adversarial.
Which is worse, hmm, prick?
Now go away and stop following up to my postings. If you absolutely must reply, confine the contents of your responses to purely technical matters and make no references to me or to any other persons save perhaps to yourself. If you are absolutely compelled, say at gunpoint, to respond in a personal and especially in a derogatory way USE E- MAIL!!! Thank you. :P
Getting sick of this. I guess it's still Pick-on-Twisted Month for another two hours. :P
JackT - 01 Aug 2007 03:09 GMT > You, meanwhile, apparently like violently attacking > other posters... You can never silence me, you moron.
I'll only stop correcting your mistakes when I've become bored with you, you pathetic bitch.
Twisted - 01 Aug 2007 03:17 GMT > > You, meanwhile, apparently like violently attacking > > other posters... [vicious insults deleted]
Go f.ck yourself, tard.
I mean that literally. Find some decent porn (if you don't have any, STFW, numbskull, and if you are too brain-dead to find any genuine and free smut on the web, there's always bittorrent) and a quiet place and masturbate.
Both of us will be much happier that way than if you continue to use your current method of keeping yourself amused and entertained.
I suggest also making your newsreader open up to alt.binaries.pictures.erotica by default to help reinforce a preference for masturbation over usenet flaming as your primary means of entertainment.
Do us all a favor now and go f.ck yourself.
HTH. HAND.
JackT - 01 Aug 2007 03:22 GMT > Go f.ck yourself... I guess that's all you've ever known... Ha!
I don't take orders from you. Whenever you make a factual mistakes, I'll be there to point out your folly. So to stop my attacks, all you have to do is simple:
(1) Stick to the facts.
(2) Limit your non-facts chatter to a minimum; for example, in your discussion of Java, don't run off and slam COBOL. Your irrelevant junk is just so stinky, I can smell it without even logging on to Google Groups.
You stink.
Anyway, it's noon now, I'm off to lunch. Smell you later, you pathetic useless moron.
Twisted - 01 Aug 2007 05:12 GMT [another steaming pile of off-topic, unconstructive, insulting bullshit]
I'm sorry, but this is comp.language.java.programmer, not your personal toilet. Please dump your offal someplace else. Thank you; HAND.
Patricia Shanahan - 01 Aug 2007 03:20 GMT >> You can even use Objects. For example, before Java 5, >> the standard iterator pattern is this: [quoted text clipped - 11 lines] > counter as an array index or do cast it to int. It definitely doesn't > permit longs as array indices. I agree about longs as array indices, because the array index has to be convertible to int by unary numeric promotion, but char is fine.
public class CharAsLoopIndex { public static void main(String[] args) { String[] array = new String[5]; for(char c = 0; c < array.length; c++){ array[c] = String.valueOf((char)(c+'A')); } } }
Remember Java does not have a concept of a loop counter as a language structure.
Patricia
Mike Schilling - 01 Aug 2007 05:37 GMT > And I'm fairly sure Java does choke on just about anything but "int" > for an array index, although that's moot if you don't use the loop > counter as an array index or do cast it to int. Anything that widens to int is a perfectly good index. That includes byte, char, and short.
> It definitely doesn't > permit longs as array indices. Right; long -> int is a narrowing conversion, so it requires a cast.
Jim Korman - 01 Aug 2007 01:30 GMT >> Can anyone tell me how to >>add an increment function to this so that each button will display a [quoted text clipped - 6 lines] >In Unicode you can count on the letters being in order and contiguous. >see http://mindprod.com/jgloss/unicode.html and then
add(new JButton(String.valueOf(letter));
Jim
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|