【问题标题】:Static to non static method静态到非静态方法
【发布时间】:2014-11-07 13:35:04
【问题描述】:

我正在尝试从静态 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

    }
}

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

【问题讨论】:

  • 你到底想达到什么目的?
  • 我正在尝试构建一个模拟,其中有两个对象一个猎人和一个被猎杀,它们都活着,猎人吃掉了猎物,并且都四处移动。我正在努力将它们绘制到屏幕上,作为然后移动的简单矩形。
  • 我的意思是在这个过渡中..
  • 看看我的代码并将其用作模型。享受

标签: java parameter-passing paint


【解决方案1】:

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

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

每个非静态方法都需要从对象中调用。静态方法属于类,所以你可以在没有对象的情况下调用它们。

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

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

【讨论】:

    【解决方案2】:

    你有一些混乱的代码!但让我把它清理一下,让它更漂亮。但是,现在由您决定让它工作。我为您的 jframe 创建了另一个类,并在构造函数中调整了您的额外/重复代码。

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

        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(),它都会调用函数paintComponent(Graphics g)。祝你好运:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-17
      • 1970-01-01
      • 1970-01-01
      • 2013-03-20
      • 2020-12-27
      相关资源
      最近更新 更多