EventHandler未检测到按钮单击事件

时间:2013-07-29 01:39:21

标签: java swing events event-handling awt

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

这是我的代码:

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语句航行并落在调试器中的其他位置。我认为在EventHandler类中可能有问题,但我不确定它在哪里。我已经在其他程序中使用过这种方法,它的工作正常,但不适用于此。

1 个答案:

答案 0 :(得分: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组件,而是使用布局管理器,或combinations of them,以及布局填充和& white space的边框。

    Java GUI可能必须在许多平台上工作,在不同的屏幕分辨率和使用不同的PLAF。因此,它们不利于组件的精确放置。要组织强大的GUI组件,而是使用布局管理器,或combinations of them 1 ,以及布局填充和& white space 2 的边框。
    1. 嵌套布局
    2. 白色空间