import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.util.zip.*;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;public class ZipTest
{
public static void main(String[] args)
{
ZipTestFrame frame = new ZipTestFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class ZipTestFrame extends JFrame
{
public ZipTestFrame()
{
setTitle("zip"); JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("File");
JMenuItem openItem = new JMenuItem("open");
menu.add(openItem);
openItem.addActionListener(new OpenAction());

JMenuItem exitItem = new JMenuItem ("Exit");
menu.add(exitItem);
exitItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
});
menuBar.add(menu);
setJMenuBar(menuBar);
JTextArea fileText = new JTextArea();
final JComboBox fileCombo = new JComboBox();
fileCombo.addActionListener(new ActionListener()
{
/**
这里没有实例化OpenAction这个类,是如果调用到的loadZipFile这个方法的?
*/
public void actionPerformed(ActionEvent event)
{
OpenAction open = new OpenAction();
open.loadZipFile((String)fileCombo.getSelectedItem());
}
}

);
getContentPane().add(fileCombo,BorderLayout.SOUTH);
getContentPane().add(new JScrollPane(fileText),BorderLayout.CENTER);

}

private  class OpenAction implements ActionListener
{

public void actionPerformed(ActionEvent event)
{
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
ExtensionFileFilter filter=new ExtensionFileFilter();
filter.addExtension(".zip");
filter.addExtension(".jar");
filter.setDescription("ZIP archives");
chooser.setFileFilter(filter);
int r=chooser.showOpenDialog(ZipTestFrame.this);
if (r == JFileChooser.APPROVE_OPTION)
{
zipname = chooser.getSelectedFile().getPath();
scanZipFile();
}
} public void scanZipFile()
{
 fileCombo.removeAllItems();
 try
{
 ZipInputStream zin = new ZipInputStream(new FileInputStream(zipname));
 ZipEntry entry;
 while ((entry = zin.getNextEntry())!=null)
 {
 fileCombo.addItem(entry.getName());
 zin.closeEntry();
 }
 zin.close();
 }
 catch(IOException e)

 {
  e.printStackTrace();  
}
}
public  void loadZipFile(String name)
{
    try
{
ZipInputStream zin = new ZipInputStream(new FileInputStream(zipname));
ZipEntry entry;
fileText.setText("");
while((entry = zin.getNextEntry())!=null)
{
if (entry.getName().equals(name))
{
BufferedReader in=new BufferedReader(new InputStreamReader(zin));
String line;
while ((line = in.readLine())!=null)
{
fileText.append(line);
fileText.append("\n");
}
}
zin.closeEntry();
}
zin.close();
}
catch(IOException e)
{
e.printStackTrace();
}
} public static final int DEFAULT_WIDTH = 400;
public static final int DEFAULT_HEIGHT = 300;

private JComboBox fileCombo;
private JTextArea fileText;
private String zipname;}class ExtensionFileFilter extends FileFilter
{ public void addExtension(String extension)
{
if (!extension.startsWith("."))
{
extension="."+extension;
extensions.add(extension.toLowerCase());
}
} public void setDescription(String aDescription)
{
description = aDescription;
} public String getDescription()
{
return description;
} public boolean accept(File f)
{
if (f.isDirectory())
{
return true;
}
return true;
}

private String description="";
private ArrayList extensions=new ArrayList();
}}
请指教

解决方案 »

  1.   

    你的内部类中的fileCombo成员变量没有实例化
      

  2.   


    public void scanZipFile()这个方法在调用fileCombo.removeAllItems();前先实例化一下fileCombo
    fileCombo = new JComboBox() ;
      

  3.   

    提示空指针是在86行,也就是在初始化文件时候说的fileCombo异常空
    还没到调用fileCombo的监听啊
    所以我感觉应该不是内部类的问题吧
      

  4.   

    scanZipFile()中确实没有实力化fileCombo
    但我实力化了还不对你改个完整的给我行不,kevinliuu
      

  5.   

    差不多OK了
    问题在于,如何把,初始化的fileCombo,传入到scanZipFile()
     但现在初始化的fileCombo定义为 final
      

  6.   

    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.util.*;
    import java.util.zip.*;
    import javax.swing.*;
    import javax.swing.filechooser.FileFilter;public class ZipTest
    {
        public static void main(String[] args)
        {
            ZipTestFrame frame = new ZipTestFrame();
            frame.setSize(400, 300);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
    }
    class ZipTestFrame extends JFrame
    {
         private final static JComboBox fileCombo = new JComboBox();
         private static JTextArea fileText = new JTextArea();
         private static String zipname;
    //    JComboBox fileCombo ;//= new JComboBox();
        public ZipTestFrame()
        {
            setTitle("zip");        JMenuBar menuBar = new JMenuBar();
            JMenu menu = new JMenu("File");
            JMenuItem openItem = new JMenuItem("open");
            menu.add(openItem);
            openItem.addActionListener(new OpenAction());        JMenuItem exitItem = new JMenuItem("Exit");
            menu.add(exitItem);
            exitItem.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
                {
                    System.exit(0);
                }
            });
            menuBar.add(menu);
            setJMenuBar(menuBar);
            
           
            fileCombo.addActionListener(new ActionListener()
            {
                /**
                 这里没有实例化OpenAction这个类,是如果调用到的loadZipFile这个方法的?
                 */
                public void actionPerformed(ActionEvent event)
                {
                    OpenAction open = new OpenAction();
                    open.loadZipFile((String) fileCombo.getSelectedItem());
                }
            }        );
            getContentPane().add(fileCombo, BorderLayout.SOUTH);
            getContentPane().add(new JScrollPane(fileText), BorderLayout.CENTER);    }    private class OpenAction implements ActionListener
        {
            public void actionPerformed(ActionEvent event)
            {
                JFileChooser chooser = new JFileChooser();
                chooser.setCurrentDirectory(new File("."));
                ExtensionFileFilter filter = new ExtensionFileFilter();
                filter.addExtension(".zip");
                filter.addExtension(".jar");
                filter.setDescription("ZIP archives");
                chooser.setFileFilter(filter);
                int r = chooser.showOpenDialog(ZipTestFrame.this);
                if (r == JFileChooser.APPROVE_OPTION)
                {
                    zipname = chooser.getSelectedFile().getPath();
                    scanZipFile();
                }
            }        public void scanZipFile()
            {
                ZipTestFrame.fileCombo.removeAllItems();
                try
                {
                    ZipInputStream zin = new ZipInputStream(new FileInputStream(
                        zipname));
                    ZipEntry entry;
                    while ((entry = zin.getNextEntry()) != null)
                    {
                        fileCombo.addItem(entry.getName());
                        zin.closeEntry();
                    }
                    zin.close();
                }
                catch (IOException e)            {
                    e.printStackTrace();
                }
            }
            public void loadZipFile(String name)
            {
                try
                {
                    ZipInputStream zin = new ZipInputStream(new FileInputStream(zipname));
                    ZipEntry entry;
                    ZipTestFrame.fileText.setText("");
                    while ((entry = zin.getNextEntry()) != null)
                    {
                        if (entry.getName().equals(name))
                        {
                            BufferedReader in = new BufferedReader(new
                                InputStreamReader(zin));
                            String line;
                            while ((line = in.readLine()) != null)
                            {
                                fileText.append(line);
                                fileText.append("\n");
                            }
                        }
                        zin.closeEntry();
                    }
                    zin.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }        public static final int DEFAULT_WIDTH = 400;
            public static final int DEFAULT_HEIGHT = 300;        //private JComboBox fileCombo= new JComboBox();
           // private JTextArea fileText;
            //private String zipname;    }
        class ExtensionFileFilter extends FileFilter
        {        public void addExtension(String extension)
            {
                if (!extension.startsWith("."))
                {
                    extension = "." + extension;
                    extensions.add(extension.toLowerCase());
                }
            }        public void setDescription(String aDescription)
            {
                description = aDescription;
            }        public String getDescription()
            {
                return description;
            }        public boolean accept(File f)
            {
                if (f.isDirectory())
                {
                    return true;
                }
                return true;
            }        private String description = "";
            private ArrayList extensions = new ArrayList();
        }
    }