Senin, 20 Juli 2009

Berikut ini adalah contoh program kalkulator.
Semoga bisa dibuat pedoman bagi para pembaca.

Sebelumnya Anda di anjurkan mengetik main berikut ini.

public class Main(
public static void main(String[] args)(
interfaceCalc objCalc = new interfaceCalc();
objCalc.pack();
objCalc.show();
}
}


Setelah itu baru mengetik interface berikut ini:


import java.awt.*;
import java.awt.event.*;
import javax.swinng.*;
import java.math.*;

class interfaceCalc extends JFrame implements ActionListener{
/*--atribut menu--*/
private JMenuBar menuUtama;
private JMenu menuEdit;
private JMenu menuHelp;
private JMenuItem itemECopy;
private JMenuItem itemEPaste;
private JMenuItem itemHTopic;
private JMenuItem itemHAbout;
/*--atribut tombol--*/
private JButton jBtn0;
private JButton jBtn1;
private JButton jBtn2;
private JButton jBtn3;
private JButton jBtn4;
private JButton jBtn5;
private JButton jBtn6;
private JButton jBtn7;
private JButton jBtn8;
private JButton jBtn9;

private JButton jBtn1perX;
private JButton jBtnBagi;
private JButton jBtnHasil;
private JButton jBtnKali;
private JButton jBtnKoma;
private JButton jBtnKurang;
private JButton jBtnMinus;
private JButton jBtnMod;
private JButton jBtnSqrt;
private JButton jBtnTambah;

private JButton jBtnC;
private JButton jBtnCE;
private JButton jBtnBack;

private JButton jBtnMC;
private JButton jBtnMPlus;
private JButton jBtnMR;
private JButton jBtnMS;
/*--atribut panel--*/
private JPanelText;
private JPanel1;
private JPanel2;
private JPanel3;
private JPanel4;
/*--atribut text--*/
private JTextField jTextCalc;
private JTextField jTextApa;
/*--atribut text--*/
private double temp=0;
private double temp1=0;
private double hasil=0;
private double count=0;
private double countkoma=0;
private double jumlahkoma=0;
private String text;
/*--atribut tanda--*/
private int klik;
private char tanda;
private int koma;
private boolean tandaklik=0;
private int jumlahklik=0;
private int ceksamadengan;
private double memory=0;
private double tandaMPlus;

public interfaceCalc() {
super("Program Kalkulator");
setSize(250,160);
setResizable(false);
super.setDefaulCloseOperation(EXIT_ON_CLOSE);
initMenu();
initPanelText();
initPanel1();
initPanel2();
initPanel3();
initPanel4();
initialize();
initActionPerformed();
}

public void initMenu() {
menuUtama = new JMenuBar();
setJMenuBar(menuUtama);
menuEdit = new JMenu("Edit");
menuHelp = new JMenu("Help");
menuUtama.add(menuEdit);
menuUtama.add(menuHelp);

itemECopy = new JMenuItem("Copy \t Ctrl+c");
itemEPaste = new JMenuItem("Paste \t Ctrl+v");
itemHTopic = new JMenuItem("Help Topic");
itemHAbout = new JMenuItem("About");

menuEdit.add(itemECopy);
menuEdit.add(itemEPaste);
menuHelp.add(itemHTopic);
menuHelp.add(itemHAbout);
}

public void initPanelText(){
/* Membuat Panel Text */
jPanelText = new JPanel();
jTextCalc = new JTextField("0",30);
jTextCalc.setEditable(false);
jTextCalc.setHorizontalAlignment(jTextField.RIGHT);
jPanelText.add(jTextCalc);
}

public void initPanel1() {
/* Membuat Panel */
jPanel1 = new JPanel1();
jPanel.setLayout(new GridLayout(1,3,1,2));
jBtnC = new JButton("C");
jBtnCE = new JButton("CE");
jBtnBack = new JButton("Backspace");
jPanel1.add(jBtnBack);
jPanel1.add(jBtnCE);
jPanel1.add(jBtnC);
}

public void initPanel2() {
jPanel2 = new JPanel();
jPanel2.setLayout(new GridLayout(4,6,1,2));
jBtn0 = new JButton("0");
jBtn0 = new JButton("1");
jBtn0 = new JButton("2");
jBtn0 = new JButton("3");
jBtn0 = new JButton("4");
jBtn0 = new JButton("5");
jBtn0 = new JButton("6");
jBtn0 = new JButton("7");
jBtn0 = new JButton("8");
jBtn0 = new JButton("9");
jBtnMinus = new JButton("+/-");
jBtnKoma = new JButton(".");
jBtnBagi = new JButton("/");
jBtnKali = new JButton("*");
jBtnKurang = new JButton("-");
jBtnTambah = new JButton("+");
jBtnSqrt = new JButton("sqrt");
jBtnMod = new JButton("%");
jBtn1perX = new JButton("1/x");
jBtnasil = new JButton("=");
jPanel2.add(jBtn7);
jPanel2.add(jBtn8);
jPanel2.add(jBtn9);
jPanel2.add(jBtnBagi);
jPanel2.add(jBtnSqrt);
jPanel2.add(jBtn4);
jPanel2.add(jBtn5);
jPanel2.add(jBtn6);
jPanel2.add(jBtnKali);
jPanel2.add(jBtnMod);
jPanel2.add(jBtn1);
jPanel2.add(jBtn2);
jPanel2.add(jBtn3);
jPanel2.add(jBtnKurang);
jPanel2.add(jBtn1perX);
jPanel2.add(jBtn0);
jPanel2.add(jBtnMinus);
jPanel2.add(jBtnKoma);
jPanel2.add(jBtnTambah);
jPanel2.add(jBtnHasil);
}

public void initPanel3() {
/* Membuat Panel 3 */
jPanel3 = new JPanel();
jPanel3.setLayout(new GridLayout(4,1,1,2));
jPanelMC = new JButton("MC");
jPanelMR = new JButton("MR");
jPanelMS = new JButton("MS");
jPanelMPlus = new JButton("M+");
jPanel3.add(jBtnMC);
jPanel3.add(jBtnMR);
jPanel3.add(jBtnMS);
jPanel3.add(jBtnMPlus);
}

public void initPanel4() {
/* Membuat Panel 4 */
jPanel4 = new JPanel();
jTextApa = new JTextField("",3);
jTextApa.setEditable(false);
jPanel4.add(jTextApa);

}

public void initialize() {
GridBagLayout gB;
GridBagConstraints gBc;

gB = new GridBagLayout();
gBc= new GridBagContraints();
getContentPane().setLayout(gB);
gBc.fill = GridBagConstraints.NONE;
gBc.weightx = 1.0;
gBc.gridwidth = 6;
gBc.gridx = 0;
gBc.gridy = 0;
gB.setConstraints(jPanelText,gBc);
getContentPane().add(jPanelText);

gBc.weightx = 1.0;
gBc.gridwidth = 1;
gBc.gridx = 0;
gBc.gridy = 1;
gB.setConstraints(jPanel4,gBc);
getContentPane().add(jPanel4);

gBc.weightx = 1.0;
gBc.gridwidth = 5;
gBc.gridx = 1;
gBc.gridy = 1;
gB.setConstraints(jPanel1,gBc);
getContentPane().add(jPanel1);

gBc.weightx = 1.0;
gBc.gridwidth = 1;
gBc.gridheight = 4;
gBc.gridx = 0;
gBc.gridy = 2;
gB.setConstraints(jPanel3,gBc);
getContentPane().add(jPanel3);

gBc.weightx = 1.0;
gBc.gridwidth = 5;
gBc.gridheight = 4;
gBc.gridx = 1;
gBc.gridy = 2;
gB.setConstraints(jPanel2,gBc);
getContentPane().add(jPanel2);
}

public void actionPerformed(ActionEvent e){
double tempkoma=1;
if ((e.getSource()==jBtn0)||(e.getSource()==jBtn1)||(e.getSource()==jBtn2)||(e.getSource()==jBtn3)||(e.getSource()==jBtn4)||(e.getSource()==jBtn5)||(e.getSource()==jBtn6)||(e.getSource()==jBtn7)||(e.getSource()==jBtn8)||(e.getSource()==jBtn9)||(e.getSource()==jBtnKoma)||(e.getSource()==jBtnMinus));
jumlahklik=0;
ceksamadengan=0;
if (e.getSource()==jBtn0){
if (koma!=0){
jumlahkoma++;
for (int i=1;i<=jumlahkoma ;i++ ){
tempkoma=tempkoma*10;
}
countkoma=0/tempkoma;
temp=temp+coutkoma;
}
else {
temp=count+0;
}
}
else if ((e.getSource()==jBtn1){
if(koma!=0){
jumlahkoma++;
for (int i=1;i<=jumlahkoma ;i++ ){
tempkoma=tempkoma*10;
}
countkoma=1/tempkoma;
temp=temp+coutkoma;
}
else {
temp=count+1;
}
}
else if ((e.getSource()==jBtn2){
if(koma!=0){
jumlahkoma++;
for (int i=1;i<=jumlahkoma ;i++ ){
tempkoma=tempkoma*10;
}
countkoma=2/tempkoma;
temp=temp+coutkoma;
}
else {
temp=count+2;
}
}
else if ((e.getSource()==jBtn3){
if(koma!=0){
jumlahkoma++;
for (int i=1;i<=jumlahkoma ;i++ ){
tempkoma=tempkoma*10;
}
countkoma=3/tempkoma;
temp=temp+coutkoma;
}
else {
temp=count+3;
}
}
else if ((e.getSource()==jBtn4){
if(koma!=0){
jumlahkoma++;
for (int i=1;i<=jumlahkoma ;i++ ){
tempkoma=tempkoma*10;
}
countkoma=4/tempkoma;
temp=temp+coutkoma;
}
else {
temp=count+4;
}
}
else if ((e.getSource()==jBtn5){
if(koma!=0){
jumlahkoma++;
for (int i=1;i<=jumlahkoma ;i++ ){
tempkoma=tempkoma*10;
}
countkoma=5/tempkoma;
temp=temp+coutkoma;
}
else {
temp=count+5;
}
}
else if ((e.getSource()==jBtn6){
if(koma!=0){
jumlahkoma++;
for (int i=1;i<=jumlahkoma ;i++ ){
tempkoma=tempkoma*10;
}
countkoma=6/tempkoma;
temp=temp+coutkoma;
}
else {
temp=count+6;
}
}
else if ((e.getSource()==jBtn7){
if(koma!=0){
jumlahkoma++;
for (int i=1;i<=jumlahkoma ;i++ ){
tempkoma=tempkoma*10;
}
countkoma=7/tempkoma;
temp=temp+coutkoma;
}
else {
temp=count+7;
}
}
else if ((e.getSource()==jBtn8){
if(koma!=0){
jumlahkoma++;
for (int i=1;i<=jumlahkoma ;i++ ){
tempkoma=tempkoma*10;
}
countkoma=8/tempkoma;
temp=temp+coutkoma;
}
else {
temp=count+8;
}
}
else if ((e.getSource()==jBtn9){
if(koma!=0){
jumlahkoma++;
for (int i=1;i<=jumlahkoma ;i++ ){
tempkoma=tempkoma*10;
}
countkoma=9/tempkoma;
temp=temp+coutkoma;
}
else {
temp=count+9;
}
}
else if ((e.getSource()==jBtn4){
if(koma!=0){
jumlahkoma++;
for (int i=1;i<=jumlahkoma ;i++ ){
tempkoma=tempkoma*10;
}
countkoma=4/tempkoma;
temp=temp+coutkoma;
}
else {
temp=count+4;
}
}

Tidak ada komentar:

Posting Komentar