I'm now learning Java AWT from scratch by myself. Now I just encounter a problem which I don't know why. I just wanna create two JButtons marked with "1" and "2", and a JTextField; what's more, I wish if I click Jbutton"1", then there'll be a "1" showed in this JTextfield and "2" for the similar action, at least in theory. Unfortunately, when running my program after compiling with JDK 1.6, I get only a "2" showed in my JTextField no matter which JButton I click. So, with my code showing below, could you help me, please? Thanks in advance.
We are all in the gutter, but some of us are looking at the stars.
You may be learning AWT from scratch, but JButton and JTextField are Swing components, extensions of their AWT predecessors, Button and TextField. You're using other Swing components in your program. Swing components usually start with the letter 'J'. Be careful not to mix Swing components with AWT components in the same program. Results can be unpredictable and returning the program's performance to predictable can be painful. There are many good Swing tutorials on the web, in the Oracle Java tutorials, and probably here at the Ranch, to aid in your quest to learn Swing, and there's always excellent support here at the Ranch.
Learning Java using Eclipse on OpenSUSE 11.2
Linux user#: 501795
Thank you for your reply. I've only one showtext object which can be shared by two JButtons objects here. What's more, there are two CTextAction objects created and attached to each JButtton, namely Button1 and Button2; I wish that when I click the Button1, it'll answered and invoke CTextAction's Constructor to set the object showtext to "text" I give when I create CtextAction object, that's to say, "1".
P.S.: If I uncomment line 48 and recompile my code, there'll be "1" and "2" showed in terminal as soon as I run the program. I'm not sure why.
Well, I've found my "bug" in the code. Now I put the showtext into the class CtextAction, which fixes my problem:
From this I've learned that the Constructor will only be invoked once, the time an object created. So, when I click the Button1, only the method actionPerformed( ) will be invoked, not the Constructor CtextAction( ); I should put the showtext into class CtextAction to make it become an inner Attribute whose value differs in different objects, namely Button1 and Button2 , hence. Thank you for your help very much.