在类classname-Java中从未调用public classname()

在类classname-Java中从未调用public classname(),java,audio,jframe,mouselistener,Java,Audio,Jframe,Mouselistener,下面的方法 公共JSound(){ } 从未被调用此代码? 有什么原因吗 如果我不能让它以这种方式工作,有没有第二种方法可以使用鼠标侦听器?它需要一个非静态的方法,所以我不知道如何实现这一点 我添加了 new JSound(); 及 但两者都不起作用 package Sound; import java.awt.Component; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; impor

下面的方法

公共JSound(){

}

从未被调用此代码? 有什么原因吗

如果我不能让它以这种方式工作,有没有第二种方法可以使用鼠标侦听器?它需要一个非静态的方法,所以我不知道如何实现这一点

我添加了

 new JSound();

但两者都不起作用

 package Sound;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;

public class JSound extends JFrame implements MouseListener {

//IDK what its for but its important for jframe
private static final long serialVersionUID = 3314065768834871224L;

//Gets General volume
public static double volume = Audio.getMasterOutputVolume()*3*100;

//Universal variables are static, and i usually put them here
public static boolean running = true;
public static String title = "Advanced Java Sound";
public static int width = 410;
public static int height = 600;

public static int ticks = 1;

//class setups
public JSound sound;

public JSound() {
    //This never gets called for some reason

    //initialises mouse input
    System.out.println("apples");
    setTitle("Simple Frame");
    addMouseListener(this);
}

public static void main(String args[]) {
    //Creates the display basicly just the empty window you will see all the stuff drawn to
    Display.Window();
    //Calls the main loop method
    mainloop();
    //SoundLoad();
    //addMouseListener(sound);
}

public static void mainloop() {
    render.quickrender();

    try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); }

    while(running) {
        long lastTime = System.nanoTime();
        double amountOfTicks = 60.0;
        double ns = 1000000000 / amountOfTicks;
        double delta = 0;
        long timer = System.currentTimeMillis();
        int frames = 0;
        while (running) {
            long now = System.nanoTime();
            delta += (now - lastTime) / ns;
            lastTime = now;
            while (delta >= 1) {
                ticks++;
                delta--;
            }
            if (running) 
                tick();
                render.renderer();
            frames++;

            if(System.currentTimeMillis() - timer > 1000) {
                timer += 1000;
                System.out.println("FPS: " + frames + " Ticks: " + ticks);
                frames = 0;
            }
        }
    }
}

public static void tick() {
    //Put any tick method stuff here, it will be executed in priority to render, and will occur more then 60 times per second

    //Audio.setMasterOutputVolume(0.5f);
    volume = Audio.getMasterOutputVolume()*4.8*100;
    //System.out.println(Audio.getMasterOutputVolume());
    //System.out.println((int)volume);
}

public void mouseClicked(MouseEvent e) {
    System.out.println(e.getX());
    System.out.println(e.getY());
}

public void mouseEntered(MouseEvent e) {

}

public void mouseExited(MouseEvent e) {

}

@Override
public void mousePressed(MouseEvent e) {
    System.out.println(e.getX());
    System.out.println(e.getY());
}

public void mouseReleased(MouseEvent e) {

}
}

JSound
从未为此代码调用过?有什么原因吗

首先,它不是一种方法。它是一个构造函数

从未调用它的原因是代码中没有表达式
newjsound()
。为什么?我不知道。问问编写代码的人

JSound
从未为此代码调用过?有什么原因吗

首先,它不是一种方法。它是一个构造函数


从未调用它的原因是代码中没有表达式
newjsound()
。为什么?我不知道。问问编写代码的人

它从未被调用,因为您从未调用JSound构造函数。
我想您应该在主循环之前的main方法中这样做

它从未被调用,因为您从未调用JSound构造函数。
import javax.swing.JFrame;

class JSound extends JFrame {
    //Universal variables are static, and i usually put them here
    public static boolean running = true;
    public static String title = "Advanced Java Sound";
    public static int width = 410;
    public static int height = 600;

    public static int ticks = 1;

    //class setups
    public JSound sound;

    public JSound() {
        //This never gets called for some reason

        //initialises mouse input
        System.out.println("apples");
        setTitle("Simple Frame");
    }

    public static void main(String args[]) {
        System.out.println("I'm before main loop");
        new JSound();
        mainloop();
        System.out.println("I'm after main loop");
        new JSound();
    }

    public static void mainloop() {

        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        while (running) {
            long lastTime = System.nanoTime();
            double amountOfTicks = 60.0;
            double ns = 1000000000 / amountOfTicks;
            double delta = 0;
            long timer = System.currentTimeMillis();
            int frames = 0;
            while (running) {
                long now = System.nanoTime();
                delta += (now - lastTime) / ns;
                lastTime = now;
                while (delta >= 1) {
                    ticks++;
                    delta--;
                }
                if (running) {
                    tick();
                }
                frames++;

                if (System.currentTimeMillis() - timer > 1000) {
                    timer += 1000;
                    System.out.println("FPS: " + frames + " Ticks: " + ticks);
                    frames = 0;
                }
            }
        }
    }

    public static void tick() {

    }
}
我想您应该在主循环之前的main方法中这样做

import javax.swing.JFrame;

class JSound extends JFrame {
    //Universal variables are static, and i usually put them here
    public static boolean running = true;
    public static String title = "Advanced Java Sound";
    public static int width = 410;
    public static int height = 600;

    public static int ticks = 1;

    //class setups
    public JSound sound;

    public JSound() {
        //This never gets called for some reason

        //initialises mouse input
        System.out.println("apples");
        setTitle("Simple Frame");
    }

    public static void main(String args[]) {
        System.out.println("I'm before main loop");
        new JSound();
        mainloop();
        System.out.println("I'm after main loop");
        new JSound();
    }

    public static void mainloop() {

        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        while (running) {
            long lastTime = System.nanoTime();
            double amountOfTicks = 60.0;
            double ns = 1000000000 / amountOfTicks;
            double delta = 0;
            long timer = System.currentTimeMillis();
            int frames = 0;
            while (running) {
                long now = System.nanoTime();
                delta += (now - lastTime) / ns;
                lastTime = now;
                while (delta >= 1) {
                    ticks++;
                    delta--;
                }
                if (running) {
                    tick();
                }
                frames++;

                if (System.currentTimeMillis() - timer > 1000) {
                    timer += 1000;
                    System.out.println("FPS: " + frames + " Ticks: " + ticks);
                    frames = 0;
                }
            }
        }
    }

    public static void tick() {

    }
}
首先,您以前的代码从未调用过
JSound
,其次,在
mainLoop
之后调用的任何东西在
running
false
之前都不会执行,所以我只能假设
JSound
是在您调用
mainLoop
之后的某个时间被调用的


首先,您以前的代码从不调用
JSound
,其次,在
mainLoop
之后调用的任何东西在
running
false
之前都不会执行,因此我只能假设
JSound
在您调用
mainLoop

之后的某个时间被调用,这就是构造函数。你必须在别处调用他。在
mainLoop
之后调用的任何东西都不会被执行(直到running为false),这就是构造函数。你必须在别处调用他。在
mainLoop
之后调用的任何东西都将永远不会被执行(直到running为false);我的主要方法是什么?还是不行?我放了新的JSound();我的主要方法是什么?还是不行吗?是的,我把它放在我的主循环之后好吧,有什么理由否决投票吗?它以什么方式不回答老年退休金计划的问题?在哪些方面可以改进?是的,我把它放在我的主要演讲之后好吧,有什么理由反对投票吗?它以什么方式不回答老年退休金计划的问题?在哪些方面可以改进?