Java: cannot access static variable from a differe

2019-07-19 08:10发布

This is quite odd, because I have a Character class that can access Frame.dimension.getWidth(); and its partner getHeight(), however when I wanted to use this in the Map class eclipse underlines it and cannot give me feedback. Running the program anyways ends up in java errors stating they can't find the X value of a Map object.

Here's the Frame class:

package Core;

import java.awt.Dimension;
import java.awt.Rectangle;
import javax.swing.JFrame;


public class Frame
{
static int width = 800, height = 600;
static Dimension dimension;

public static void main(String[]args){
    JFrame frame= new JFrame("RETRO");
    frame.add(new Screen());

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(width,height);
    frame.setVisible(true);
    frame.setResizable(false);
    dimension = frame.getContentPane().getSize();
}
}

And the Map class:

package Core;

import java.awt.Frame;
import java.awt.Image;
import java.awt.Rectangle;
import javax.swing.ImageIcon;
import java.awt.Dimension;

public class Map
{
    double x, y;

Image map;
ImageIcon background = new ImageIcon("images/Maps/maze.jpg");

public Map(){
    map = background.getImage();
}

public void move(double moveX, double moveY){
        x += moveX;
        y += moveY;
}

//////////Gets
public Image getMap(){
    return map;
}
public double getX(){
    if(x<0)
        x=0;
    else if(x>Frame.dimension.getWidth())
        x=Frame.dimension.getWidth();

    return x;
}

public double getY(){
    if(y<0)
        y=0;
    else if(y>Frame.dimension.getHeight())
        y=Frame.dimension.getHeight();

    return y;
}

public int getHeight(){
    return map.getHeight(null);
}

public int getWidth(){
    return map.getWidth(null);
}
}

I could provide the Character class but it is very long and messy at the moment.. however the word Frame was only used in the calling of Frame.dimension.getWidth()/getHeight();

4条回答
Root(大扎)
2楼-- · 2019-07-19 08:21

Your Map class is referring to java.awt.Frame (which you imported), not Core.Frame.

If you really need to keep the import java.awt.Frame, just use the fully qualified name of your frame class when referring to it (Core.Frame) to avoid the collision, e.g. in your getX method:

public double getX(){
    if(x<0)
        x=0;
    else if(x>Core.Frame.dimension.getWidth())
        x=Core.Frame.dimension.getWidth();

    return x; 
}

Or, if you don't really need to use java.awt.Frame at all, just remove that import line.

查看更多
姐就是有狂的资本
3楼-- · 2019-07-19 08:35

You may have a name clash with your own Core.Frame and Java's own java.awt.Frame.

I would re-name your Frame class to something different from a core-Java name, and I'd avoid using static variables as well.

查看更多
神经病院院长
4楼-- · 2019-07-19 08:38

the Frame class you refer to in x=Frame.dimension.getWidth(); is java.awt.Frame. note that you imported this class. Try mentioning explicitly: Core.Frame instead or remove the line import java.awt.Frame; if you don't use this class.

查看更多
贪生不怕死
5楼-- · 2019-07-19 08:38

Seems like there's a collision between import between java.awt.Frame and Core.Frame

You should add "import static Core.Frame.dimension" to your imports, and then just work with "dimension" instead of "Frame.dimension"

查看更多
登录 后发表回答