Komunikat przed zamknięciem programu. ( reached of the while persing )

0

Cześć poradziłem sobie z wątkami i już elegancko przerywa mi wykonanie utworu i wszystko ładnie działa jednak wkradł mi się jakiś prosty błąd i znów zawiesił mnie na kilka dni. Mianowicie ostatni nawias ,,}.. ciągle świeci się na czerwono i pokazuje błąd ( reached of the while persing). Co zrobiłem w tym kodzie źle? Dopiero zaczynam przygodę dlatego proszę o wyrozumiałość ;)

package wordsy;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;

public class Clear2 implements Runnable {

    public Thread gameThread;
    public static boolean running = false;

    public synchronized void start() {
        if (running) {
            return;
        }
        running = true;
        gameThread = new Thread(this);
        gameThread.start();
    }

    public synchronized void stop() {
        if (!running) {
            return;
        }
        running = false;
        try {
            gameThread.join();
        } catch (InterruptedException ex) {
            Logger.getLogger(Clear2.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void run() {

        {
            try {
                File file = new File("C:\\Efekty dzwiekowe\\test\\approach.mp3");
                FileInputStream fis = new FileInputStream(file);
                BufferedInputStream bis = new BufferedInputStream(fis);

                try {
                    Player player = new Player(bis);
                    player.play();
                } catch (JavaLayerException ex) {
                }

            } catch (IOException e) {
            }
        }
    }

    public static void main(String[] args) {

        Runnable zadanieWatku = new NewMp3();
        Thread mojWatek = new Thread(zadanieWatku);
        mojWatek.start();

        JFrame ramka = new JFrame();
        ramka.setSize(500, 500);
        ramka.setVisible(true);

        JButton button = new JButton();

        button.setText("stop");

        button.setVisible(true);
        ramka.add(button);

        button.addActionListener(new ActionListener() {
            @Override

            public void actionPerformed(ActionEvent e) {
                mojWatek.stop();
            } -------------------------------------------- **// - tutaj jest problem opisany ( reached of the while persing )**


1

W przypadku maina i buttona musisz wszystko uczciwie pozamykać. Coś takiego:

button.addActionListener(new ActionListener() {
   @Override
   public void actionPerformed(ActionEvent e) {
      mojWatek.stop();
   }
});

Czyli nie tylko wąsaty nawias ale też ) który domknie ( otwarty za addActionListener.

0

Kurcze, niestety chyba to nie to, dodałem na końcu nawiasy ale dalej ten sam błąd :(

  button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                
                mojWatek.stop();
            }
  
      });
   
0

Cały kod:

 

package wordsy;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;

public class Clear2 implements Runnable {

    public Thread gameThread;
    public static boolean running = false;

    public synchronized void start() {
        if (running) {
            return;
        }
        running = true;
        gameThread = new Thread(this);
        gameThread.start();
    }

    public synchronized void stop() {
        if (!running) {
            return;
        }
        running = false;
        try {
            gameThread.join();
        } catch (InterruptedException ex) {
            Logger.getLogger(Clear2.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void run() {

        {
            try {
                File file = new File("C:\\Efekty dzwiekowe\\test\\approach.mp3");
                FileInputStream fis = new FileInputStream(file);
                BufferedInputStream bis = new BufferedInputStream(fis);
                try {
                    Player player = new Player(bis);
                    player.play();
                } catch (JavaLayerException ex) {
                }
            } catch (IOException e) {
            }
        }
    }

    public static void main(String[] args) {

        Runnable zadanieWatku = new NewMp3();
        Thread mojWatek = new Thread(zadanieWatku);
        mojWatek.start();

        JFrame ramka = new JFrame();
        ramka.setSize(500, 500);
        ramka.setVisible(true);
        JButton button = new JButton();
        button.setText("stop");
        button.setVisible(true);
        ramka.add(button);

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                mojWatek.stop();
            }
        });




1

A gdzie zamykasz main i klasę? Dodaj jeszcze 2 razy } na końcu.

0

To autor klepie kod w notatniku?

0

Raczej nie, kod w pełni pisany (Notatnik, Notepad++) zawiera zazwyczaj takie importy:

import java.awt.event.*;
import java.io.*;
...

1 użytkowników online, w tym zalogowanych: 0, gości: 1