![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Oct 2005
Posts: 8
Rep Power: 0
![]() |
noob gui help..
I have a class that implements actionlistener, and I add an actionlistener to a button button.addActionListener(this); it says "cannot use this in a static context"
?? |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
it means that you can only use this in an object of the class and not the class itself.
|
|
|
|
|
|
#3 |
|
Encoder
|
TeamElement1, can you please share the code?
I tried the following code:-
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Test
{
public static void main(String[] args)
{
MyFrame frame = new MyFrame();
frame.show();
}
}
class MyFrame extends JFrame implements ActionListener
{
public MyFrame()
{
JButton button = new JButton("Ok");
JPanel panel = new JPanel();
panel.add(button);
button.addActionListener(this);
getContentPane().add(panel);
pack();
}
public void actionPerformed(ActionEvent e)
{
System.out.println("button pressed!");
}
}its similar to what you say, this code has a button.addActionListener(this) statement. but it compiles and runs successfully! |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Oct 2005
Posts: 8
Rep Power: 0
![]() |
I fixed it, I guess static was where it shouldn't be. I was just being careless
![]() |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|