当前位置: 代码迷 >> java >> 我调用的哪个方法是静态的?
  详细解决方案

我调用的哪个方法是静态的?

热度:47   发布时间:2023-07-17 20:57:39.0

所以继承人我的代码:

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 行“Project.add(label1);”上遇到错误。 它说它不能对 add 方法进行静态引用,因为 add 方法不是静态的。 除了 Project 之外,我还调用了哪些其他方法? 我尝试将 Project 设为静态,但它不是有效的修饰符。 任何人都可以帮忙吗?

public Project(){更改Project.add(label1); this.add(label1);

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

  相关解决方案