如何在JFrame中获取外部事件侦听器的变量?

时间:2016-03-03 03:12:51

标签: java swing jframe

我想返回count的新值,以便下面的while语句可以更改标签的文本。除此之外的其他所有工作。

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;

public class beepBoop extends JApplet { 

    private JButton play = new JButton("Play");
    private JButton loop = new JButton("Loop");
    private JButton stop = new JButton("Stop");
    JLabel songTitle = new JLabel("Test");
    JLabel amountClicks = new JLabel();
    JLabel warning = new JLabel("KEEP ON LISTENING!!!");
    private AudioClip audioClip;

    int count = 0;

    public beepBoop() {

        audioClip = Applet.newAudioClip(getClass().getResource("song1.wav"));
        JPanel pane = new JPanel(new FlowLayout());
        JPanel panel = new JPanel();
        panel.add(play);
        panel.add(loop);
        panel.add(stop);
        panel.add(songTitle);
        panel.add(amountClicks);
        panel.add(warning);
        stop.setEnabled(false);
        add(panel, BorderLayout.CENTER);


        play.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    start();
                    songTitle.setText("LOVE THE SONG");
                    count++;
                    amountClicks.setText("You have pressed button " + count + " times.");
                }
        });
        loop.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    loop();
                    songTitle.setText("YOU REALLY LOVE THIS SONG");
                }
        });
        stop.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    stop();
                    songTitle.setText("WOW YOU ALREADY QUIT IT!!!");
                }
        });
        int countTotal = count;

        while (countTotal >= 5) {
            if (countTotal >= 5) {
                warning.setText("MAYBE YOU'VE HAD TOO MUCH!!!");
            } else if (countTotal >= 10) {
                warning.setText("YOU HAVE HAD WAYYY TO MUCH!!!");
            } else {
                warning.setText("WOW!!!");
            }
        }

    }

    public void start() {

        audioClip.play();
        play.setEnabled(false);
        stop.setEnabled(true);
        loop.setEnabled(true);       
    }

    public void loop() {
        stop();
        audioClip.loop();
        loop.setEnabled(false);
        play.setEnabled(true); 
        stop.setEnabled(true);
    }    

    public void stop() {
        audioClip.stop();
        stop.setEnabled(false);
        loop.setEnabled(true);
        play.setEnabled(true);        
    }    

    public static void main(String[] args) {

        beepBoop player = new beepBoop();

         JFrame frame = new JFrame("beepBoop");
         beepBoop applet = new beepBoop();
        applet.init();

        frame.getContentPane().add(applet, BorderLayout.CENTER);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setSize(225,275);
        frame.setVisible(true);       
    }
}

所以我要想的是,我想做的就是在play.addActionListener之外获取变量,以便下面的while语句能够在触发了大量的点击后显示正确的响应。

1 个答案:

答案 0 :(得分:3)

  

我想返回count的新值,以便下面的while语句可以更改标签的文本。

不,你真的不喜欢。认真。这段代码:

    while (countTotal >= 5) {
        if (countTotal >= 5) {
            warning.setText("MAYBE YOU'VE HAD TOO MUCH!!!");
        } else if (countTotal >= 10) {
            warning.setText("YOU HAVE HAD WAYYY TO MUCH!!!");
        } else {
            warning.setText("WOW!!!");
        }
    }

属于线性用户界面控制台程序,您可以准确地知道代码流何时进入此循环,并且当它完成时,它必须保持循环直到用户输入数据为止。

但这并不是Swing(或任何事件驱动范式使用)程序的工作方式,因为使用事件驱动的代码,您无法完全控制程序流,因为用户拥有自由与呈现给他并启用的任何GUI组件交互。

因此,您可能希望在其中一个ActionListener中使用计数器变量,并且根据计数器变量的状态更改侦听器的行为。 if / else或switch块可以用于此目的。

所以,也许,......

play.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            start();
            songTitle.setText("LOVE THE SONG");
            count++;
            amountClicks.setText("You have pressed button " + count + " times.");

            if (countTotal >= 5) {
                warning.setText("MAYBE YOU'VE HAD TOO MUCH!!!");
            } else if (countTotal >= 10) {
                warning.setText("YOU HAVE HAD WAYYY TO MUCH!!!");
            } else {
                warning.setText("WOW!!!");
            }
        }
});