Results 1 to 5 of 5
- 08-01-2011, 11:12 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 53
- Rep Power
- 0
Cannot make a static reference to the non-static method
So I have this Japplet which represents a bunch of Jpanels. I first say to the Japplet that it has to display the first Jpanel. In this JPanel is a button which has an actionlistener connected to it. When you click on it the method ChangePanel is called from the container (the Japplet) which changes the Jpanel that is displayed (from present to future)
Here's the code:
So I try to run this code and it says at point A and B:Java Code:import java.awt.Image; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.JApplet; import javax.swing.JPanel; public class menu extends JApplet { /** * */ private static final long serialVersionUID = 1L; private JPanel[] GamePanels = {new FirstPanel(), new SecondPanel()}; Image BackgroundImage; public static void ChangePanel(int present,int future) { remove(GamePanels[present]); [COLOR="red"]Point A[/COLOR] add(GamePanels[future]); [COLOR="red"]Point B[/COLOR] } public void init() { this.setSize(200,300); this.add(GamePanels[0]); GamePanels[0].setVisible(true); } }
And it gives a fast solution:Cannot make a static reference to the non-static field GamePanelsI do this (Which I shouldn't because I actually want the Jpanel to be a instance method and not a static one)Change modifier GamePanels to static
and in return get the following error messages:
What am I doing wrong? Or how do I get this to work?Exception in thread "AWT-EventQueue-1" java.lang.Error: Unresolved compilation problems:
Cannot make a static reference to the non-static method remove(Component) from the type JApplet
Cannot make a static reference to the non-static method add(Component) from the type Container
PS: I doubt there's anything wrong with my panels
- 08-01-2011, 11:34 PM #2
And why is ChangePanel, which should be changePanel* static?
db
* Code Conventions for the Java(TM) Programming Language: Contents
- 08-01-2011, 11:43 PM #3
Member
- Join Date
- Jul 2011
- Posts
- 53
- Rep Power
- 0
Tru that...
It's because I changed the method to static as a fast solution to the following problem:
(This is my first panel called FirstPanel.java)
It says at point A the following:Java Code:import java.awt.Color; import java.awt.Graphics; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.JButton; import javax.swing.JPanel; public class FirstPanel extends JPanel implements ActionListener { /** * */ private static final long serialVersionUID = 1L; protected JButton b1,b2; Image Background; Color color; public FirstPanel() { try { Background = ImageIO.read(new File("ButtonEscape.png")); } catch (IOException ex) { } color= Color.black; b1 = new JButton("Change My Color!!"); b1.setMnemonic(KeyEvent.VK_D); b1.setActionCommand("ChangeColor"); b2 = new JButton("DisplayNextScreen"); b2.setMnemonic(KeyEvent.VK_E); b2.setActionCommand("Next"); b1.addActionListener(this); b2.addActionListener(this); add(b1); add(b2); } @Override public void paintComponent(Graphics g) { g.setColor(color); g.fillRect(0, 0, getWidth(), getHeight()); g.drawImage(Background, getWidth()/2-Background.getWidth(this)/2, 2*getHeight()/3, null); } @Override public void actionPerformed(ActionEvent e) { if ("ChangeColor".equals(e.getActionCommand())) { if (color==Color.black){ color=Color.white; } else { color=Color.black; } repaint(); } else { menu.changePanel(0,1); [COLOR="red"]Point A[/COLOR] } } }
And the fast solution is to make changePanel() a static method which I did (and thus it went static)Cannot make a static reference to the non-static method changePanel(int, int) from the type menu
But now I have the above error haunting with no idea how to solve it...
-
You're fixing things backwards and it's coming back to bite you. The solution is not to make any methods static, but rather to call methods on objects, not on classes. Suggestions include
- Fix your Java naming (as Darryl mentions) so you don't confuse others who try to help you or grade you. Class names should begin with a capital letter and methods and variables with a lower case letter. You should do this now.
- Make changePanel a non-static method.
- Pass an instance of the applet (this within the applet itself) into your FirstPanel and SecondPanel constructors and have these classes store that instance in a Menu variable. This way these classes can call non-static methods on the Main object.
- Use a CardLayout to do the JPanel swapping for you. The tutorials will show you how to do this.
Last edited by Fubarable; 08-02-2011 at 12:47 AM.
- 08-02-2011, 12:42 AM #5
Member
- Join Date
- Jul 2011
- Posts
- 53
- Rep Power
- 0
Similar Threads
-
Cannot make a static reference...
By Guy in forum New To JavaReplies: 12Last Post: 07-28-2011, 07:22 AM -
Garbage Collection - Self reference, static and non-static.
By garyiskidding in forum Advanced JavaReplies: 10Last Post: 03-17-2011, 11:22 AM -
Cannot make a static reference to the non-static method
By Xycose in forum New To JavaReplies: 10Last Post: 11-14-2010, 07:06 AM -
Can't make static reference to non-static method -> huh?! Simple car prgm
By enerj in forum New To JavaReplies: 7Last Post: 09-24-2010, 05:09 AM -
make static ref to non-static method?
By McChill in forum New To JavaReplies: 7Last Post: 02-23-2009, 05:48 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
I will read about the Cardlayout tomorrow its rather late now though

Bookmarks