Klassenmethode aus anderer Klasse aufrufen

Status
Nicht offen für weitere Antworten.

babuschka

Top Contributor
Ich hab ein kleines Problem:
Ein Thread (also eine andere Klasse) muss ein Label aus einer anderen Klasse verändern. Und das jede Sekunde. Jetzt muss das jede Sekunde geschehen. Ich hab das folgendermaßen gemacht:
Einen Timer erstellt, der jede Sekunde eigentlich getPlaylength() aufrufen soll (gaaanz unten). Aber dieZeile ist falsch. also wie mach iches, dass die methode in klasse noise aus klasse PlaylengthTimer aufgerufen werden kann? (Der code ist extrem gekürzt, sollte aber das wichtigste enthalten.


Code:
/*
 * Noise.java
 *
 * Created on 4. Juli 2004, 12:39
 */

import java.awt.*;
import java.text.*;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.*;
import javax.media.*; // JMF

public class Noise extends JFrame implements ControllerListener {
    
    /** All used variables */
    private Player player;
    private Timer timer = new Timer();
    
    // initSetLookAndFeel()
    private String LookAndFeel;
    
    // getPlaylength()
    private int seconds, minutes;
    private Time time, mediaTime;
    private DecimalFormat df;
    
    /** Creates new form Noise */
    public Noise() {
        initComponents();
    }
    
    private void initComponents() {
        lblPlaylength = new javax.swing.JFormattedTextField();
     }

    private void bPlayActionPerformed(java.awt.event.ActionEvent evt) {
        if(player != null)
            player.start();
       [b] timer.schedule(new PlaylengthTimer(), 1000, 1000);[/b]
    }

    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {
        System.exit(0);
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]){
        new Noise().show();
    }
   
    public void getPlaylength() {
        time = player.getDuration();
        mediaTime = player.getMediaTime();
        
        
        seconds = (int)Math.floor(time.getSeconds() - mediaTime.getSeconds()); // Total amount of seconds
        minutes = (int)Math.floor((time.getSeconds() / 60)); // Total amount of minutes
        seconds = seconds - minutes * 60; // Seconds left
        
        df = new DecimalFormat("00");
       lblPlaylength.setText("-" + String.valueOf(df.format(minutes)) + ":" + String.valueOf(df.format(seconds))); // -02:21
    }
    
    public void controllerUpdate(ControllerEvent controllerEvent) {
        if(controllerEvent instanceof EndOfMediaEvent) {
            if(player != null) {
                player.stop();
                player.setMediaTime(new Time(0));
                player.deallocate();
            }
        }
    }
    
    // Variables declaration - do not modify
    private javax.swing.JFormattedTextField lblPlaylength;
    // End of variables declaration
}

class PlaylengthTimer extends TimerTask
{
  public void run()
  {
    Noise.getPlaylength();
  }
}
 
die Methode getPlaylength() sollte static sein (public static void), dann müsste es funzen
 
Fehlermedung ist folgende:

Noise.java [442:1] non-static method getPlaylength() cannot be referenced from a static context
Noise.getPlaylength();
^
1 error
Errors compiling Noise.

Wenn ich jetzt aber public static void getPlaylength mache, bekomme ich 18 fehler:

Noise.java [391:1] non-static variable time cannot be referenced from a static context
time = player.getDuration();
^
Noise.java [391:1] non-static variable player cannot be referenced from a static context
time = player.getDuration();
^
Noise.java [392:1] non-static variable mediaTime cannot be referenced from a static context
mediaTime = player.getMediaTime();
^
Noise.java [392:1] non-static variable player cannot be referenced from a static context
mediaTime = player.getMediaTime();
^
Noise.java [395:1] non-static variable seconds cannot be referenced from a static context
seconds = (int)Math.floor(time.getSeconds() - mediaTime.getSeconds()); // Total amount of seconds
^
Noise.java [395:1] non-static variable time cannot be referenced from a static context
seconds = (int)Math.floor(time.getSeconds() - mediaTime.getSeconds()); // Total amount of seconds
^
Noise.java [395:1] non-static variable mediaTime cannot be referenced from a static context
seconds = (int)Math.floor(time.getSeconds() - mediaTime.getSeconds()); // Total amount of seconds
^
Noise.java [396:1] non-static variable minutes cannot be referenced from a static context
minutes = (int)Math.floor((time.getSeconds() / 60)); // Total amount of minutes
^
Noise.java [396:1] non-static variable time cannot be referenced from a static context
minutes = (int)Math.floor((time.getSeconds() / 60)); // Total amount of minutes
^
Noise.java [397:1] non-static variable seconds cannot be referenced from a static context
seconds = seconds - minutes * 60; // Seconds left
^
Noise.java [397:1] non-static variable seconds cannot be referenced from a static context
seconds = seconds - minutes * 60; // Seconds left
^
Noise.java [397:1] non-static variable minutes cannot be referenced from a static context
seconds = seconds - minutes * 60; // Seconds left
^
Noise.java [399:1] non-static variable df cannot be referenced from a static context
df = new DecimalFormat("00");
^
Noise.java [400:1] non-static variable minutes cannot be referenced from a static context
lblPlaylength.setText("-" + String.valueOf(df.format(minutes)) + ":" + String.valueOf(df.format(seconds))); // -02:21
^
Noise.java [400:1] non-static variable df cannot be referenced from a static context
lblPlaylength.setText("-" + String.valueOf(df.format(minutes)) + ":" + String.valueOf(df.format(seconds))); // -02:21
^
Noise.java [400:1] non-static variable seconds cannot be referenced from a static context
lblPlaylength.setText("-" + String.valueOf(df.format(minutes)) + ":" + String.valueOf(df.format(seconds))); // -02:21
^
Noise.java [400:1] non-static variable df cannot be referenced from a static context
lblPlaylength.setText("-" + String.valueOf(df.format(minutes)) + ":" + String.valueOf(df.format(seconds))); // -02:21
^
Noise.java [400:1] non-static variable lblPlaylength cannot be referenced from a static context
lblPlaylength.setText("-" + String.valueOf(df.format(minutes)) + ":" + String.valueOf(df.format(seconds))); // -02:21
^
18 errors
Errors compiling Noise.
 
musst alle variablen die von der statischen Methode aufgerufen werden auch statisch deklarieren sonst gehts nicht =))
 
Gut, ich hab jetzt einfach mal alles static gemacht, was ich brauchte. Jetzt bvekomme ich nur noch einen Fehler. Aberich kann dami nichts anfangen!

Noise.java [386:1] non-static variable this cannot be referenced from a static context
player.addControllerListener(this);
^
1 error
Errors compiling Noise.
 
ähm... alles statisch zu machen ist wohl ein bisschen überflüssig, oder? wieso übergibst du deinem trhead nicht einfach die instanz von deinem JLabel oder deiner Klasse oder was auch immer?
 
Status
Nicht offen für weitere Antworten.

Zurück
Oben