1. buat project baru dan beri nama sesuai dengan keinginan hati
2. pilih file JFrame Form
3. kita buat tampilan seperti gambar di atas
4. ganti nama pada masing-masing object, misalkan jika nama objek sebelum dirubah "button" maka ganti nama tersebut misal btnsave , digunakan unutk mempermudah pembacaan
5. lalu masukkan coding yang ada
dibawah ini merupakan source-code :
import javax.swing.*;
import java.io.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.filechooser.FileFilter;
public class NewJFrame extends javax.swing.JFrame {
public NewJFrame() {
initComponents();
//Saat di awal aplikasi
//Judul = Untitled
this.setTitle("Untitled - NOTES");
//Atur Fillter jenis file dengan Handle fc
fc = new JFileChooser();
fc.addChoosableFileFilter(new jenisFile(EXT));
fc.addChoosableFileFilter(new jenisFile(EXT2));
//Filter Bawaan (All Files) di disable-kan
fc.setAcceptAllFileFilterUsed(false);
//variabel fileBaru diinisialisai dengan TRUE
fileBaru = true;
}
JFileChooser fc;
File file;
boolean fileBaru;
final String[] EXT = {".csv"};
final String[] EXT2 = {".txt", ".java"};
public String text, namafiletext;
private void msg(String pesan) {
JOptionPane.showMessageDialog(this, pesan, "Informasi", JOptionPane.INFORMATION_MESSAGE);
}
private boolean okToReplace() {
return (JOptionPane.showConfirmDialog(this, "File " + file.getName() + " Sudah Ada\n" + "Akan Ditimpa ?",
"Peringatan", JOptionPane.YES_NO_OPTION) == 0);
}
class jenisFile extends FileFilter {
private String[] s;
jenisFile(String[] sArg) {
s = sArg;
}
// Tentukan file apa yang akan ditampilkan
public boolean accept(File fArg) {
if (fArg.isDirectory()) {
return true;
}
// Jika Extension dari file sesuai syarat, tampilkan
for (int i = 0; i < s.length; ++i) {
if (fArg.getName().toLowerCase().indexOf(s[i].toLowerCase()) > 0) {
return true;
}
}
// selain kedua hal di atas, anggap false
return false;
}
public String getDescription() {
String tmp = "";
for (int i = 0; i < s.length; ++i) {
tmp += "*" + s[i] + " ";
}
return tmp;
}
}
private void SimpanFile() {
PrintWriter pw = null;
try {
pw = new PrintWriter(new BufferedWriter(new FileWriter(file)));
} catch (IOException e) {
msg("Gagal Simpan File : '" + file.getName());
return;
}
pw.print(isi1.getText());
pw.close();
this.setTitle(file.getName() + " - NOTES");
fileBaru = false;
}
class Enskripsimd5 {
private String convertToHex(byte[] data) {
StringBuffer buf = new StringBuffer();
for (int i = 0; i < data.length; i++) {
int halfbyte = (data[i] >>> 4) & 0x0F;
int two_halfs = 0;
do {
if ((0 <= halfbyte) && (halfbyte <= 9)) {
buf.append((char) ('0' + halfbyte));
} else {
buf.append((char) ('a' + (halfbyte - (int) geser.getValue())));
}
halfbyte = data[i] & 0x0F;
} while (two_halfs++ < 1);
}
return buf.toString();
}
public String MD5(String text)
throws NoSuchAlgorithmException, UnsupportedEncodingException {
MessageDigest md;
md = MessageDigest.getInstance("MD5");
byte[] md5hash = new byte[32];
md.update(text.getBytes("iso-8859-1"), 0, text.length());
md5hash = md.digest();
return convertToHex(md5hash);
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
private String readFile(File file) {
String result = "";
try {
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);
while (dis.available() != 0) {
result += dis.readLine() + "\n";
}
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
return result;
}
/** 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")
//
private void initComponents() {
buka = new javax.swing.JButton();
simpan = new javax.swing.JButton();
simpan_sbg = new javax.swing.JButton();
keluar = new javax.swing.JButton();
baru = new javax.swing.JButton();
enkripsi = new javax.swing.JButton();
md5_1 = new javax.swing.JButton();
random1 = new javax.swing.JButton();
lblnamafile = new javax.swing.JLabel();
lblisifile = new javax.swing.JLabel();
nama_file = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
md5_2 = new javax.swing.JButton();
jLabel4 = new javax.swing.JLabel();
save = new javax.swing.JButton();
saveas2 = new javax.swing.JButton();
random2 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
isi2 = new javax.swing.JTextArea();
jScrollPane2 = new javax.swing.JScrollPane();
isi1 = new javax.swing.JTextArea();
geser = new javax.swing.JSpinner();
password1 = new javax.swing.JButton();
password2 = new javax.swing.JButton();
pass1 = new javax.swing.JTextField();
pass2 = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setBackground(javax.swing.UIManager.getDefaults().getColor("EditorPane.inactiveForeground"));
buka.setIcon(new javax.swing.ImageIcon("D:\\CD BUKU\\CD BONUS\\Program Demo Project\\icon1\\47.png")); // NOI18N
buka.setText("buka file");
buka.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bukaActionPerformed(evt);
}
});
simpan.setIcon(new javax.swing.ImageIcon("D:\\CD BUKU\\CD BONUS\\Program Demo Project\\icon2\\45.png")); // NOI18N
simpan.setText("simpan file");
simpan.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
simpanActionPerformed(evt);
}
});
simpan_sbg.setIcon(new javax.swing.ImageIcon("D:\\CD BUKU\\CD BONUS\\Program Demo Project\\icon2\\46.png")); // NOI18N
simpan_sbg.setText("simpan sebagai");
simpan_sbg.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
simpan_sbgActionPerformed(evt);
}
});
keluar.setText("keluar");
keluar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
keluarActionPerformed(evt);
}
});
baru.setIcon(new javax.swing.ImageIcon("D:\\CD BUKU\\CD BONUS\\Program Demo Project\\icon1\\21.png")); // NOI18N
baru.setText("new file");
baru.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
baruActionPerformed(evt);
}
});
enkripsi.setText("lakukan enkripsi");
md5_1.setText("MD5");
md5_1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
md5_1ActionPerformed(evt);
}
});
random1.setText("random");
random1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
random1ActionPerformed(evt);
}
});
lblnamafile.setText("nama file");
lblisifile.setText("Isi file asli");
jLabel3.setText("geser");
md5_2.setText("MD5");
jLabel4.setText("isi file telah di enkripsi");
save.setText("save");
save.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
saveActionPerformed(evt);
}
});
saveas2.setText("save as");
saveas2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
saveas2ActionPerformed(evt);
}
});
random2.setText("random");
random2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
random2ActionPerformed(evt);
}
});
isi2.setColumns(20);
isi2.setRows(5);
jScrollPane1.setViewportView(isi2);
isi1.setColumns(20);
isi1.setRows(5);
jScrollPane2.setViewportView(isi1);
password1.setText("password");
password1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
password1ActionPerformed(evt);
}
});
password2.setText("password");
password2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
password2ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(buka)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(simpan)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(simpan_sbg)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 111, Short.MAX_VALUE)
.addComponent(keluar)
.addGap(18, 18, 18))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(baru)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(enkripsi))
.addGroup(layout.createSequentialGroup()
.addComponent(lblnamafile)
.addGap(18, 18, 18)
.addComponent(nama_file, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(lblisifile)
.addGroup(layout.createSequentialGroup()
.addComponent(md5_1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(random1)))
.addGap(17, 17, 17)
.addComponent(jLabel3))
.addGroup(layout.createSequentialGroup()
.addGap(10, 10, 10)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 282, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(35, 35, 35)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(geser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addGap(10, 10, 10)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel4)
.addComponent(md5_2, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(save, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(random2, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(saveas2, javax.swing.GroupLayout.Alignment.LEADING)))
.addGroup(layout.createSequentialGroup()
.addComponent(password2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(pass2, javax.swing.GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE))
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 190, Short.MAX_VALUE))))))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(password1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(pass1, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(simpan)
.addComponent(simpan_sbg)
.addComponent(buka)
.addComponent(keluar))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(baru)
.addComponent(enkripsi)
.addComponent(jLabel3)
.addComponent(geser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lblnamafile)
.addComponent(nama_file, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(lblisifile)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(md5_1)
.addComponent(random1)))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(41, 41, 41)
.addComponent(md5_2, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel4))
.addGroup(layout.createSequentialGroup()
.addGap(5, 5, 5)
.addComponent(save)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(saveas2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(random2))))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addGap(45, 45, 45)
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 159, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(password1)
.addComponent(pass1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(password2)
.addComponent(pass2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap())
);
pack();
}//
private void bukaActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
JFileChooser fc = new JFileChooser();
JFrame jf = new JFrame();
int ret = fc.showOpenDialog(jf.add(fc));
if (ret == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
text = readFile(file);
isi1.setText(text);
namafiletext = file.getPath().toString();
nama_file.setText(namafiletext);
}
}
private void simpanActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (fileBaru) {
simpan.doClick();
} else {
SimpanFile();
}
}
private void simpan_sbgActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (fc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
file = fc.getSelectedFile();
if (!file.exists() || okToReplace()) {
SimpanFile();
}
}
}
private void baruActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
isi1.setText("");
isi1.requestFocus();
this.setTitle("Untitled - NOTES");
fileBaru = true;
}
private void keluarActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
System.exit(0);
}
private void md5_1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
save.setEnabled(true);
password2.setEnabled(true);
pass2.setEnabled(true);
isi2.setEnabled(true);
saveas2.setEnabled(true);
md5_2.setEnabled(true);
random2.setEnabled(true);
Enskripsimd5 md5 = new Enskripsimd5();
try {
isi2.setText(md5.MD5(isi1.getText()));
} catch (NoSuchAlgorithmException ex) {
Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null);
}
}
private void random1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
save.setEnabled(true);
password1.setEnabled(true);
pass2.setEnabled(true);
isi2.setEnabled(true);
saveas2.setEnabled(true);
md5_2.setEnabled(true);
random2.setEnabled(true);
String randomKey = "";
double randomNumber;
for (int i = 0; i < isi1.getText().length(); i++) {
randomNumber = Math.random() * 25 + 65;
randomKey = randomKey + (char) randomNumber;
}
isi2.setText(randomKey);
}
private void saveActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (fileBaru) {
saveas2.doClick();
} else {
SimpanFile();
}
}
private void saveas2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (fc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
file = fc.getSelectedFile();
if (!file.exists() || okToReplace()) {
SimpanFile();
}
}
}
private void random2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String randomKey = "";
double randomNumber;
for (int i = 0; i < isi2.getText().length(); i++) {
randomNumber = Math.random() * 25 + 65;
randomKey = randomKey + (char) randomNumber;
}
isi1.setText(randomKey);
}
private void password1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (pass2.getText().equals(pass1.getText())) {
msg("password anda benar : " + pass1.getText());
isi1.setText(isi2.getText());
} else {
msg("password anda salah : " + pass1.getText());
}
}
private void password2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (pass1.getText().equals(pass2.getText())) {
msg("password anda benar : " + pass2.getText());
isi2.setText(isi1.getText());
} else {
msg("password anda salah : " + pass2.getText());
}
}
/**
* @param args the command line arguments
*/
// Variables declaration - do not modify
private javax.swing.JButton baru;
private javax.swing.JButton buka;
private javax.swing.JButton enkripsi;
private javax.swing.JSpinner geser;
private javax.swing.JTextArea isi1;
private javax.swing.JTextArea isi2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JButton keluar;
private javax.swing.JLabel lblisifile;
private javax.swing.JLabel lblnamafile;
private javax.swing.JButton md5_1;
private javax.swing.JButton md5_2;
private javax.swing.JTextField nama_file;
private javax.swing.JTextField pass1;
private javax.swing.JTextField pass2;
private javax.swing.JButton password1;
private javax.swing.JButton password2;
private javax.swing.JButton random1;
private javax.swing.JButton random2;
private javax.swing.JButton save;
private javax.swing.JButton saveas2;
private javax.swing.JButton simpan;
private javax.swing.JButton simpan_sbg;
// End of variables declaration
}
0 komentar: