CX's メモ未満

[Latest] [ToDo] [News] [Idea] [Memo] [Hatena] [Schedule] [Edit] [Config] [Help] | [Group] [Home] [PV:70479]
0000 | 00 | 01 |
2004 | 05 | 06 | 07 | 08 | 10 |
2005 | 08 |
2010 | 01 |

2010-01-25いろんな言語でメモ帳を作ってみる。

[][] Java でメモ帳  Java でメモ帳 - CX's メモ未満 を含むブックマーク はてなブックマーク -  Java でメモ帳 - CX's メモ未満

/**
 File : Notepad.java
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;

public class Notepad extends JFrame implements ActionListener {
    public static void main( String args[] ) {
        SwingUtilities.invokeLater( new Runnable() {
            public void run() {
                Notepad app = new Notepad( "Notepad" );
                app.setVisible( true );
            }
        } );
    }

    JMenuBar menuBar;
    JMenu menu1;
    JMenuItem menuItem1;
    JMenuItem menuItem2;
    JMenuItem menuItem3;
    JTextArea text1;
    JFileChooser fileChooser;

    Notepad( String title ) {
        super( title );
        initComponent();
    }

    public void initComponent() {
        initMenu();
        initBody();
        initLayout();
    }

    public void initMenu() {
        menuBar = new JMenuBar();
        menu1 = new JMenu( "File" );
        menu1.setMnemonic( 'F' );
        menuItem1 = new JMenuItem( "Open" );
        menuItem1.setMnemonic( 'O' );
        menuItem1.addActionListener( this );
        menuItem2 = new JMenuItem( "Save" );
        menuItem2.setMnemonic( 'S' );
        menuItem2.addActionListener( this );
        menuItem3 = new JMenuItem( "Close(X)" );
        menuItem3.setMnemonic( 'X' );
        menuItem3.addActionListener( this );
        
        fileChooser = new JFileChooser();
    }

    public void initBody() {
        text1 = new JTextArea( 20, 80 );
    }

    public void initLayout() {
        menu1.add( menuItem1 );
        menuBar.add( menu1 );
        setJMenuBar( menuBar );

        add( text1 );

        setSize( 640, 480 );
        setLocationRelativeTo( null );
        setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    }

    public void actionPerformed( ActionEvent e ) {
        if ( e.getSource() == menuItem1 ) {
            openFile();
        } else if ( e.getSource() == menuItem2 ) {
            saveFile();
        } else if ( e.getSource() == menuItem3 ) {
            System.exit( 0 );
        }
    }

    public void openFile() {
        try {
            int nResult = fileChooser.showOpenDialog( this );
            if ( nResult == JFileChooser.APPROVE_OPTION ) {
                File file = fileChooser.getSelectedFile();
                FileReader fileReader = new FileReader( file.getAbsolutePath() );
                BufferedReader bufferedReader = new BufferedReader( fileReader );
                text1.setText( bufferedReader.readLine() );
                String strLine;
                while ( ( strLine = bufferedReader.readLine() ) != null ) {
                    text1.append( "\n" + strLine );
                }
            }
        } catch ( Exception e ) {
            e.printStackTrace();
        }
    }

    public void saveFile() {
        try {
            int nResult = fileChooser.showSaveDialog( this );
            if ( nResult == JFileChooser.APPROVE_OPTION ) {
                File file = fileChooser.getSelectedFile();
                FileWriter fileWriter = new FileWriter( file.getAbsolutePath() );
                PrintWriter printWriter = new PrintWriter( fileWriter );
                printWriter.write( text1.getText() );
                printWriter.close();
            }
        } catch ( Exception e ) {
            e.printStackTrace();
        }
    }
}
トラックバック - http://cx-memo.g.hatena.ne.jp/cx20/20100125