2

我正在尝试创建一个简单的小程序,它在单击播放或循环按钮时播放音频文件,然后在单击停止时停止。但是由于某种原因,我的事件处理无法正常工作,我无法弄清楚原因。

这是我的代码:

import java.applet.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;


public class JWater extends JApplet {

public void init()
{
    sound = getAudioClip(getCodeBase(),"water001.au"); //Set audio file

    //Prepare to try and create GUI
    try
    {
        SwingUtilities.invokeAndWait(new Runnable()
        {
            public void run()
            {                   
                makeGUI(); //Create the GUI                         
            }
        });
    }

    catch (Exception e)
    {
        System.err.println("GUI was not created successfully"); //In case something goes wrong
    }               
}

private void makeGUI() {

    //Create content pane which everything will reside in
    setBounds(100, 100, 317, 189);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);
    setVisible(true);

    //Create and add panel, buttons and the label will be added to this 
    JPanel panel = new JPanel();
    panel.setBounds(0, 0, 300, 150);
    contentPane.add(panel);
    panel.setLayout(null);

    //Create and add the label
    JLabel lblNewLabel = new JLabel("This is the sound of flowing water.");
    lblNewLabel.setBounds(54, 46, 250, 14);
    panel.add(lblNewLabel);

    jOP = new JOptionPane();
    jOP.setBounds(10,40,250,10);
    panel.add(jOP);


    //Create and add media control buttons      
    JButton btnPlay = new JButton("Play");
    btnPlay.setBounds(10, 116, 89, 23);
    panel.add(btnPlay);

    JButton btnLoop = new JButton("Loop");
    btnLoop.setBounds(109, 116, 89, 23);
    panel.add(btnLoop);

    JButton btnStop = new JButton("Stop");
    btnStop.setBounds(208, 116, 89, 23);
    panel.add(btnStop);

    //Create an event handler named handler
    EventHandler handler = new EventHandler();

    //Add these action listeners to detect when a button is clicked
    btnPlay.addActionListener(handler);
    btnLoop.addActionListener(handler);
    btnStop.addActionListener(handler);
}

//Implement the event handler class for button clicks
    class EventHandler implements ActionListener
    {       
        public void actionPerformed(ActionEvent event)
        {
            if (event.getSource() == btnPlay)   
            {

            }

            else if (event.getSource() == btnLoop)  
            {

            }

            else if (event.getSource() == btnStop)  
            {                           
            }

            else
            {           
                JOptionPane.showMessageDialog(null,"Message not detected or not sent!!!  Recevied " + event.getSource(),"ERROR CODE 1",JOptionPane.ERROR_MESSAGE);
            }
        }       
    }

    AudioClip sound;
    JPanel contentPane,panel;
    JButton btnPlay,btnLoop,btnStop;
    JOptionPane jOP;
}

每当单击一个按钮时,它就会通过我的 if 语句并落在调试器中的 else 上。我认为 EventHandler 类中可能有问题,但我不确定它在哪里。我在其他程序中使用过这种方法,它工作得很好,但不适用于这个。

4

1 回答 1

4
//Create and add media control buttons      
JButton btnPlay = new JButton("Play");
btnPlay.setBounds(10, 116, 89, 23);
panel.add(btnPlay);

JButton btnLoop = new JButton("Loop");
btnLoop.setBounds(109, 116, 89, 23);
panel.add(btnLoop);

JButton btnStop = new JButton("Stop");
btnStop.setBounds(208, 116, 89, 23);
panel.add(btnStop);

应该:

//Create and add media control buttons      
btnPlay = new JButton("Play");
btnPlay.setBounds(10, 116, 89, 23);
panel.add(btnPlay);

btnLoop = new JButton("Loop");
btnLoop.setBounds(109, 116, 89, 23);
panel.add(btnLoop);

btnStop = new JButton("Stop");
btnStop.setBounds(208, 116, 89, 23);
panel.add(btnStop);

解决您看到的直接问题。通过JButton在构建按钮之前添加,代码有效地将它们重新声明为局部变量。从而“隐藏”动作侦听器正在比较的类属性。

更多提示

  1. setBounds(100, 100, 317, 189); 删除它。小程序大小应在 HTML 中设置。
  2. Java GUI 可能必须在多个平台、不同的屏幕分辨率和使用不同的 PLAF 上工作。因此,它们不利于组件的精确放置。要为健壮的 GUI 组织组件,请改为使用布局管理器或它们的组合,以及用于空白空间的布局填充和边框。

    Java GUI 可能必须在多个平台、不同的屏幕分辨率和使用不同的 PLAF 上工作。因此,它们不利于组件的精确放置。要为强大的 GUI 组织组件,请改用布局管理器或它们的组合1,以及空白区域的布局填充和边框2
    1. 嵌套布局
    2. 白色空间

于 2013-07-29T01:41:52.070 回答