我称之为静态的哪种方法?

时间:2015-07-30 21:46:56

标签: java static non-static

所以继承我的代码:

package project;

import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import project.Project.MyGraphics;

public class Project extends JFrame implements Runnable{
String thing = "none";
public static int x;
public static int y;
int w = 1600;
int h = 1000;
public int ww = 5;
public int hh = 5;
public class AL extends KeyAdapter{
    public void keyPressed(KeyEvent e) {
        int KeyCode = e.getKeyCode();
        if (KeyCode == e.VK_NUMPAD1){
            downleft();
            if (thing == "none" || thing == "d" || thing == "dr" || thing == "l" || thing == "r" ||
                    thing == "ul" || thing == "u" || thing == "ur") {
                thing = "dl";
                System.out.println("Drawing down to the left!");
            }
            if (x > 1595) {
                x = 1595;
            }
            if (y > 974) {
                y = 974;
            }
            if (x < 0) {
                x = 0;
            }
            if (y < 0) {
                y = 0;
            }

        }
        if (KeyCode == e.VK_NUMPAD2){
            down();
            if (thing == "none" || thing == "dl" || thing == "dr" || thing == "l" || thing == "r" ||
                    thing == "ul" || thing == "u" || thing == "ur") {
                thing = "d";
            System.out.println("Drawing down!");
        }
            if (x > 1595) {
                x = 1595;
            }
            if (y > 974) {
                y = 974;
            }
            if (x < 0) {
                x = 0;
            }
            if (y < 0) {
                y = 0;
            }
        }
        if (KeyCode == e.VK_NUMPAD3){
            downright();
            if (thing == "none" || thing == "dl" || thing == "d" || thing == "l" || thing == "r" ||
                    thing == "ul" || thing == "u" || thing == "ur") {
                thing = "dr";
            System.out.println("Drawing down to the right!");
            }
            if (x > 1595) {
                x = 1595;
            }
            if (y > 974) {
                y = 974;
            }
            if (x < 0) {
                x = 0;
            }
            if (y < 0) {
                y = 0;
            }
        }
        if (KeyCode == e.VK_NUMPAD4){
            left();
            if (thing == "none" || thing == "dl" || thing == "d" || thing == "dr" || thing == "r" ||
                    thing == "ul" || thing == "u" || thing == "ur") {
                thing = "l";
            System.out.println("Drawing to the left");
            }
            if (x > 1595) {
                x = 1595;
            }
            if (y > 974) {
                y = 974;
            }
            if (x < 0) {
                x = 0;
            }
            if (y < 0) {
                y = 0;
            }
        }
        if (KeyCode == e.VK_NUMPAD6){
            right();
            if (thing == "none" || thing == "dl" || thing == "d" || thing == "dr" || thing == "l" ||
                    thing == "ul" || thing == "u" || thing == "ur") {
                thing = "r";
            System.out.println("Drawing to the right!");
            }
            if (x > 1595) {
                x = 1595;
            }
            if (y > 974) {
                y = 974;
            }
            if (x < 0) {
                x = 0;
            }
            if (y < 0) {
                y = 0;
            }
        }
        if (KeyCode == e.VK_NUMPAD7){
            upleft();
            if (thing == "none" || thing == "dl" || thing == "d" || thing == "dr" || thing == "l" ||
                    thing == "r" || thing == "u" || thing == "ur") {
                thing = "ul";
            System.out.println("Drawing up to the left!");
            }
            if (x > 1595) {
                x = 1595;
            }
            if (y > 974) {
                y = 974;
            }
            if (x < 0) {
                x = 0;
            }
            if (y < 0) {
                y = 0;
            }
        }
        if (KeyCode == e.VK_NUMPAD8){
            up();
            if (thing == "none" || thing == "dl" || thing == "d" || thing == "dr" || thing == "l" ||
                    thing == "r" || thing == "ul" || thing == "ur") {
                thing = "u";
            System.out.println("Drawing up!");
            }
            if (x > 1595) {
                x = 1595;
            }
            if (y > 974) {
                y = 974;
            }
            if (x < 0) {
                x = 0;
            }
            if (y < 0) {
                y = 0;
            }
        }
        if (KeyCode == e.VK_NUMPAD9){
            upright();
            if (thing == "none" || thing == "dl" || thing == "d" || thing == "dr" || thing == "l" ||
                    thing == "r" || thing == "ul" || thing == "u") {
                thing = "ur";
            System.out.println("Drawing up to the right!");
            }
            if (x > 1595) {
                x = 1595;
            }
            if (y > 974) {
                y = 974;
            }
            if (x < 0) {
                x = 0;
            }
            if (y < 0) {
                y = 0;
            }
        }
        if (KeyCode == e.VK_B){
            ww += 5;
            hh += 5;
            System.out.println("Resized! It is now " + ww + " by " + hh + "!");
            if (ww > 150) {
                ww = 150;
                hh = 150;
                System.out.println("The size went too high! Resized back to 150 by 150!");
            }
        }
        if (KeyCode == e.VK_S){
            ww -= 5;
            hh -= 5;
            System.out.println("Resized! It is now " + ww + " by " + hh + "!");
            if (ww < 5) {
                ww = 5;
                hh = 5;
                System.out.println("The size went too low! Resized back to 5 by 5!");
            }
        }
    }
}
public void downleft(){
    x-=5;
    y+=5;
}
public void down(){
    y+=5;
}
public void downright(){
    x+=5;
    y+=5;
}
public void left(){
    x-=5;
}
public void right(){
    x+=5;
}
public void upleft(){
    x-=5;
    y-=5;
}
public void up(){
    y-=5;
}
public void upright(){
    x+=5;
    y-=5;
}
public Project(){
    addKeyListener(new AL());
    setTitle("Etch a Sketch");
    setResizable(false);
    setVisible(true);
    setSize(w, h);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    getContentPane().add(new MyGraphics());

    JLabel label1 = new JLabel("Size: " + x + " by " + y, JLabel.CENTER);
    label1.setAlignmentX(0);
    label1.setAlignmentY(0);
    Project.add(label1);

    x = 100;
    y = 100;
}
public static void main(String[] args) {
    new Project();
}
class MyGraphics extends JPanel {
    public void paintComponent(Graphics g) {
        g.fillRect(x, y, ww, hh);
        repaint();
    }
}
@Override
public void run() {

}
}

我在第247行&#34; Project.add(label1);&#34;上收到错误。它说它不能对add方法进行静态引用,因为add方法不是静态的。除了Project之外我还有哪种方法。我尝试使Project静态,但它不是一个有效的修饰符。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:3)

2147483647中将public Project(){更改为Project.add(label1);

这是因为this.add(label1);中的add(..)不是静态方法,如果你需要在这个类之外调用它,你将使用一个实例变量,或者如果你想在同一个类中使用它似乎现在使用关键字class Project