package com.test1;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
public class JSoundDemo extends JFrame implements ActionListener{
public static void main(String[] args) {
// TODO Auto-generated method stub
JSoundDemo jsd=new JSoundDemo();
} JPanel contentPanel;
JTextField jtf=new JTextField();
JPanel jPanel1=new JPanel();
JLabel jLabel1=new JLabel();
JButton jButton1=new JButton();
JButton jButton2=new JButton();
java.applet.AudioClip clip=null;
java.lang.ClassLoader cl=null; public JSoundDemo(){
super();
this.setVisible(true);
this.setBounds(500, 250, 418,118);
this.setTitle("JSoundDemo");
jLabel1.setFont(new Font("Dialog",Font.BOLD,18));
jLabel1.setToolTipText("");
jLabel1.setText("waiting.....");
jButton1.setText("PLAY");
jButton1.addActionListener(this);
jButton2.setText("STOP");
jButton2.setEnabled(false);
jButton2.addActionListener(this);
jtf.setText(ClassLoader.getSystemResource("Sounds/12.au").toString());
//为什么这句显示空指针异常
contentPanel=(JPanel) this.getContentPane();
contentPanel.add(jtf,BorderLayout.NORTH);
jPanel1.add(jButton1);
jPanel1.add(jButton2);
contentPanel.add(jPanel1,BorderLayout.CENTER);
contentPanel.add(jLabel1,BorderLayout.SOUTH);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==jButton1){
System.out.println("hahh");
jButton1.setEnabled(false);
jButton2.setEnabled(true);
//实例化clip
try {
clip=java.applet.Applet.newAudioClip(new java.net.URL(jtf.getText()));
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
clip.play();
jLabel1.setText("NowPlaying"+jtf.getText());
}
if(e.getSource()==jButton2){
System.out.println("woa");
jButton1.setEnabled(true);
jButton2.setEnabled(false);
clip.stop();
jLabel1.setText("waiting");
}
}
}异常截图如下nulljava se
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
public class JSoundDemo extends JFrame implements ActionListener{
public static void main(String[] args) {
// TODO Auto-generated method stub
JSoundDemo jsd=new JSoundDemo();
} JPanel contentPanel;
JTextField jtf=new JTextField();
JPanel jPanel1=new JPanel();
JLabel jLabel1=new JLabel();
JButton jButton1=new JButton();
JButton jButton2=new JButton();
java.applet.AudioClip clip=null;
java.lang.ClassLoader cl=null; public JSoundDemo(){
super();
this.setVisible(true);
this.setBounds(500, 250, 418,118);
this.setTitle("JSoundDemo");
jLabel1.setFont(new Font("Dialog",Font.BOLD,18));
jLabel1.setToolTipText("");
jLabel1.setText("waiting.....");
jButton1.setText("PLAY");
jButton1.addActionListener(this);
jButton2.setText("STOP");
jButton2.setEnabled(false);
jButton2.addActionListener(this);
jtf.setText(ClassLoader.getSystemResource("Sounds/12.au").toString());
//为什么这句显示空指针异常
contentPanel=(JPanel) this.getContentPane();
contentPanel.add(jtf,BorderLayout.NORTH);
jPanel1.add(jButton1);
jPanel1.add(jButton2);
contentPanel.add(jPanel1,BorderLayout.CENTER);
contentPanel.add(jLabel1,BorderLayout.SOUTH);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==jButton1){
System.out.println("hahh");
jButton1.setEnabled(false);
jButton2.setEnabled(true);
//实例化clip
try {
clip=java.applet.Applet.newAudioClip(new java.net.URL(jtf.getText()));
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
clip.play();
jLabel1.setText("NowPlaying"+jtf.getText());
}
if(e.getSource()==jButton2){
System.out.println("woa");
jButton1.setEnabled(true);
jButton2.setEnabled(false);
clip.stop();
jLabel1.setText("waiting");
}
}
}异常截图如下nulljava se
解决方案 »
- java高手都哪去了?新手有事请教你们
- 急急急急急......在线等待!!!!!!!!!!!!各位大哥帮帮忙....
- 程序调试问题,"找不到符号"!
- 各位前辈你们有没有java联接数据库的源代码?
- log4j 的问题?不难也不简单 希望今天结贴(100)
- 初学 碰到问题了,谢谢 大虾指点!!
- Object 这种类型怎么用啊 , 给个例子
- 博看文思最新iPhone-3G教学大纲惊现北京
- 在jBuilder7内如何配Inprise Application Server?
- Applet问题,applet在tomcat+apache环境下是否支持FileDialog类,急,急 !!!!快来抢分呀!!!!
- 一个有关线程问题的疑惑,比较简单,我是新手,请指教下
- 新手 问答
jtf.setText(ClassLoader.getSystemResource("Sounds/12.au").toString());
至于为什么为空,是因为你的资源路径错误
ClassLoader.getSystemResource("Sounds/12.au").toString()
System.out.println(url);
String text=url.toString();
jtf.setText(text);我把你的代码分解了下,发现url是空的,由于对swing不是很了解,也不知道该怎么给你解释;估计是路径的问题,你可以把12.au放到src目录下,不要sounds目录试试;
ClassLoader.getSystemResource("Sounds/12.au");这句改为ClassLoader.getSystemResource("12.au");