Asalamualaikum wr wb..
Pada posting kali ini saya akan menjelaskan mengenai pembuatan aplikasi notepad yang terdiri dari menu dimana di dlam nya berisi fasilitas menambah(new), open file, save dan close. Hasil dari program dalam sebagi berikut :
|
Tampilan Menu |
|
Tampilan ketika di close namun masi terjadi kegiatan pada test area |
|
|
Save file |
========================================================================
PEMBUATAN
1. Pertama yaitu membuat GUI nya terlebih dahulu seperti berikut :
Kemudian source codenya adalah sebagi berikut :
package notepad_diana;
import java.io.File;
import javax.swing.JOptionPane;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import javax.swing.JFileChooser;
/**
*
* @author diana
*/
public class HomeNotepad extends javax.swing.JFrame {
private File file;
/** Creates new form HomeNotepad */
public HomeNotepad() {
initComponents();
setTitle("Notepad dmr");
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
TextArea = new javax.swing.JTextArea();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
MenuNew = new javax.swing.JMenuItem();
MenuOpen = new javax.swing.JMenuItem();
MenuSave = new javax.swing.JMenuItem();
MenuSaveAs = new javax.swing.JMenuItem();
MenuClose = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
MenuAbout = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Notepad dmr");
TextArea.setColumns(20);
TextArea.setFont(new java.awt.Font("Ribbontic", 0, 13));
TextArea.setRows(5);
jScrollPane1.setViewportView(TextArea);
jMenuBar1.setBackground(new java.awt.Color(255, 204, 204));
jMenu1.setBorder(null);
jMenu1.setText("Menu");
jMenu1.setFont(new java.awt.Font("Ribbontic", 1, 12));
MenuNew.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
MenuNew.setFont(new java.awt.Font("Ribbontic", 1, 12));
MenuNew.setText("New");
MenuNew.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
MenuNewActionPerformed(evt);
}
});
jMenu1.add(MenuNew);
MenuOpen.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
MenuOpen.setFont(new java.awt.Font("Ribbontic", 1, 12));
MenuOpen.setText("Open File");
MenuOpen.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
MenuOpenActionPerformed(evt);
}
});
jMenu1.add(MenuOpen);
MenuSave.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
MenuSave.setFont(new java.awt.Font("Ribbontic", 1, 12));
MenuSave.setText("Save");
MenuSave.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
MenuSaveActionPerformed(evt);
}
});
jMenu1.add(MenuSave);
MenuSaveAs.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.ALT_MASK | java.awt.event.InputEvent.CTRL_MASK));
MenuSaveAs.setFont(new java.awt.Font("Ribbontic", 1, 12));
MenuSaveAs.setText("Save as...");
MenuSaveAs.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
MenuSaveAsActionPerformed(evt);
}
});
jMenu1.add(MenuSaveAs);
MenuClose.setFont(new java.awt.Font("Ribbontic", 1, 12));
MenuClose.setText("Close");
MenuClose.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
MenuCloseActionPerformed(evt);
}
});
jMenu1.add(MenuClose);
jMenuBar1.add(jMenu1);
jMenu2.setBorder(null);
jMenu2.setText("Other");
jMenu2.setFont(new java.awt.Font("Ribbontic", 1, 12));
jMenu2.setHideActionText(true);
MenuAbout.setFont(new java.awt.Font("Ribbontic", 1, 12));
MenuAbout.setText("About");
MenuAbout.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
MenuAboutActionPerformed(evt);
}
});
jMenu2.add(MenuAbout);
jMenuBar1.add(jMenu2);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
private void MenuNewActionPerformed(java.awt.event.ActionEvent evt) {
New();
}
private void MenuSaveActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if(file==null)
{
save_as();
}
else
{
save();
}
}
private void MenuSaveAsActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
save_as();
}
private void MenuCloseActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
close();
}
private void MenuOpenActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Open();
}
private void MenuAboutActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
About abt = new About();
abt.show();
}
private void formWindowClosing(java.awt.event.WindowEvent evt) {
close();
}
/**
* @param args the command line arguments
*/
private void New(){
if(TextArea.getText().equals(""))
{
TextArea.setText("");
setTitle("Notepad dmr");
}
else{
int choice1;
if(file!=null){
choice1=JOptionPane.showConfirmDialog(this,"Do you want to save changes to "+file.getName(),"Notetext",JOptionPane.YES_NO_CANCEL_OPTION);
if(choice1==JOptionPane.YES_OPTION)
{
save();
TextArea.setText("");
setTitle("Notepad dmr");
}
if(choice1==JOptionPane.NO_OPTION)
{
TextArea.setText("");
setTitle("Notepad dmr");
}
else{}
}
else{
choice1=JOptionPane.showConfirmDialog(this,"Do you want to save changes?","Notetext",JOptionPane.YES_NO_CANCEL_OPTION);
if(choice1==JOptionPane.YES_OPTION)
{
save();
TextArea.setText("");
setTitle("Notepad dmr");
}
if(choice1==JOptionPane.NO_OPTION)
{
TextArea.setText("");
setTitle("Notepad dmr");
}
else{}
}
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new HomeNotepad().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JMenuItem MenuAbout;
private javax.swing.JMenuItem MenuClose;
private javax.swing.JMenuItem MenuNew;
private javax.swing.JMenuItem MenuOpen;
private javax.swing.JMenuItem MenuSave;
private javax.swing.JMenuItem MenuSaveAs;
private javax.swing.JTextArea TextArea;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JScrollPane jScrollPane1;
// End of variables declaration
private void save_as() {
JFileChooser fileChooser=new JFileChooser(".");
int choice=fileChooser.showSaveDialog(this);
if(choice==JFileChooser.APPROVE_OPTION)
{
file=new File(fileChooser.getSelectedFile().getPath()+".txt");
setTitle(file.getPath());
save();
}
}
private void save() {
if(file!=null)
{
FileWriter fw = null;
try {
fw = new FileWriter(file);
fw.write(TextArea.getText());
fw.flush();
} catch (IOException ex) {
JOptionPane.showMessageDialog(this,"An error occured! Cannot Save");
} finally {
try {
fw.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
else
{
save_as();
}
}
private void close() {
if(TextArea.getText().equals(""))
{
System.exit(0);
}
else{
int choice1;
if(file!=null){
choice1=JOptionPane.showConfirmDialog(this,"Do you want to save changes to "+file.getName(),"Notetext",JOptionPane.YES_NO_CANCEL_OPTION);
if(choice1==JOptionPane.YES_OPTION)
{
save();
System.exit(0);
}
if(choice1==JOptionPane.NO_OPTION)
{
System.exit(0);
}
else{}
}
else{
choice1=JOptionPane.showConfirmDialog(this,"Do you want to save changes?","Notetext",JOptionPane.YES_NO_CANCEL_OPTION);
if(choice1==JOptionPane.YES_OPTION)
{
save();
System.exit(0);
}
if(choice1==JOptionPane.NO_OPTION)
{
System.exit(0);
}
else{}
}
}
}
private void Open() {
JFileChooser fileChooser=new JFileChooser(".");
int choice=fileChooser.showOpenDialog(this);
if(choice==JFileChooser.APPROVE_OPTION)
{
FileReader fr = null;
try {
file = fileChooser.getSelectedFile();
setTitle(file.getPath());
fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
TextArea.setText("");
while (br.ready()) {
TextArea.append(br.readLine() + "\n");
}
}catch (FileNotFoundException ex) {
JOptionPane.showMessageDialog(this,"Cannot find the file");
}
catch (IOException ex) {
JOptionPane.showMessageDialog(this,"Cannot read the file data");
}
}
}
}
2. Kemudian pembuatan frame untuk about
========================================================================
Video tutorialnya :
http://www.youtube.com/watch?v=Dz59yI2lO6k&feature=youtu.be
Demiakian Posting kali ini, semoga bermanfaat yaaa :)