读取二进制文件并在Jlist

时间:2015-06-04 16:57:53

标签: java swing file file-io jpanel

我如何读取二进制文件并在JPanel.Up中显示到现在我已经初始化了一个jframe,jpanel为jbutton当我运行代码时按钮和框架出现但当我点击显示按钮我需要显示的是这里的文件数据就是我所做的代码。

    public class GraphicalDisplay{
        private JFrame cframe;
        private JButton showButton;
        public GraphicalDisplay() {

        }

    public void displayCompanyList() {
        //creating frame for the display of contents and containers and other elements like jlabel and jbutton
        cframe = new JFrame("List of Companies");
        cframe.setLocation(700,150);
        cframe.setSize(600,300);

        //creating button 
        showButton =new JButton("Show list");

        //create panel to hold button
        JPanel bpanel=new JPanel();
        bpanel.add(showButton);
        //add panel to the frame
        cframe.add(bpanel,BorderLayout.CENTER);
        cframe.setVisible(true);

        showButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {

                 byte [] buffer =null;
                 File a_file = new File("companies.dat");
                 try
                 {
                 FileInputStream fis = new FileInputStream("companies.dat");
                 int length = (int)a_file.length();
                 buffer = new byte [length];
                 fis.read(buffer);
                 fis.close();
                 }
                 catch(IOException e)
                 {
                 e.printStackTrace();
                 }
            }
        });         
    }
}

0 个答案:

没有答案