Java 静态到非静态方法

Java 静态到非静态方法,java,parameter-passing,paint,Java,Parameter Passing,Paint,我试图从一个静态的main方法转移到一个称为paintComponent的非静态方法,但是我遇到的问题是我不能以我现有的方式从静态方法转移到非静态方法。该类为以下类型,其中hunter和hunted为外部类: import javax.swing.JFrame; import java.awt.Graphics; public class Main extends JFrame{ //Public class: Available for all other classes to

我试图从一个静态的main方法转移到一个称为paintComponent的非静态方法,但是我遇到的问题是我不能以我现有的方式从静态方法转移到非静态方法。该类为以下类型,其中hunter和hunted为外部类:

import javax.swing.JFrame;
import java.awt.Graphics;

public class Main extends JFrame{       //Public class: Available for all other classes to refer to

    private static final long serialVersionUID = -4511248732627763442L;

    public static void main(String[] args){     

        frame();
        repaint();
        move();         //Passes to the method move() in the class Main()

    }

    public static JFrame frame(){
        JFrame frame = new JFrame("Hunter VS Hunted");          //Sets the window title
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);          //Sets the size of the window
        frame.setVisible(true);                                 //Says to display the window
        frame.setResizable(false);                              //Sets it so the screen cannot be adjusted
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   //Closes the window when the x is pushed        

        System.out.println("Frame works.");  
        return frame;
    }

    public void paintComponent(Graphics g){

        super.paintComponents(g);   //Assigns a graphics value to g, so that it can be passed to other methods                          
        Hunted.paint(g);
        Hunter.paint(g);

        System.out.println("Main.paintComponent works.");

    }

    public static void move(){

        Hunter.move();          //Passes to move() in the Hunter class
        Hunted.move();          //Passes to move() in the Hunter class

    }
}

记住我是初学者,所以请尽量保持简单

您需要使用对象调用repaint和paintComponents

JFrame frame = frame();
frame.repaint();
move();
每个非静态方法都需要从对象调用。静态方法属于类,因此可以在不使用对象的情况下调用它们

JFrame frame = frame();
frame.repaint();
move();
重新绘制和绘制组件属于JFrame对象。它们不是静态的,因此需要使用对象重绘调用它们,并将调用paintComponents。 “frame”方法将返回一个JFrame对象。因此,您可以使用从“frame”方法返回的JFrame对象调用repaint方法


话虽如此,我不确定您试图在代码中实现什么,即使我的解释将解决编译错误,但如果不进一步详细说明,它可能无法实现您试图实现的目标。

您的代码有些混乱!但让我把它清理一下,让它更好看。不过,现在就由你来决定如何让它发挥作用。我已经为您的jframe创建了另一个类,并在constractor中调整了额外/重复的代码

顺便说一句,我对闹鬼和闹鬼一无所知

    import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Main {

public static void main(String[] args){     
    myFrame x = new myFrame("Hunter VS Hunted");
    x.ui(x.getContentPane());

}


}

class myFrame extends JFrame {

    public myFrame(String name){
        // constractor
        super(name); //Sets the window title
        setExtendedState(JFrame.MAXIMIZED_BOTH);          //Sets the size of the window
        setVisible(true);                                 //Says to display the window
        setResizable(false);                              //Sets it so the screen cannot be adjusted
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   //Closes the window when the x is pushed        
    }

    public void ui(final Container pane){
        JLabel test = new JLabel("test frame");
        pane.add(test);
        System.out.println("Frame works.");

    }

}

现在,如果在这个类中包含图形,那么每次调用repaint时,它都会调用函数paintComponentGraphics g。祝你好运:

你到底想实现什么?我正在尝试建立一个模拟,其中有两个物体,一个是猎人,一个是被猎杀的,它们都活着,而猎人吃掉了被猎杀的,并且都在移动。我正在努力的是把这些画到屏幕上,作为简单的矩形,然后移动。我的意思是在这个转换中…看看我的代码,并将其用作模型。享受