ya sekedar mencobah belajar java dengan membuat projek Kalkulator agak berbeda dengan kalkulator di delphi dulu,baik codingnya dan tataletaknya langsung aja ni :
coding ini simpan/Save aja dengan nama CobaCalculator. setelah itu compile jadi deh. ni cuma kalkulator sedehana banget untuk lebih bagus silahkan modifikasi
coding ini simpan/Save aja dengan nama CobaCalculator. setelah itu compile jadi deh. ni cuma kalkulator sedehana banget untuk lebih bagus silahkan modifikasi
/**
* @(#)Text1.java
*
*
* @author
* @version 1.00 2009/1/8
*/
import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.UIManager;
class MiniCalculator extends JFrame implements ActionListener {
private JPanel panelAtas = new JPanel();
private JPanel panelTengah = new JPanel();
private JPanel panelBawah = new JPanel();
private JTextField tampilan = new JTextField("");
private JCheckBox pemisahRibuan = new JCheckBox("Pemisah Ribuan", true);
private JButton[] tombolFungsi =
{
new JButton("Hapus"), new JButton("Batal"),
new JButton("Ulang"), new JButton("^"),
new JButton("7"), new JButton("8"),
new JButton("9"), new JButton("*"),
new JButton("4"), new JButton("5"),
new JButton("6"), new JButton("/"),
new JButton("1"), new JButton("2"),
new JButton("3"), new JButton("-"),
new JButton("0"), new JButton("."),
new JButton("="), new JButton("+"),
};
private JLabel informasi = new JLabel("(c)2008");
private JButton tombolExit = new JButton("Exit");
private char operator = ' ';
private double dataPertama = 0.0;
private double dataKedua = 0.0;
private double dataHasil = 0.0;
private boolean awalKetikan = true;
private boolean entryPertama = true;
private boolean entryDesimal = true;
//----------------------------
//Constructor class
//----------------------------
public MiniCalculator() {
super("Kalkulator Sederhana");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setSize(350,300);
setResizable(false);
//Procedure
setPanelAtas();
setPanelTengah();
setPanelBawah();
resetNilai();
getContentPane().setLayout(new BorderLayout());
getContentPane().add(panelAtas, BorderLayout.NORTH);
getContentPane().add(panelTengah, BorderLayout.CENTER);
getContentPane().add(panelBawah, BorderLayout.SOUTH);
show();
}
//----------------------
//Mereset seluruh nilai
//----------------------
private void resetNilai()
{
operator = ' ';
dataPertama = 0.0;
dataKedua = 0.0;
dataHasil = 0.0;
awalKetikan = true;
entryPertama = true;
entryDesimal = false;
}
//-------------------
//Mengatur Panel Atas
//-------------------
private void setPanelAtas()
{
pemisahRibuan.setForeground(new Color (0,0,0));
pemisahRibuan.setFont(new Font ("arial", Font.PLAIN, 14));
tampilan.setEnabled(false);
tampilan.setHorizontalAlignment(JTextField.RIGHT);
tampilan.setFont(new Font("arial", Font.BOLD, 15));
panelAtas.setLayout(new BorderLayout());
panelAtas.add(tampilan, BorderLayout.CENTER);
panelAtas.add(pemisahRibuan, BorderLayout.SOUTH);
}
//---------------------
//Mengatur Panel Tengah
//---------------------
private void setPanelTengah()
{
panelTengah.setLayout(new GridLayout (5,4));
for (int i=0; i<5*4; temp =" data.toString();"> 3)
{
temp = data.substring(data.length() - 3);
data.delete(data.length() - 3, data.length());
temp = pisahkan(data) + ',' + temp.toString();
}
return(temp);
}
//---------------------------
//Pemisah Ribuan
//---------------------------
private String pisahkanRibuan(double data)
{
String string = Double.toString(data);
int titik = string.indexOf('.');
String pecahan = string.substring(titik);
long bulat = new Double(dataHasil).longValue();
string = Long.toString(bulat);
string = pisahkan(new StringBuffer (string));
return(string);
}
//---------------------------
//Menghapus karakter terakhir
//---------------------------
private void hapusKarakter()
{
if (tampilan.getText().length() > 0)
{
StringBuffer data = new StringBuffer(tampilan.getText());
char terakhir = data.charAt(data.length()-1);
if (terakhir == '.')
entryDesimal = false;
data.deleteCharAt(data.length()-1);
tampilan.setText(data.toString());
}
}
//---------------------------
//Membatalkan data Terakhir
//---------------------------
private void batalkanData()
{
if (entryPertama)
dataPertama = 0.0;
else
dataKedua = 0.0;
tampilan.setText("");
}
//---------------------------
//Mengupdate data angka
//---------------------------
private void updateData(int index)
{
if (awalKetikan)
tampilan.setText("");
String label = tombolFungsi[index].getLabel();
char karakter = label.charAt(0);
StringBuffer data = new StringBuffer(tampilan.getText());
tampilan.setText(data.toString() + karakter);
awalKetikan = false;
}
//---------------------------
//Mengupdate Data Operator
//---------------------------
private void updateOperator(int index)
{
if (entryPertama)
{
StringBuffer data = new StringBuffer(tampilan.getText());
dataPertama = Double.parseDouble(data.toString());
}
String label = tombolFungsi[index].getLabel();
operator = label.charAt(0);
entryPertama = false;
awalKetikan = true;
}
//----------------------------
//Melakukan proses perhitungan
//----------------------------
private void prosesPerhitungan()
{
StringBuffer data = new StringBuffer(tampilan.getText());
dataKedua = Double.parseDouble(data.toString());
switch(operator)
{
case '+': dataHasil = dataPertama + dataKedua;
break;
case '-': dataHasil = dataPertama - dataKedua;
break;
case '*': dataHasil = dataPertama * dataKedua;
break;
case '/': dataHasil = dataPertama / dataKedua;
break;
case '^': dataHasil = Math.pow(dataPertama, dataKedua);
break;
}
if (pemisahRibuan.isSelected())
tampilan.setText(pisahkanRibuan(dataHasil));
else
tampilan.setText(Double.toString(dataHasil));
entryPertama = true;
awalKetikan = true;
}
//--------------------------
//Menambahkan tanda desimal
//--------------------------
private void tambahTandaDesimal()
{
if(!entryDesimal && !awalKetikan)
{
entryDesimal = true;
StringBuffer data = new StringBuffer(tampilan.getText());
tampilan.setText(data.toString() + '.');
}
}
//---------------------------
//Action terhadap respon user
//---------------------------
public void actionPerformed(ActionEvent event)
{
Object objek = event.getSource();
int lokasi = 0;
if (objek == tombolExit)
System.exit(0);
else
{
for (; lokasi<20; lokasi++)
if (objek == tombolFungsi[lokasi])
break;
switch(lokasi)
{
case 0 : hapusKarakter();
break;
case 1 : batalkanData();
break;
case 2 : resetNilai();
tampilan.setText("");
break;
case 16 : ;
case 12 : ;
case 13 : ;
case 14 : ;
case 8 : ;
case 9 : ;
case 10 : ;
case 4 : ;
case 5 : ;
case 6 : updateData(lokasi);
break;
case 3 : ;
case 7 : ;
case 11 : ;
case 15 : ;
case 19 : updateOperator(lokasi);
break;
case 18 : prosesPerhitungan();
break;
case 17 : tambahTandaDesimal();
break;
}
}
}
}
//-----------
//Class Utama
//-----------
public class CobaCalculator
{
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
} catch (Exception e) {
System.out.println("Look And Feel Not Supported, Ignored !!!");
}
new MiniCalculator();
} // end run method
}); // end thread safe
}
}
0 komentar:
Posting Komentar