简体   繁体   English

静态到非静态方法

[英]Static to non static method

I am trying to move from a static main() to a non static method called paintComponent(), but the problem I am having is I can not move from Static to Non static in the way which I have. 我正在尝试从静态main()转移到称为paintComponent()的非静态方法,但是我遇到的问题是我无法以自己的方式从“静态”转移到“非静态”。 The class is a follows, where hunter and hunted are external classes: 该类是以下类,其中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

    }
}

Bear in mind I am a beginner, so please try to keep it simple! 请记住,我是初学者,所以请尽量保持简单!

You need to call repaint and paintComponents using an object. 您需要使用一个对象来调用repaint和paintComponents。

JFrame frame = frame();
frame.repaint();
move();

Every non-static method needs to be called from an object. 每个非静态方法都需要从一个对象中调用。 Static methods belongs to the class, so you can call them without an object. 静态方法属于该类,因此您可以在没有对象的情况下调用它们。

repaint and paintComponents belongs to the JFrame object. repaint和paintComponents属于JFrame对象。 They are not static and so they need to be called using an object (repaint will call paintComponents). 它们不是静态的,因此需要使用一个对象来调用它们(重新绘制将调用paintComponents)。 Your 'frame()' method will return a JFrame object. 您的'frame()'方法将返回JFrame对象。 So you can call the repaint() method using the JFrame object that is returned from the 'frame()' method. 因此,您可以使用从'frame()'方法返回的JFrame对象调用repaint()方法。

Having said that, I'm not sure what you are trying to accomplish in your code, and even if my explanation will resolve a Compilation-Error, without further elaboration, it might not accomplish what you are trying to achieve. 话虽如此,但我不确定您要在代码中尝试完成的工作,即使我的解释能够解决Compilation-Error(编译错误),而无需进一步详细说明,它也可能无法完成您要实现的目标。

you have got some messed up code! 您有一些混乱的代码! but let me clean it up a bit, make it more presentable. 但让我整理一下,使其更美观。 However it is up to you to get it to work now. 但是,这取决于您现在使其开始工作。 I have created another class for your jframe and adjusted your extra / repetitive code in the constractor. 我为您的jframe创建了另一个类,并在承包商中调整了您的额外/重复代码。

btw i know nothing about haunted and haunter. 顺便说一句,我对鬼屋和鬼屋一无所知。

    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.");

    }

}

now if you include graphics in this class, everytime you call up repaint(), it will call the function paintComponent(Graphics g). 现在,如果您在此类中包含图形,则每次调用repaint()时,它将调用函数paintComponent(Graphics g)。 good luck :) 祝好运 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM