gpt4 book ai didi

java - EventHandler 未检测到按钮单击事件

转载 作者:行者123 更新时间:2023-11-30 07:15:17 26 4
gpt4 key购买 nike

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

这是我的代码:

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

最佳答案

//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,代码有效地将它们重新声明为局部变量。从而“隐藏” Action 监听器正在比较的类属性。

更多提示

  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. 空白

关于java - EventHandler 未检测到按钮单击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17914830/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com