LAPORAN
PRAKTIKUM
Bahasa Pemograman 2
Modul I
Nama : Radea Nurlaila
NIM :
2015091083
Prodi :
SI 2015 C
LABORATORIUM
KOMPUTER
FAKULTAS
ILMU KOMPUTER
UNIVERSITAS
KUNINGAN
2017
Jl.
Cut NyakDien, No.36A CijohoKuninganTelp/Faks : 0232-974824
1. FORM PERHITUNGAN SEDERHANA
Source Code :
package Latihan_01;
/**
*
* @author uniku
*/
public class frm_01 extends javax.swing.JFrame {
/**
* Creates new form frm_01
*/
public frm_01() {
initComponents();
}
/**
* 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() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
txtBil1 = new javax.swing.JTextField();
txtBil2 = new javax.swing.JTextField();
txtOperator = new javax.swing.JTextField();
jLabel4 = new javax.swing.JLabel();
txtHasil = new javax.swing.JTextField();
jLabel5 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Bilangan ke-1");
jLabel2.setText("Bilangan ke-2");
jLabel3.setText("Operator");
txtBil1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtBil1ActionPerformed(evt);
}
});
txtBil2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtBil2ActionPerformed(evt);
}
});
txtOperator.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtOperatorActionPerformed(evt);
}
});
jLabel4.setText("Hasil");
jLabel5.setText("Program Perhitungan");
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(87, 87, 87)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel5)
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(49, 49, 49))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3)
.addComponent(jLabel2)
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(txtBil1)
.addComponent(txtBil2)
.addComponent(txtOperator)
.addComponent(txtHasil, javax.swing.GroupLayout.DEFAULT_SIZE, 84, Short.MAX_VALUE))
.addGap(364, 364, 364))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(22, 22, 22)
.addComponent(jLabel5)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(txtBil1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(txtBil2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(txtOperator, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(txtHasil, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(119, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void txtBil2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
// txtHasil.setText(String.valueOf(Integer.parseInt(txtBil1.getText())+Integer.parseInt(txtBil2.getText())));
// int Bil1,Bil2,Hasil;
// Bil1=Integer.parseInt(txtBil1.getText());
// Bil2=Integer.parseInt(txtBil2.getText());
// Hasil=Bil1+Bil2;
// txtOperator.setText(String.valueOf(Hasil));
}
private void txtBil1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
// int Bil1,Bil2,Hasil;
//Bil1=Integer.parseInt(txtBil1.getText());
// Bil2=Integer.parseInt(txtBil2.getText());
// Hasil=Bil1+Bil2;
// txtOperator.setText(String.valueOf(Hasil));
}
private void txtOperatorActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int Bil1,Bil2,hsl;
Bil1=Integer.parseInt(txtBil1.getText());
Bil2=Integer.parseInt(txtBil2.getText());
if (txtOperator.getText().equals("+")){
hsl=Bil1+Bil2;
txtHasil.setText(String.valueOf(hsl));
}else if (txtOperator.getText().equals("-")){
hsl=Bil1-Bil2;
txtHasil.setText(String.valueOf(hsl));
}else if (txtOperator.getText().equals("*")){
hsl=Bil1*Bil2;
txtHasil.setText(String.valueOf(hsl));
}else if (txtOperator.getText().equals("/")){
hsl=Bil1/Bil2;
txtHasil.setText(String.valueOf(hsl));
} else {
txtHasil.setText("error!");
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(frm_01.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(frm_01.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(frm_01.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(frm_01.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new frm_01().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JTextField txtBil1;
private javax.swing.JTextField txtBil2;
private javax.swing.JTextField txtHasil;
private javax.swing.JTextField txtOperator;
// End of variables declaration
}
2. FORM LOGIN SEDERHANA
Source Code :
package Latihan_01;
import javax.swing.JOptionPane;
/**
*
* @author uniku
*/
public class frm_03 extends javax.swing.JFrame {
/**
* Creates new form frm_03
*/
public frm_03() {
initComponents();
}
/**
* 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() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
txtuser = new javax.swing.JTextField();
btnlogin = new javax.swing.JButton();
btncancel = new javax.swing.JButton();
txtpassword = new javax.swing.JPasswordField();
jLabel3 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("User Name");
jLabel2.setText("Password");
txtuser.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtuserActionPerformed(evt);
}
});
btnlogin.setText("Login");
btnlogin.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnloginActionPerformed(evt);
}
});
btncancel.setText("Cancel");
btncancel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btncancelActionPerformed(evt);
}
});
txtpassword.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtpasswordActionPerformed(evt);
}
});
jLabel3.setText("Login Form");
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(81, 81, 81)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE)
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(txtuser)
.addGroup(layout.createSequentialGroup()
.addComponent(btnlogin)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(btncancel))
.addComponent(txtpassword))))
.addContainerGap(344, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(27, 27, 27)
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(txtuser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(txtpassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnlogin)
.addComponent(btncancel))
.addContainerGap(149, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void txtuserActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void txtpasswordActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void btnloginActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String user,pass;
user = "radea";
pass = "12345";
if(txtuser.getText().equals(user) && String.valueOf(txtpassword.getPassword()).equals(pass)){
frm_01 a= new frm_01();
JOptionPane.showMessageDialog(null,"Login Berhasil");
a.setVisible(true);
}else{
JOptionPane.showMessageDialog(null,"Login Gagal");
}
}
private void btncancelActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
txtuser.setText(null);
txtpassword.setText(null);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(frm_03.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(frm_03.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(frm_03.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(frm_03.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new frm_03().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btncancel;
private javax.swing.JButton btnlogin;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JPasswordField txtpassword;
private javax.swing.JTextField txtuser;
// End of variables declaration
}
3. KALKULATOR SEDERHANA
Langkah-Langkah Pembuatan :
2. Rancanglah tampilan kalkuator tersebut
3. Ubahlah variable JtextField & JButton yang ada
1 4.
Masukan script program pada
JButton dengan cara klik kanan pada JButton yang dituju, kemudian pilih events
-> Action -> Action Performed
Berikut
program yang diinputkan pada setiap JButton :
·
btnAngka0
angka += "0";
display.setText(angka);
|
·
btnAngka1
angka += "1";
display.setText(angka);
|
·
btnAngka2
angka +=
"2";
display.setText(angka);
|
·
btnAngka3
angka +=
"3";
display.setText(angka);
|
·
btnAngka4
angka +=
"4";
display.setText(angka);
|
·
btnAngka5
angka +=
"5";
display.setText(angka);
|
·
btnAngka6
angka +=
"6";
display.setText(angka);
|
·
btnAngka7
angka +=
"7";
display.setText(angka);
|
·
btnAngka8
angka +=
"8";
display.setText(angka);
|
·
btnAngka9
angka +=
"9";
display.setText(angka);
|
Script diatas berfungsi agar ketika
Jbutton ditekan makan akan menampilkan angka tertentu pada JTextField . Lalu
mengubah text pada display menjadi isi dari variable angka
·
btnKoma
angka +=
".";
display.setText(angka);
|
Berfungsi menampilan Koma(.)
·
btnHapus
display.setText("");
angka1=0.0;
angka2=0.0;
jumlah=0.0;
angka="";
|
Menghapus variable yang telah
diinputkan
·
btnTambah
angka1=Double.parseDouble(angka);
display.setText("+");
angka="";
pilih=1;
|
·
btnKurang
angka1=Double.parseDouble(angka);
display.setText("-");
angka="";
pilih=2;
|
·
btnKali
angka1=Double.parseDouble(angka);
display.setText("*");
angka="";
pilih=3;
|
·
btnBagi
angka1=Double.parseDouble(angka);
display.setText("/");
angka="";
pilih=4;
|
·
btnSamadengan
switch(pilih){
case 1:
angka2 = Double.parseDouble(angka);
jumlah = angka1 + angka2;
angka = Double.toString(jumlah);
display.setText(angka);
break;
case 2:
angka2 = Double.parseDouble(angka);
jumlah = angka1 - angka2;
angka = Double.toString(jumlah);
display.setText(angka);
break;
case 3:
angka2
= Double.parseDouble(angka);
jumlah
= angka1 * angka2;
angka
= Double.toString(jumlah);
display.setText(angka);
break;
case 4:
angka2
= Double.parseDouble(angka);
jumlah
= angka1 / angka2;
angka
= Double.toString(jumlah);
display.setText(angka);
break;
default:
break;
}
|
1 5. Setelah
input program pada JButton telah selesai dilakukan, Run (jalankan) program
tersebut
4. SCRIPT KALKULATOR TANPA GUI
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import java.sql.*;
import javax.swing.table.DefaultTableModel;
public class kalkulator extends JFrame implements ActionListener
{
private JPanel pBrg = new JPanel();
private JLabel l1 = new JLabel ("1"),
lSama = new JLabel ("Jumlah "),
lnama = new JLabel ("Dibuat Oleh : Radea Nurlaila");
private JTextField fLayar = new JTextField ();
private JButton btnTambah = new JButton (),
btnKurang = new JButton (),
btnKali = new JButton (),
btnBagi = new JButton (),
btnPersen = new JButton (),
btnKoma = new JButton (),
btnOff = new JButton (),
btnOn = new JButton (),
btnBack = new JButton (),
btnMinus = new JButton (),
btnPangkat = new JButton (),
btnBagi1 = new JButton (),
btn00 = new JButton (),
btn0 = new JButton (),
btn1 = new JButton (),
btn2 = new JButton (),
btn3 = new JButton (),
btn4 = new JButton (),
btn5 = new JButton (),
btn6 = new JButton (),
btn7 = new JButton (),
btn8 = new JButton (),
btn9 = new JButton (),
btnSama = new JButton ();
private String isiLayar = "";
private String snilai1, snilai2, tombol,bukan_angka;
private int hasil;
boolean angka_baru = false;
public Kalkulator() {
setPreferredSize(new Dimension(500,530));
setTitle("Kalkulator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
new BorderLayout();
JDesktopPane pBrg = new JDesktopPane();
add(pBrg, BorderLayout.CENTER);
pBrg.setBackground(Color.red);
/* Mengatur letak objek Text Di Container */
fLayar.setBounds (50, 40, 385, 50);
lnama.setBounds (250, 450, 200, 50);
/* Mengatur letak objek Button di Container */
btnOff.setBounds (50, 100, 85, 40);
btnOff.setLabel("Exit");
btnOn.setBounds (350, 100, 85, 40);
btnOn.setLabel("C");
btnTambah.setBounds (350, 160, 85, 40);
btnTambah.setLabel("+");
btnKurang.setBounds (350, 210, 85, 40);
btnKurang.setLabel("-");
btnKali.setBounds (350, 260, 85, 40);
btnKali.setLabel("*");
btnBagi.setBounds (350, 310, 85, 40);
btnBagi.setLabel("/");
btnBack.setBounds (150, 100, 185, 40);
btnBack.setLabel("Backspace");
btnSama.setBounds (50, 360, 185, 90);
btnSama.setLabel("=");
btnSama.setFont(new Font("Times New Roman", 10, 50));
btnKoma.setBounds (250,310, 85, 40);
btnKoma.setLabel(".");
btnPersen.setBounds (350, 360, 85, 40);
btnPersen.setLabel("%");
btnPangkat.setBounds (350, 410, 85, 40);
btnPangkat.setLabel("x^y");
btnBagi1.setBounds (250, 410, 85, 40);
btnBagi1.setLabel("1/x");
btnMinus.setBounds (250, 360, 85, 40);
btnMinus.setLabel("( - )");
//btnAdd.setToolTipText("Tombol Tambah Data");
btn7.setBounds (50, 160, 85, 40);
btn7.setLabel("7");
btn8.setBounds (150, 160, 85, 40);
btn8.setLabel("8");
btn9.setBounds (250, 160, 85, 40);
btn9.setLabel("9");
btn4.setBounds (50, 210, 85, 40);
btn4.setLabel("4");
btn5.setBounds (150, 210, 85, 40);
btn5.setLabel("5");
btn6.setBounds (250, 210, 85, 40);
btn6.setLabel("6");
btn1.setBounds (50, 260, 85, 40);
btn1.setLabel("1");
btn2.setBounds (150, 260, 85, 40);
btn2.setLabel("2");
btn3.setBounds (250, 260, 85, 40);
btn3.setLabel("3");
btn0.setBounds (150, 310, 85, 40);
btn0.setLabel("0");
btn00.setBounds (50, 310, 85, 40);
btn00.setLabel("00");
fLayar.setHorizontalAlignment (JTextField.RIGHT);
/* Objek Button di Non Aktifkan dan di aktifkan */
btnTambah.setEnabled(true);
btnKurang.setEnabled(true);
btnKali.setEnabled(true);
btnBagi.setEnabled(true);
btnSama.setEnabled(true);
btnBack.setEnabled(true);
btnPersen.setEnabled(true);
btnMinus.setEnabled(true);
btnPangkat.setEnabled(true);
btnBagi1.setEnabled(true);
/* Mengatur objek untuk dapat berinteraksi */
btn1.addActionListener (this);
btn2.addActionListener (this);
btn3.addActionListener (this);
btn4.addActionListener (this);
btn5.addActionListener (this);
btn6.addActionListener (this);
btn7.addActionListener (this);
btn8.addActionListener (this);
btn9.addActionListener (this);
btn0.addActionListener (this);
btn00.addActionListener (this);
btnTambah.addActionListener (this);
btnKurang.addActionListener (this);
btnKali.addActionListener (this);
btnBagi.addActionListener (this);
btnSama.addActionListener (this);
btnKoma.addActionListener (this);
btnOff.addActionListener (this);
btnOn.addActionListener (this);
btnBack.addActionListener (this);
btnPersen.addActionListener (this);
btnMinus.addActionListener (this);
btnPangkat.addActionListener (this);
btnBagi1.addActionListener (this);
// Meletakkan seluruh kontrol pada objek panel */
pBrg.add (fLayar);
pBrg.add (btn1);
pBrg.add (btn2);
pBrg.add (btn3);
pBrg.add (btn4);
pBrg.add (btn5);
pBrg.add (btn6);
pBrg.add (btn7);
pBrg.add (btn8);
pBrg.add (btn9);
pBrg.add (btn0);
pBrg.add (btn00);
pBrg.add (btnOff);
pBrg.add (btnOn);
pBrg.add (btnTambah);
pBrg.add (btnKurang);
pBrg.add (btnSama);
pBrg.add (btnKali);
pBrg.add (btnBagi);
pBrg.add (btnKoma);
pBrg.add (btnBack);
pBrg.add (btnPersen);
pBrg.add (btnMinus);
pBrg.add (btnPangkat);
pBrg.add (btnBagi1);
pBrg.add (lnama);
/* Menambahkan objek panel (pBrg) ke container frame */
//getContentPane().add (pBrg);
/* Menampilkan frame ke layar monitor */
pack();
setVisible (true);
setLocationRelativeTo(null);
}
public static void main(String[] args) {
new Kalkulator().setVisible(true);
}
/* Fungsi jika user melakukan action penekanan tombol Button */
public void actionPerformed (ActionEvent ae) {
Object obj = ae.getSource();
String a = ae.getActionCommand();
if (obj == btn1)
{
isiLayar = isiLayar + "1";
fLayar.setText(isiLayar);
}
if (obj == btn2)
{
isiLayar = isiLayar + "2";
fLayar.setText(isiLayar);
}
if (obj == btn3)
{
isiLayar = isiLayar + "3";
fLayar.setText(isiLayar);
}
if (obj == btn4)
{
isiLayar = isiLayar + "4";
fLayar.setText(isiLayar);
}
if (obj == btn5)
{
isiLayar = isiLayar + "5";
fLayar.setText(isiLayar);
}
if (obj == btn6)
{
isiLayar = isiLayar + "6";
fLayar.setText(isiLayar);
}
if (obj == btn7)
{
isiLayar = isiLayar + "7";
fLayar.setText(isiLayar);
}
if (obj == btn8)
{
isiLayar = isiLayar + "8";
fLayar.setText(isiLayar);
}
if (obj == btn9)
{
isiLayar = isiLayar + "9";
fLayar.setText(isiLayar);
}
if (obj == btn0)
{
isiLayar = isiLayar + "0";
fLayar.setText(isiLayar);
}
if (obj == btn00)
{
isiLayar = isiLayar + "00";
fLayar.setText(isiLayar);
}
if (obj == btnKoma)
{
isiLayar = isiLayar + ".";
fLayar.setText(isiLayar);
}
if (obj == btnTambah)
{
tombol = "tambah";
snilai1 = fLayar.getText();
isiLayar = "";
fLayar.setText(isiLayar);
}
if (obj == btnKurang)
{
tombol = "kurang";
snilai1 = fLayar.getText();
isiLayar = "";
fLayar.setText(isiLayar);
}
if (obj == btnKali)
{
tombol = "kali";
snilai1 = fLayar.getText();
isiLayar = "";
fLayar.setText(isiLayar);
}
if (obj == btnBagi)
{
tombol = "bagi";
snilai1 = fLayar.getText();
isiLayar = "";
fLayar.setText(isiLayar);
}
if (obj == btnPersen)
{
tombol = "persen";
snilai1 = fLayar.getText();
isiLayar = "";
fLayar.setText(isiLayar);
if(tombol == "persen")
{
snilai2 = fLayar.getText();
Float hasil = Float.parseFloat(snilai1) / 100;
String shasil = ""+hasil;
fLayar.setText(shasil);
isiLayar = ("");
}
}
if (obj == btnMinus)
{
isiLayar = isiLayar + "-";
fLayar.setText(isiLayar);
}
if (obj == btnPangkat)
{
tombol = "pangkat";
snilai1 = fLayar.getText();
isiLayar = "";
fLayar.setText(isiLayar);
}
if (obj == btnBagi1)
{
tombol = "bagi1";
snilai1 = fLayar.getText();
isiLayar = "";
fLayar.setText(isiLayar);
if(tombol == "bagi1")
{
snilai2 = fLayar.getText();
Float hasil = 1 / Float.parseFloat(snilai1);
String shasil = ""+hasil;
fLayar.setText(shasil);
isiLayar = ("");
}
}
if(obj == btnOff)
{
this.dispose();
}
if(obj == btnOn)
{
On();
}
if (obj == btnSama)
{
Sama();
}
if (obj == btnBack)
{
{
if (fLayar.getText().length() > 0)
fLayar.setText(fLayar.getText().substring(0, fLayar.getText().length() - 1));
else
JOptionPane.showMessageDialog(null, "Tidak ada angka yang bisa dihapus",
"Kesalahan", JOptionPane.WARNING_MESSAGE);
}
}
}
public void Sama()
{
if(tombol == "tambah")
{
snilai2 = fLayar.getText();
Float hasil = Float.parseFloat(snilai1) + Float.parseFloat(snilai2);
String shasil = ""+hasil;
fLayar.setText(shasil);
isiLayar = ("");
}
if(tombol == "kurang")
{
snilai2 = fLayar.getText();
Float hasil = Float.parseFloat(snilai1) - Float.parseFloat(snilai2);
String shasil = ""+hasil;
fLayar.setText(shasil);
isiLayar = ("");
}
if(tombol == "kali")
{
snilai2 = fLayar.getText();
Float hasil = Float.parseFloat(snilai1) * Float.parseFloat(snilai2);
String shasil = ""+hasil;
fLayar.setText(shasil);
isiLayar = ("");
}
if(tombol == "bagi")
{
snilai2 = fLayar.getText();
Float hasil = Float.parseFloat(snilai1) / Float.parseFloat(snilai2);
String shasil = ""+hasil;
fLayar.setText(shasil);
isiLayar = ("");
}
if(tombol == "pangkat")
{
Double snilai1= Double.valueOf(fLayar.getText());
Double snilai2=Double.valueOf(fLayar.getText());
Double hasil=Double.valueOf(Math.pow(snilai1, snilai2));
String shasil = ""+hasil;
fLayar.setText(shasil);
isiLayar = ("");
}
}
public void On()
{
isiLayar="";
fLayar.setText("0");
}
private void tampil()
{
try
{
}
catch (Exception e)
{
JOptionPane.showMessageDialog(this, "Ada Kesalahan!!!");
}
}
()
{
fLayar.setText ("");
fLayar.requestFocus ();
}
}
0 komentar:
Posting Komentar