【问题标题】:EventHandler Not Detecting Button Click EventEventHandler 未检测到按钮单击事件
【发布时间】:2013-07-28 16:47:21
【问题描述】:

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

这是我的代码:

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 类中可能有问题,但我不确定它在哪里。我在其他程序中使用过这种方法,效果很好,但不适用于这个。

【问题讨论】:

  • 顺便说一句 - 很好的问题。我特别感谢您使用正确的方式创建 Swing 小程序的可编译代码,并且没有忽略异常。

标签: java swing events event-handling awt


【解决方案1】:
//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 them1,以及 white space2 的布局填充和边框。
    1. 嵌套布局
    2. 空白

【讨论】:

  • face palm 所以代码正在创建新的按钮元素,这些元素没有根据变量名称定义,因此处理程序没有检测到这些名称,因为它们不是包含在 if 语句中?伙计,我是史诗般的菲尔,哈哈。谢谢。
  • ..如果有人认为我在为 OP 的利益而夸大其词,可以在 1 和 2 中看到我的手掌证据。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多