//************************************************************* // 外部アーカイブの利用テスト // 「OuterJarTest」 // //          作 成 者:ルート高菜    //          作成開始月:2007/8 //          最終更新月:2007/11 [TN116/J106] //************************************************************* import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextArea; public class OuterJarTest extends JFrame implements ActionListener { //Eclipseの場合、シリアライズ可能クラスでこれがないと警告が出る private static final long serialVersionUID=0; public static void main(String[] args) { OuterJarTest frame=new OuterJarTest(); frame.setTitle("OuterJarTest"); // frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //右上×印で終了 frame.pack(); frame.setVisible(true); frame.setLocationRelativeTo(null); //画面の真ん中に表示 } // JButton sameDirClass; JButton sameDirImage; JButton sameDirText; JButton sameDirZipClass; JButton sameDirZipImage; JButton sameDirZipText; JButton otherDirClass; JButton otherDirImage; JButton otherDirText; JButton OtherDirZipClass; JButton OtherDirZipImage; JButton OtherDirZipText; // public OuterJarTest(){ JPanel panel=new JPanel(); panel.setLayout(new GridLayout(4,4)); panel.add(new JLabel("same dir JAR")); panel.add(sameDirClass=new JButton("class")); panel.add(sameDirImage=new JButton("image")); panel.add(sameDirText=new JButton("text")); panel.add(new JLabel("same dir ZIP")); panel.add(sameDirZipClass=new JButton("class")); panel.add(sameDirZipImage=new JButton("image")); panel.add(sameDirZipText=new JButton("text")); panel.add(new JLabel("other dir JAR")); panel.add(otherDirClass=new JButton("class")); panel.add(otherDirImage=new JButton("image")); panel.add(otherDirText=new JButton("text")); panel.add(new JLabel("other dir ZIP")); panel.add(OtherDirZipClass=new JButton("class")); panel.add(OtherDirZipImage=new JButton("image")); panel.add(OtherDirZipText=new JButton("text")); //コンテナ枠 getContentPane().add(panel); // sameDirClass.addActionListener(this); sameDirImage.addActionListener(this); sameDirText.addActionListener(this); sameDirZipClass.addActionListener(this); sameDirZipImage.addActionListener(this); sameDirZipText.addActionListener(this); otherDirClass.addActionListener(this); otherDirImage.addActionListener(this); otherDirText.addActionListener(this); OtherDirZipClass.addActionListener(this); OtherDirZipImage.addActionListener(this); OtherDirZipText.addActionListener(this); } // public void actionPerformed(ActionEvent e) { Object object=e.getSource(); if(object==sameDirClass){ SameDir same=new SameDir(); same.showDialog(this); }else if(object==sameDirZipClass){ SameDirZip samezip=new SameDirZip(); samezip.showDialog(this); }else if(object==otherDirClass){ OtherDir other=new OtherDir(); other.showDialog(this); }else if(object==OtherDirZipClass){ OtherDirZip otherzip=new OtherDirZip(); otherzip.showDialog(this); }else if(object==sameDirImage){ ImageIcon icon=new ImageIcon(getClass().getResource("image/same.jpg")); showImageFrame("same dir",icon); }else if(object==sameDirZipImage){ ImageIcon icon=new ImageIcon(getClass().getResource("image/samezip.jpg")); showImageFrame("same dir zip",icon); }else if(object==otherDirImage){ ImageIcon icon=new ImageIcon(getClass().getResource("image/other.jpg")); showImageFrame("other dir",icon); }else if(object==OtherDirZipImage){ ImageIcon icon=new ImageIcon(getClass().getResource("image/otherzip.jpg")); showImageFrame("other dir zip",icon); }else if(object==sameDirText){ String data=getText("text/same.txt"); if(data!=null){ showTextFrame("same dir",data); } }else if(object==sameDirZipText){ String data=getText("text/samezip.txt"); if(data!=null){ showTextFrame("same dir zip",data); } }else if(object==otherDirText){ String data=getText("text/other.txt"); if(data!=null){ showTextFrame("other dir",data); } }else if(object==OtherDirZipText){ String data=getText("text/otherzip.txt"); if(data!=null){ showTextFrame("other dir zip",data); } } } // private void showImageFrame(String title,ImageIcon image){ JFrame imageFrame=new JFrame(title); imageFrame.getContentPane().add(new JButton(image)); imageFrame.pack(); imageFrame.setVisible(true); imageFrame.setLocationRelativeTo(null); //画面の真ん中に表示 } private void showTextFrame(String title,String text){ JFrame textFrame=new JFrame(title); textFrame.getContentPane().add(new JTextArea(text)); textFrame.pack(); textFrame.setVisible(true); textFrame.setLocationRelativeTo(null); //画面の真ん中に表示 } private String getText(String path){ try{ //jar内のテキストファイルの入力ストリームを得る InputStream is=getClass().getResourceAsStream(path); //Stream(古い系統)からReader(新しい系統)へ変換する BufferedReader br=new BufferedReader(new InputStreamReader(is)); StringBuffer buf=new StringBuffer(); String line; while((line=br.readLine())!=null){ buf.append(line+"\n"); } br.close(); // return buf.toString(); }catch(Exception exc){ return null; } } }