![]() |
|
#1
|
|||
|
|||
addActionListener(this) conflicts with staticI have started to recall Java and encountered the following problem. Help, pls.
A simple window with a button. So far as the program has to react to the button click, I describe the class as public class Class1 implements ActionListener {... The program should run on a local computer, i.e. it is not an applet. Thus the execution must (IMHO) begin from public static void main(String[] args) {... To react on the button click, I must (IMHO) write button1.addActionListener(this); However, it gives an error. The message is "Cannot use this in a static context". I guess that this points to the instance of an object but not to the class, while there is no class (because of static). Cannot get how to solve this problem. Thank you! |
|
#2
|
||||
|
||||
Re: addActionListener(this) conflicts with statica button needs an ActionListener to perform action for the button. Check out the code here, see: private class ButtonListener, for example code.
http://www.gidforums.com/showpost.ph...93&postcount=1 Without seeing more of what you are trying to do, this is all I can suggest. There is a way to use 'this', but I'd have to see what you have done. What it SOUNDS like, is that you are trying to use addActionListener() WITHIN main(), which is static in context. I don't know why so many Java users try to do it that way, for it's not using the OO capability of Java. 1. Create your button and modify its properties in a constructor. 2. main() should theoretically only have one statement that creates an instance of the class, something like this: (this is skeleton code, with your class name as an example) JAVA Code:
__________________
Use the force...read the source!! WYCIWYG -- what you code is what you get! Last edited by TurboPT : 02-Aug-2006 at 11:43.
|
|
#3
|
|||
|
|||
Re: addActionListener(this) conflicts with staticThank you
I actually tried a similar idea before asking here but I located public static void main(String[] args) {... in a separate object and thus in a separate file. So, I got "Fatal exception" without any explanation and concluded that the idea is wrong |
Recent GIDBlog
Python ebook by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| calling static member function through object | alcoholic | C++ Forum | 3 | 06-Dec-2005 06:10 |
| Bloodshed Dev C++ Project Options | JdS | C++ Forum | 6 | 11-Nov-2005 17:23 |
| error related to static member functions and varaibles | kai85 | C++ Forum | 3 | 20-May-2005 06:11 |
| Convert Java to C++??? | leanieleanz | C++ Forum | 1 | 04-Mar-2005 19:06 |
| changing caption of static text control | Pandiani | MS Visual C++ / MFC Forum | 1 | 20-Sep-2004 01:38 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The