如何修复我的 actionevent 错误和 actionlistener 错误

问题描述

 public class JProgressBar06 
{   
 public static void main(String[] args) 
{
    KFrame frame = new KFrame();
    
    frame.setSize(900,600);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}
}

 class KFrame extends JFrame implements ActionListener
{
  String[] songs = {
                "01 - Moonlight.wav","02 - Dangerous Woman.wav","03 - Be Alright.wav"
                };
JButton[] buttons = new JButton[songs.length];
    
KFrame()
{       
    JPanel panel = new JPanel();
    panel.setLayout(null);
    this.add(panel);
    panel.setVisible(true);
    
    for(int i=0; i<buttons.length; i++)
    {
        buttons[i] = createJB("BUTTON 0" + String.valueOf(i+1),100,i*50+150,200,25,Color.BLUE,Color.WHITE);
        panel.add(buttons[i]);
                
        JProgressBar jpb = createJPB(350,Color.GREEN);
        panel.add(jpb);
    }
}


public void actionPerformed(ActionEvent ae)
    {
        File file = new File("sounds");
        
        AudioInputStream audioInputStream = null;
        try
        {
        audioInputStream = AudioSystem.getAudioInputStream(file);
        }
        catch (UnsupportedAudioFileException uafe)
        {
            uafe.printStackTrace();
        }
        catch (IOException ioe)
        {
            ioe.printStackTrace();
        }
        AudioFormat format = audioInputStream.getFormat();
        long frames = audioInputStream.getFrameLength();
        double durationInSeconds = (frames+0.0)/format.getFrameRate();
        

*你能帮我改正错误吗

我不知道我的代码是否结构良好

我有 jpb 问题

我在使用 actionevent 或 actionlistener 时遇到问题

感谢您的理解*

        int i=0;
        
        if(ae.getSource() == buttons[i])
        {
            jpb.setVisible(true);
            try
            {
                Clip clip = AudioSystem.getClip();
                clip.open(AudioSystem.getAudioInputStream(file));
                clip.start();
                
                while (i <= 100)
                {
                  Thread.sleep((long) (durationInSeconds*1000/100));
                  jpb.paintImmediately(0,25);
                  jpb.setValue(i);
                  i++;         
                }
                System.out.println(durationInSeconds);
             } 
              catch (Exception e)
              {
                  e.printStackTrace();  
              }
        }
    }

private JButton createJB(String text,int x,int y,int width,int height,Color background,Color foreground)
{
    JButton jb = new JButton(text);
    jb.setBounds(x,y,width,height);
    jb.setBackground(background);
    jb.setForeground(foreground);
    return jb;      
}

private JProgressBar createJPB(int x,int heigth,Color foreground)
{
    JProgressBar jpb = new JProgressBar(0,100);
    jpb.setValue(0);
    jpb.setStringPainted(true);
    jpb.setBounds(x,heigth);
    jpb.setForeground(foreground);
    jpb.setVisible(true);
    return jpb;
}
}

*你能帮我改正错误吗

我不知道我的代码是否结构良好

我有 jpb 问题

我在使用 actionevent 或 actionlistener 时遇到问题

感谢您的理解*

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...