public class MyMusic extends JFrame implements ActionListener{ private JMenuBar jmb; private JMenu jm; private JMenuItem jml; private static Image image; private MyPanel mp=null; private JButton paly; public static void main(String[] args) { // TODO Auto-generated method stub new MyMusic(); } public MyMusic() { // 初始化控件 jmb=new JMenuBar(); jm=new JMenu("文件"); jml=new JMenuItem("File") ; jml.addActionListener(this);//给file注册监听事件 jmb.add(jm); jm.add(jml); //得到图片的资源 image=Toolkit.getDefaultToolkit().createImage(MyMusic.class.getResource("/music.gif")); mp=new MyPanel(); mp.setLayout(null); paly=new JButton("播放"); mp.add(paly); this.add(mp); this.setJMenuBar(jmb); //处理主窗体 this.setTitle("java 音乐播放器"); this.setSize(600, 600); this.setLocationRelativeTo(null); this.setResizable(false); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } //给按钮设置监听的方法 @Override public void actionPerformed(ActionEvent e) { if(e.getActionCommand().equals("File")){ JFileChooser jfc=new JFileChooser(); jfc.setDialogTitle("请选择文件..."); jfc.setSelectedFile(new File("*.*")); jfc.showOpenDialog(null); jfc.setVisible(true); //得到用户的路径 String FileName=jfc.getSelectedFile().getAbsolutePath(); System.out.println(FileName); } } //自己写一个JPanel 继承JPanel 然后在JPanel上画图片 class MyPanel extends JPanel{ @Override public void paint(Graphics g) { // TODO Auto-generated method stub super.paint(g); g.drawImage(image,0,0,this); } } }