Selasa, 12 April 2011

Program membuat file dengan bahasa JAVA


berikut code Membuat file dengan bahasa JAVA


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

class BuatFile extends JFrame implements ActionListener
{
                private JTextArea output;
                private JButton btnHasil, btnTutup;
   
    public BuatFile(){
                                JPanel panel_1=new JPanel();
                                panel_1.setLayout(new FlowLayout());
        panel_1.add(btnHasil=new JButton("Simpan"));
        panel_1.add(btnTutup=new JButton("Tutup"));
                                JScrollPane scroll = new JScrollPane(output=new JTextArea());
        output.setFont(new Font("Arial",0,14));
                                output.setLineWrap(true);
                                output.setWrapStyleWord(true);
                                scroll.setPreferredSize(new Dimension(300, 100));
                                JPanel panel_2=new JPanel();
                                panel_2.setLayout(new BorderLayout());
                                panel_2.add(scroll);

                                setLayout(new BorderLayout());
                                add(panel_2,"Center");
                                add(panel_1,"South");
                   
                                btnHasil.addActionListener(this);
        btnTutup.addActionListener(this);                      
                }

                public void actionPerformed(ActionEvent e){
    
                   if(e.getSource()==btnHasil){
              String hasil=output.getText();
              DataOutputStream simpanFile=null;

                                   try{
                                     simpanFile=new DataOutputStream(new FileOutputStream("TYO.txt"));        
                                     simpanFile.writeBytes(hasil);
             simpanFile.close();
                                   }catch(IOException ex){
                                                   JOptionPane.showMessageDialog(null,"Error--"+ex.toString(),"Warning",
                                                                   JOptionPane.ERROR_MESSAGE);
                                   }

                 JOptionPane.showMessageDialog(null,"Data berhasil disimpan pada file 'TYO.txt' ","Simpan",
                                                 JOptionPane.INFORMATION_MESSAGE);
                   }
      
                   if(e.getSource()==btnTutup){
                                   JOptionPane.showMessageDialog(null,"--- Terima kasih ---\n     by : Raditya Pratama Putra",
                                                   "Thank you", JOptionPane.INFORMATION_MESSAGE);
       System.exit(0);
                   }
                }

    public static void main(String[] args){       
        BuatFile frame = new BuatFile();
                                frame.pack();
                                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                                frame.setTitle("Notepad Sederhana by Raditya Pratama Putra");
                                frame.setVisible(true);
                               
    }
}

Tidak ada komentar:

Posting Komentar