我用的是swing组件,在jFrame上做的!谢谢了!

解决方案 »

  1.   

    import java.awt.*;
    import java.awt.event.*;
    import java.io.File;
    import java.io.IOException;import javax.imageio.ImageIO;
    import javax.swing.*;
    import java.io.*;import sun.audio.*;public class ButtonEvent
    {
        public static void main(String[] args)
        {
            ButtonFrame frame = new ButtonFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
    }
    class ButtonFrame extends JFrame
    {
        public ButtonFrame()
        {
            setTitle("ButtonTest");
            setSize(400, 400);
            ButtonPanel panel = new ButtonPanel();
            add(panel);
        }
    }
    class ButtonPanel extends JPanel
    {
        public ButtonPanel()
        {
            JButton yellowButton = new JButton("Yellow");
            JButton buleButton = new JButton("Blue");
            JButton redButton = new JButton("read");
            add(yellowButton);
            add(buleButton);
            add(redButton);
            ColorAction yellowAction = new ColorAction("111");
            ColorAction blueAction = new ColorAction("222");
            ColorAction redAction = new ColorAction("333");
            yellowButton.addActionListener(yellowAction);
            buleButton.addActionListener(blueAction);
            redButton.addActionListener(redAction);
        }
        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
        }    private class ColorAction implements ActionListener
        {
            public ColorAction(String s)
            {
                w = s;
            }
            public void actionPerformed(ActionEvent event)
            {
                JFrame frame = new JFrame();            frame.setVisible(true);
                frame.setTitle(w);            frame.setSize(300, 200);            ImagePanel panel = new ImagePanel();
                frame.add(panel);            try
                {
                    FileInputStream fileau = new FileInputStream("C:\\WINDOWS\\Media\\tada.wav");
                    AudioStream as = new AudioStream(fileau);
                    AudioPlayer.player.start(as);
                }
                catch (FileNotFoundException ex)
                {
                    frame.setTitle("Error");
                }
                catch (IOException ex)
                {
                    frame.setTitle("Error");
                }        }        class ImagePanel extends JPanel
            {
                ImagePanel()
                {
                    try
                    {
                        image = ImageIO.read(new File("d:\\1.jpg"));
                    }
                    catch (IOException e)
                    {
                        e.printStackTrace();
                    }
                }
                public void paintComponent(Graphics g)
                {
                    super.paintComponent(g);
                    g.drawImage(image, 0, 0, null);
                }
            }
        }    private String w;
        private Image image;
    }
      

  2.   

    if(e.getSource()==save){
        fileDlg=new FileDialog(this,"保存文件",FileDialog.SAVE);
        fileDlg.show();
        fileName=fileDlg.getFile();
        str=ta.getText();
        byteBuf=str.getBytes();
        try{
         FileOutputStream out=new FileOutputStream(fileName);
         out.write(byteBuf);
         out.close();
        }catch(IOException ioe){}
       } 这是保存功能,另存效仿这个就OK了,你自己想一想。
      

  3.   


    咱只写了awt做的,还没用Swing呢
      

  4.   

           if(e.getSource()==save)//保存
             {
                try
                {
                    fw=new FileWriter("t102output.txt");//t102output.txt你想要存放的文件名,可以任意。
                    fw.write(area.getText());                
                    fw.close();
                }catch(Exception  ex)
                {
                    ex.printStackTrace();
                }
            }