第19回セミナー

ファイル操作

Sample7.java

import java.io.*;
import java.awt.*;
import javax.swing.*;

public class Sample7 extends JFrame {
  private JLabel[] lb;
  private Icon ic;
  private JScrollPane[] sp;
  private JTabbedPane tp;

  public static void main(String[] args){
    (new Sample7()).setVisible(true);
  }

  public Sample7(){
    super("サンプル");
    setSize(300,200);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    File fl=new File(".");
    File[] fls=fl.listFiles(new MyFileFilter());

    lb=new JLabel[fls.length];
    sp=new JScrollPane[fls.length];
    tp=new JTabbedPane();

    for(int i=0;i<fls.length;i++){
      ic=new ImageIcon(fls[i].getName());
      lb[i]=new JLabel(ic);
      sp[i]=new JScrollPane(lb[i]);
      tp.add(fls[i].getName(),sp[i]);
    }
    add(tp);
  }

  class MyFileFilter implements FilenameFilter {
    public boolean accept(File f,String n){
      if(n.toLowerCase().endsWith(".png")||
         n.toLowerCase().endsWith(".jpg"))
        return true;
      return false;
    }
  }
}

Sample8.java

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Sample8 extends JFrame implements ActionListener,FocusListener {
  private JTextArea[] ta;
  private JScrollPane[] sp;
  private JPanel pn;
  private JDesktopPane dp;
  private JInternalFrame[] itf;
  private JTextArea active_ta;

  public static void main(String[] args){
    (new Sample8()).setVisible(true);
  }

  public Sample8(){
    super("サンプル");
    setSize(500,400);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    dp=new JDesktopPane();
    JMenuBar mb=new JMenuBar();
    JMenu mn=new JMenu("編集");
    mn.add("切り取り").addActionListener(this);
    mn.add("コピー").addActionListener(this);
    mn.add("貼り付け").addActionListener(this);
    mb.add(mn);
    setJMenuBar(mb);

    File fl=new File(".");
    File[] fls=fl.listFiles(new MyFileFilter());

    try{
      ta=new JTextArea[fls.length];
      sp=new JScrollPane[fls.length];
      itf=new JInternalFrame[fls.length];

      for(int i=0;i<fls.length;i++){
        ta[i]=new JTextArea();
        ta[i].addFocusListener(this);
        sp[i]=new JScrollPane(ta[i]);
        itf[i]=new JInternalFrame(fls[i].getName(),true,true,true,true);

        BufferedReader br=new BufferedReader(new FileReader(fls[i]));
        ta[i].read(br,null);
        br.close();

        itf[i].add(sp[i]);
        dp.add(itf[i]);
        itf[i].setLocation(i*10,i*10);
        itf[i].setSize(300,200);
        itf[i].setVisible(true);
      }
      add(dp);
    }
    catch(IOException e){ System.err.println(e); }
  }

  public void actionPerformed(ActionEvent ev){
    String cmd=ev.getActionCommand();
    if(cmd.equals("切り取り"))
      active_ta.cut();
    if(cmd.equals("コピー"))
      active_ta.copy();
    if(cmd.equals("貼り付け"))
      active_ta.paste();
  }

  public void focusGained(FocusEvent ev){
    Object obj=ev.getSource();
    if(obj instanceof JTextArea)
      active_ta=(JTextArea)obj;
  }
  public void focusLost(FocusEvent ev){}

  class MyFileFilter implements FilenameFilter {
    public boolean accept(File f,String n){
      if(n.toLowerCase().endsWith(".java"))
        return true;
      return false;
    }
  }
}

正規表現

大規模ソフトウェアの開発方法

Javaセミナー2014


トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS