@634

正規表現エディタ - 枠組み(GUI)作成

Advertisement

GUIを作る

ここでの作業
  • フレームの作成
  • スクロールできるテキストエリアを2つ作成(元のテキスト表示用と抽出結果表示用)
  • メニューを作る(テキスト読み込み・テキスト保存・アプリケーションの終了・変換)

GUIを作った

何はともあれ、サンプルコード

MainFrameクラス(フレーム)
package regularexpressioneditor.gui;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextPane;

public class MainFrame extends JFrame{

    // 左パネル
    JTextPane text;                // テキストパネル
    JScrollPane spanel_left;    // スクロールパネル

    // 右パネル
    JTextPane result;            // テキストパネル
    JScrollPane spanel_right;    // スクロールパネル

    // メニュー
    JMenuBar menuBar;            //  メニューバー
    JMenu file;
    JMenu edit;
    JMenuItem open;
    JMenuItem save;
    JMenuItem quit;
    JMenuItem check;

    /**
     * 初期設定を行う
     */
    public MainFrame(){

        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        // 左ウィンドウ
        text = new JTextPane();
        spanel_left = new JScrollPane(text);

        // 右ウィンドウ
        result = new JTextPane();
        spanel_right = new JScrollPane(result);

        //メニュー
        menuBar = new JMenuBar();
        file = new JMenu("ファイル");
        edit = new JMenu("編集");
        open = new JMenuItem("開く");
        save = new JMenuItem("名前を付けて保存");
        quit = new JMenuItem("終了");
        check = new JMenuItem("文字列抽出");

        // リスナ登録
        open.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                openDocument();
        }});
        save.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                saveDocument();
        }});
        quit.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                quit();
        }});
        check.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                check();
        }});

        //メニュー構築
        file.add(open);
        file.add(save);
        file.add(new JSeparator());
        file.add(quit);
        edit.add(check);

        //メニューバーにメニューを追加。
        menuBar.add(file);
        menuBar.add(edit);

        //メニューバーをコンテナに追加。
        setJMenuBar(menuBar);

        // スクロールバー設定
        spanel_left.setVerticalScrollBarPolicy(
            JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        spanel_right.setVerticalScrollBarPolicy(
            JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

        // レイアウト
        this.getContentPane().setLayout(new GridLayout(1, 2));

        // パネル設定
        this.getContentPane().add(spanel_left);
        this.getContentPane().add(spanel_right);
    }

    //ファイルを開く
    public void openDocument(){
        try{
            text.setText(Dialog.open(this));
        }catch(IOException e){
            e.printStackTrace();
        }
    }

    //ファイルの保存
    public void saveDocument(){
        try{
            Dialog.save(this, result.getText());
        }catch(IOException e){
            e.printStackTrace();
        }
    }

    //プログラムの終了
    public void quit(){
        int result = JOptionPane.showConfirmDialog(
                     getContentPane(), "終了しますか?");
        if(result == 0){
            System.exit(0);
        }
    }

    //データ抽出
    public void check(){
        // 未実装
    }
}

Dialogクラス(ダイアログの管理)
package regularexpressioneditor.gui;

import java.awt.Component;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JFileChooser;

/**
 * ダイアログ管理クラス
 * ダイアログを使用する処理を実装する
 */
public class Dialog {

    // ファイル選択ダイアログ
    private static final JFileChooser chooser = new JFileChooser();

    /**
     * ファイル保存
     * @param parent 親コンポーネント。指定しない場合はnull
     * @param text 保存する文字列
     * @throws IOException
     */
    public static void save(Object parent, String text) throws IOException{
        File file = null;
        FileWriter fw = null;
        BufferedWriter br = null;

        try{
            if((chooser.showSaveDialog((Component)parent))
                              == JFileChooser.APPROVE_OPTION){
                file = chooser.getSelectedFile();
                fw = new FileWriter(file);
                br = new BufferedWriter(fw);
                br.write(text);
                br.close();
            }
        }catch(IOException e){
            throw e;

        }finally{
            try{
                br.close();
            }catch(Exception e){
            }
        }
    }

    /**
     * ファイルを開く
     * ダイアログで選択されたファイルを読み込む
     * @param parent 親コンポーネント。指定しない場合はnull
     * @return 読み込んだ文字列
     * @throws IOException
     */
    public static String open(Object parent) throws IOException{
        File file = null;
        FileReader fr = null;
        BufferedReader br = null;

        try{
            if(chooser.showOpenDialog((Component)parent)
                            == JFileChooser.APPROVE_OPTION){
                StringBuffer buffer = new StringBuffer();
                file = chooser.getSelectedFile();
                fr = new FileReader(file);
                br = new BufferedReader(fr);
                while(br.ready()){
                    buffer.append(br.readLine());
                }
                return buffer.toString();
            }else{
                return "";
            }

        }catch(IOException e){
            throw e;

        }finally{
            try{
                br.close();
            }catch(Exception e){
            }
        }
    }
}

RegularExpressionEditorクラス(メイン)
package regularexpressioneditor;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.UIManager;

import regularexpressioneditor.gui.MainFrame;

public class RegularExpressionEditor {
    public static void main(String args[]){

        JFrame.setDefaultLookAndFeelDecorated(true);
        JDialog.setDefaultLookAndFeelDecorated(true);

        try{
            javax.swing.plaf.metal.
            MetalLookAndFeel.setCurrentTheme(
            new javax.swing.plaf.metal.DefaultMetalTheme());

            UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
        }catch(Exception e){
            e.printStackTrace();
            System.exit(-1);
        }

        MainFrame mf = new MainFrame();
        mf.setTitle("RegularExpressionEditor");
        mf.setSize(400, 300);
        mf.show();
    }
}

動作イメージ

図1:起動後画面
図1:起動後画面

図2:ファイル読み込み後&メニュー
図2:ファイル読み込み後&メニュー

実装したもの
  • テキストファイルの保存
  • テキストファイルの読み込み
  • アプリケーションの終了
「文字列抽出」メニュークリック時にcheckメソッドが実行されます(処理は未実装)。次回はURL抽出処理をここに実装する。

Advertisement

ショートカット

634
634ブログ
このカテゴリのトップページに戻る
Incubator(Pukiwiki)
634ラボ
   UIコレクションギャラリー
   ZO-3ジェネレーター

サイト検索


Y!ログール