Sunday, 29 November 2009

Aplikasi Aritmatika Dengan Java

Berikut Ini Adalah Aplikasi Yang Pernah Saya Buat Waktu Awal Belajar Pemrograman Java.

Hasilnya Aplikasinya Seperti Dibawah Ini :


















Source Code Lengkapnya :

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class utama implements ActionListener{
JFrame frame;
JLabel label1,label2;
JTextField tbil1,tbil2;
JButton bkali,bbagi,bkurang,btambah;
int bil1,bil2,hasil;
String op;
public utama()
{
label1 = new JLabel("Masukan Bil.Bulat 1 :");
label1.setLocation(10, 10);
label1.setSize(label1.getPreferredSize());

label2 = new JLabel("Masukan Bil.Bulat 2 : ");
label2.setLocation(10, 40);
label2.setSize(label2.getPreferredSize());

tbil1 = new JTextField(14);
tbil1.setLocation(140, 10);
tbil1.setSize(tbil1.getPreferredSize());

tbil2 = new JTextField(14);
tbil2.setLocation(140, 40);
tbil2.setSize(tbil2.getPreferredSize());

bkali = new JButton("Kali");
bkali.setLocation(10, 70);
bkali.setSize(bkali.getPreferredSize());
bkali.addActionListener(this);

bbagi = new JButton("Bagi");
bbagi.setLocation(70, 70);
bbagi.setSize(bbagi.getPreferredSize());
bbagi.addActionListener(this);

bkurang = new JButton("Kurang");
bkurang.setLocation(135, 70);
bkurang.setSize(bkurang.getPreferredSize());
bkurang.addActionListener(this);

btambah = new JButton("Tambah");
btambah.setLocation(215, 70);
btambah.setSize(btambah.getPreferredSize());
btambah.addActionListener(this);

}
public void tampil()
{
frame = new JFrame("Contoh Aplikasi");
frame.setLayout(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(label1);
frame.getContentPane().add(label2);
frame.getContentPane().add(tbil1);
frame.getContentPane().add(tbil2);
frame.getContentPane().add(bkali);
frame.getContentPane().add(bbagi);
frame.getContentPane().add(bkurang);
frame.getContentPane().add(btambah);
frame.setSize(315, 140);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setResizable(false);
}
public static void main(String[] args)
{
utama jalan = new utama();
jalan.tampil();
}

public void actionPerformed(ActionEvent e) {
try
{
bil1 = Integer.parseInt(tbil1.getText());
bil2 = Integer.parseInt(tbil2.getText());
if(e.getSource()==bkali)
{
hasil = bil1*bil2;
op = "x";
}
else if(e.getSource()==bbagi)
{
hasil = bil1/bil2;
op = ":";
}
else if(e.getSource()==bkurang)
{
hasil = bil1-bil2;
op = "-";
}
else if(e.getSource()==btambah)
{
hasil = bil1+bil2;
op = "+";
}
JOptionPane.showMessageDialog(frame, bil1+" "+op+" "+bil2+" = "+hasil, "Informasi", JOptionPane.INFORMATION_MESSAGE);
}catch(Exception evt)
{
JOptionPane.showMessageDialog(frame, evt.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
}

Tutorial Jtable

Berikut Ini Saya Share Sedikit Ilmu Yang Saya Pelajari Dari Om Google, Cukup Sulit Awalnya Tapi Kalau Sudah Terbiasa Pasti Jadi Muda. Dimana Tutorial Jtable Tersebut Meliputi Penambahan Baris, Hapus Baris Dan Isi Data Pada Baris Perbaris. Untuk Lebih Jelasnya Lihat Gambar Di Bawah Ini :



Berikut Source Code Lengkapnya :

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
*
* @author Mr.Bayu
*/
public class utama extends javax.swing.JFrame {
DefaultTableModel model;
Object data[][]=null;
int i;
public utama() {
initComponents();
model = (DefaultTableModel) tabel.getModel();
}
@SuppressWarnings("unchecked")
//
private void initComponents() {

jPanel1 = new javax.swing.JPanel();
tno_hp = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
tnama = new javax.swing.JTextField();
talamat = new javax.swing.JTextField();
jScrollPane1 = new javax.swing.JScrollPane();
tabel = new javax.swing.JTable();
btambah = new javax.swing.JButton();
bhapus = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Demo Jtable");
setLocationByPlatform(true);
setResizable(false);
getContentPane().setLayout(null);

jPanel1.setBackground(new java.awt.Color(0, 255, 0));
jPanel1.setLayout(null);
jPanel1.add(tno_hp);
tno_hp.setBounds(110, 80, 230, 20);

jLabel1.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
jLabel1.setText("No HP");
jPanel1.add(jLabel1);
jLabel1.setBounds(10, 80, 90, 17);

jLabel2.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
jLabel2.setText("Nama");
jPanel1.add(jLabel2);
jLabel2.setBounds(10, 20, 90, 17);

jLabel3.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
jLabel3.setText("Alamat");
jPanel1.add(jLabel3);
jLabel3.setBounds(10, 50, 90, 17);
jPanel1.add(tnama);
tnama.setBounds(110, 20, 230, 20);
jPanel1.add(talamat);
talamat.setBounds(110, 50, 230, 20);

tabel.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null}
},
new String [] {
"Nama", "Alamat", "No HP"
}
));
tabel.setRowSelectionInterval(tabel.getRowCount()-1, tabel.getRowCount()-1);
jScrollPane1.setViewportView(tabel);

jPanel1.add(jScrollPane1);
jScrollPane1.setBounds(10, 140, 430, 190);

btambah.setText("Tambah");
btambah.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btambahActionPerformed(evt);
}
});
jPanel1.add(btambah);
btambah.setBounds(10, 110, 90, 23);

bhapus.setText("Hapus");
bhapus.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bhapusActionPerformed(evt);
}
});
jPanel1.add(bhapus);
bhapus.setBounds(110, 110, 73, 23);

getContentPane().add(jPanel1);
jPanel1.setBounds(0, 0, 450, 340);

java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-456)/2, (screenSize.height-372)/2, 456, 372);
}//


private void btambahActionPerformed(java.awt.event.ActionEvent evt) {
int nama = tnama.getText().length();
int alamat = talamat.getText().length();
int nohp = tno_hp.getText().length();
if(nama==0|alamat==0|nohp==0)
{
JOptionPane.showMessageDialog(rootPane, "Terdapat Kolom Yang Kosong!", "Informasi", JOptionPane.INFORMATION_MESSAGE);
}
else
{
tabel.setValueAt(tnama.getText(), tabel.getSelectedRow(), 0);
tabel.setValueAt(talamat.getText(), tabel.getSelectedRow(), 1);
tabel.setValueAt(tno_hp.getText(), tabel.getSelectedRow(), 2);
model.addRow(data);
tabel.setRowSelectionInterval(tabel.getRowCount()-1, tabel.getRowCount()-1);
}
}

private void bhapusActionPerformed(java.awt.event.ActionEvent evt) {
model.removeRow(tabel.getSelectedRow());
if(tabel.getRowCount()==0)
{
model.addRow(data);
}
tabel.setRowSelectionInterval(tabel.getRowCount()-1, tabel.getRowCount()-1);
}

public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new utama().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JButton bhapus;
private javax.swing.JButton btambah;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable tabel;
private javax.swing.JTextField talamat;
private javax.swing.JTextField tnama;
private javax.swing.JTextField tno_hp;
// End of variables declaration

}

GUI Transparan

import com.sun.awt.AWTUtilities;

public class NewJFrame extends javax.swing.JFrame {

public NewJFrame() {
initComponents();
/**Untuk Membuat Form Transparan*/
AWTUtilities.setWindowOpacity(this, 0.70F);
}
@SuppressWarnings("unchecked")
//
private void initComponents() {

jLabel1 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Transparan");

jLabel1.setText("Hello world");

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(159, 159, 159)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(173, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(137, 137, 137)
.addComponent(jLabel1)
.addContainerGap(149, Short.MAX_VALUE))
);

pack();
}//



public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
// End of variables declaration

}

Hasilnya :

TrayIcon

import java.awt.SystemTray;
import java.awt.TrayIcon;
import javax.swing.ImageIcon;

public class postingan extends javax.swing.JFrame {

/** Creates new form postingan */
SystemTray sistem;
ImageIcon gambar;
TrayIcon ikon;
public postingan() {
initComponents();
gambar = new ImageIcon(getClass().getResource("/logo_garuda.jpg"));
ikon = new TrayIcon(gambar.getImage(), "Ini Trayicon");
sistem = SystemTray.getSystemTray();
try
{
sistem.add(ikon);
}catch(Exception e)
{
}
}
@SuppressWarnings("unchecked")
//
private void initComponents() {

jLabel1 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Demo JfileChooser");
setLocationByPlatform(true);
getContentPane().setLayout(null);

jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/logo_garuda.jpg"))); // NOI18N
getContentPane().add(jLabel1);
jLabel1.setBounds(130, 70, 150, 90);

java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-416)/2, (screenSize.height-272)/2, 416, 272);
}//

public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new postingan().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
// End of variables declaration
}
Hasilnya :


Sekarang Perhatikan Di Notifications areanya, Terdapat Icon Sebagai Berikut :

Saturday, 28 November 2009

JfileChooser

Mula-mula kita buat form seperti di bawah ini :



Kita Tambahkan Event Jbuttonnya, sebagai berikut :

private void bbrowseActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser file = new JFileChooser();
file.setAcceptAllFileFilterUsed(true);
file.setFileFilter(new FileNameExtensionFilter("Gambar", "jpg"));
file.showOpenDialog(this);
}

kode di atas artinya ketika kita mengklik tombol browse maka akan menampilkan jfilechooser, jfilechooser hanya untuk menampilkan gambar dengan extensi jpg.

Berikut Source Kode Langkapnya :
public class postingan extends javax.swing.JFrame {

/** Creates new form postingan */
public postingan() {
initComponents();
}

@SuppressWarnings("unchecked")
//
private void initComponents() {

bbrowse = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Demo JfileChooser");
setLocationByPlatform(true);
getContentPane().setLayout(null);

bbrowse.setText("Browse");
bbrowse.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bbrowseActionPerformed(evt);
}
});
getContentPane().add(bbrowse);
bbrowse.setBounds(140, 100, 110, 23);

java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-416)/2, (screenSize.height-272)/2, 416, 272);
}//


private void bbrowseActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser file = new JFileChooser();
file.setAcceptAllFileFilterUsed(true);
file.setFileFilter(new FileNameExtensionFilter("Gambar", "jpg"));
file.showOpenDialog(this);
}

public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new postingan().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JButton bbrowse;
// End of variables declaration

}

Hasilnya :

Jprogress Bar Dan Thread

Berikut ini saya berikan contoh penggunaan thread dalam jprogress bar.
mula-mula kita buat form seperti di bawah ini :
Setelah membuat form, selanjutnya kita mainkan kodenya. pada bagian kode seperti berikut :
public class postingan extends javax.swing.JFrame , kita tambahkan threadnya maka kodenya jadi seperti berikut :
public class postingan extends javax.swing.JFrame implements Runnable {
selanjtnya pada bagian event jbuttonnya. kita berikan event yang akan membangkitkan thread, kode nya seperti berikut :
new Thread(this).start();
tambahkan juga threadnya pada bagian paling bawah sebagai berikut :
public void run() {
while(true)
{
jproses.setValue(i);
jproses.setStringPainted(rootPaneCheckingEnabled);
i++;
try
{
Thread.sleep(1000);
}catch(Exception e)
{

}
}
}

berikut source kode lengkapnya :
public class postingan extends javax.swing.JFrame implements Runnable {

/** Creates new form postingan */
int i=0;
public postingan() {
initComponents();
}

@SuppressWarnings("unchecked")
//
private void initComponents() {

bklik = new javax.swing.JButton();
jproses = new javax.swing.JProgressBar();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Demo Jprogress bar dan thread");
setLocationByPlatform(true);
getContentPane().setLayout(null);

bklik.setText("Klik Saya");
bklik.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bklikActionPerformed(evt);
}
});
getContentPane().add(bklik);
bklik.setBounds(120, 110, 170, 23);
getContentPane().add(jproses);
jproses.setBounds(30, 50, 350, 19);

java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-416)/2, (screenSize.height-272)/2, 416, 272);
}//


private void bklikActionPerformed(java.awt.event.ActionEvent evt) {
new Thread(this).start();
}


public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new postingan().setVisible(true);
}
});
  }

// Variables declaration - do not modify
private javax.swing.JButton bklik;
private javax.swing.JProgressBar jproses;
// End of variables declaration

public void run() {
while(true)
{
jproses.setValue(i);
jproses.setStringPainted(rootPaneCheckingEnabled);
i++;
try
{
Thread.sleep(1000);
}catch(Exception e)
{

}
}
}
}
 Hasilnya :