Created
May 22, 2012 08:45
-
-
Save resarahadian/2767662 to your computer and use it in GitHub Desktop.
Aplikasi Notepad dengan Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* @author Resa C.R | |
*/ | |
import java.awt.*; | |
import javax.swing.*; | |
import java.awt.event.*; | |
import java.io.*; | |
import java.util.Scanner; | |
public class Notepad extends JFrame | |
{ | |
Container konten = getContentPane(); | |
private JMenuBar menuBaris = new JMenuBar(); | |
private JMenu menuFile = new JMenu("Berkas"); | |
private JMenu menuTentang = new JMenu("Tentang"); | |
private JMenuItem miAplikasi = new JMenuItem("Aplikasi"); | |
private JMenuItem miSimpan = new JMenuItem("Simpan"); | |
private JMenuItem miBuka = new JMenuItem("Buka"); | |
private JMenuItem miKeluar = new JMenuItem("Keluar"); | |
private JTextArea textArea = new JTextArea("",0,0); | |
private JFileChooser fc = new JFileChooser(); | |
//Konstuktor | |
public Notepad() | |
{ | |
setTitle("Aplikasi Notepad"); | |
setSize(700,600); | |
setVisible(true); | |
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
setLocationRelativeTo(null); | |
setJMenuBar(menuBaris); | |
menuBaris.add(menuFile); | |
menuFile.add(miSimpan); | |
miSimpan.setMnemonic(KeyEvent.VK_S); //Membuat nama shortcut | |
miSimpan.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,ActionEvent.CTRL_MASK)); //Membuat shorcut Keyboard | |
menuFile.add(miBuka); | |
menuFile.addSeparator(); //Menambahkan garis separator | |
miBuka.setMnemonic(KeyEvent.VK_B); | |
miBuka.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_B,ActionEvent.CTRL_MASK)); | |
menuFile.add(miKeluar); | |
miKeluar.setMnemonic(KeyEvent.VK_K); | |
miKeluar.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_K,ActionEvent.CTRL_MASK)); | |
menuBaris.add(menuTentang); | |
menuTentang.add(miAplikasi); | |
miAplikasi.setMnemonic(KeyEvent.VK_A); | |
miAplikasi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,ActionEvent.CTRL_MASK)); | |
konten.add(textArea); | |
textArea.setFont(new Font("Arial",Font.BOLD,14)); //Mengatur Fonts | |
textArea.setLineWrap(true); | |
miBuka.addActionListener(new ActionListener() | |
{ | |
public void actionPerformed(ActionEvent act) | |
{ | |
int buka = fc.showOpenDialog(textArea); | |
if(buka == JFileChooser.APPROVE_OPTION) | |
{ | |
textArea.setText(""); | |
try | |
{ | |
Scanner scan = new Scanner(new FileReader(fc.getSelectedFile().getPath())); //Membuat scanner membaca file yang dibuka | |
while(scan.hasNext()) | |
{ | |
textArea.append(scan.nextLine()); | |
} | |
} | |
catch(Exception ex) | |
{ | |
System.out.println(ex); | |
} | |
} | |
} | |
}); | |
miSimpan.addActionListener(new ActionListener() | |
{ | |
public void actionPerformed(ActionEvent act) | |
{ | |
int simpan = fc.showOpenDialog(textArea); | |
if(simpan == JFileChooser.APPROVE_OPTION) | |
{ | |
try | |
{ | |
BufferedWriter bw = new BufferedWriter(new FileWriter(fc.getSelectedFile().getPath())); //Menulis File yang kita buka di TextArea | |
bw.write(textArea.getText()); | |
bw.close(); | |
} | |
catch(Exception ex) | |
{ | |
System.out.println(ex); | |
} | |
} | |
} | |
}); | |
miKeluar.addActionListener(new ActionListener() | |
{ | |
public void actionPerformed(ActionEvent act) | |
{ | |
dispose(); //Keluar dari aplikasi | |
} | |
}); | |
miAplikasi.addActionListener(new ActionListener() | |
{ | |
public void actionPerformed(ActionEvent act) | |
{ | |
JOptionPane.showMessageDialog(null,"Aplikasi Notepad","Pesan",JOptionPane.INFORMATION_MESSAGE); | |
} | |
}); | |
}//Akhir Konstruktor | |
public static void main(String[] ar) | |
{ | |
new Notepad(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment