0% found this document useful (0 votes)
66 views9 pages

Nadya Parameswari Jasmine

The document describes a Java program that creates a basic calculator application with a graphical user interface. The program uses multiple panels, buttons, and labels to display the calculator layout. It includes code to handle button click events and perform basic math operations like addition, subtraction, multiplication, and division based on the operator clicked. The program initializes the GUI elements, sets event listeners on each button, and contains a class to handle button clicks and perform the calculations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views9 pages

Nadya Parameswari Jasmine

The document describes a Java program that creates a basic calculator application with a graphical user interface. The program uses multiple panels, buttons, and labels to display the calculator layout. It includes code to handle button click events and perform basic math operations like addition, subtraction, multiplication, and division based on the operator clicked. The program initializes the GUI elements, sets event listeners on each button, and contains a class to handle button clicks and perform the calculations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Tugas Pemrograman Visual Pertemuan ke- 5

<Kalkulator dengan event>


Nama : Nadya Parameswari Jasmine

NIM : 09021381621105

Source Code:

<
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package kalkulator;

/**
*
* @author dhea
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Kalkulator extends JFrame{


public static JTextField textField;
public static String angka="";
public static int pilih;
public static double bil1,bil2,jumlah;

public Kalkulator(){
JPanel JInput = new JPanel(new GridLayout(6,4,0,0));

JButton Menu = new JButton("≡");


Menu.setBackground(new Color(225, 225, 225));
Menu.setBorderPainted(false);

JLabel LabelStandard = new JLabel("Standard");


LabelStandard.setBackground(new Color(225, 225, 225));
LabelStandard.setOpaque(true);

JPanel JPStandar = new JPanel(new BorderLayout());


JPStandar.add(Menu, BorderLayout.WEST);
JPStandar.add(LabelStandard, BorderLayout.CENTER);

JTextField TextFieldHitung = new JTextField(" 0 ");


TextFieldHitung.setPreferredSize(new Dimension(0, 100));
TextFieldHitung.setBackground(new Color(225, 225, 225));
TextFieldHitung.setHorizontalAlignment(JTextField.RIGHT);

JPanel JPKiri = new JPanel(new BorderLayout());


JPKiri.add(JPStandar, BorderLayout.NORTH);
JPKiri.add(TextFieldHitung, BorderLayout.SOUTH);

JButton ButtonHistory = new JButton("History");


ButtonHistory.setBackground(new Color(225, 225, 225));
ButtonHistory.setBorderPainted(false);

JButton ButtonMemory = new JButton("Memory");


ButtonMemory.setBackground(new Color(225, 225, 225));
ButtonMemory.setBorderPainted(false);

JPanel JPHistory = new JPanel(new GridLayout(1, 2, 0, 0));


JPHistory.add(ButtonHistory);
JPHistory.add(ButtonMemory);

JLabel LabelHistory = new JLabel("There's no history yet");


LabelHistory.setVerticalAlignment(JLabel.TOP);
LabelHistory.setBackground(new Color(225, 225, 225));
LabelHistory.setOpaque(true);

JPanel JPKanan = new JPanel(new BorderLayout());


JPKanan.add(JPHistory, BorderLayout.NORTH);
JPKanan.add(LabelHistory, BorderLayout.CENTER);

JPanel JOperasi = new JPanel(new GridLayout(1, 5, 0, 0));


JButton Operasi[] = new JButton[5];
Operasi[0] = new JButton("MC");
Operasi[1] = new JButton("MR");
Operasi[2] = new JButton("M+");
Operasi[3] = new JButton("M-");
Operasi[4] = new JButton("MS");

for (int i = 0; i < Operasi.length; i++) {


Operasi[i].setBackground(new Color(225, 225, 225));

Operasi[i].setBorderPainted(false);
JOperasi.add(Operasi[i]);
}
JButton Tombol[] = new JButton[24];
Tombol[0] = new JButton("%");
Tombol[1] = new JButton("√");
Tombol[2] = new JButton("x^2");
Tombol[3] = new JButton("1/x");
Tombol[4] = new JButton("CE");
Tombol[5] = new JButton("C");
Tombol[6] = new JButton("DEL");
Tombol[7] = new JButton("÷");
Tombol[8] = new JButton("7");
Tombol[9] = new JButton("8");
Tombol[10] = new JButton("9");
Tombol[11] = new JButton("x");
Tombol[12] = new JButton("4");
Tombol[13] = new JButton("5");
Tombol[14] = new JButton("6");
Tombol[15] = new JButton("-");
Tombol[16] = new JButton("1");
Tombol[17] = new JButton("2");
Tombol[18] = new JButton("3");
Tombol[19] = new JButton("+");
Tombol[20] = new JButton("±");
Tombol[21] = new JButton("0");
Tombol[22] = new JButton(".");
Tombol[23] = new JButton("=");

for (int i = 0; i < Tombol.length; i++) {


try {
int num = Integer.parseInt(Tombol[i].getText());
Tombol[i].setBackground(new Color(255, 255, 255));
}
catch (NumberFormatException e) {
Tombol[i].setBackground(new Color(240, 240, 240));
}
Tombol[i].setBorderPainted(false);
JInput.add(Tombol[i]);
}
JPanel JPKiri1 = new JPanel(new BorderLayout());
JPKiri1.add(JOperasi, BorderLayout.NORTH);
JPKiri1.add(JInput, BorderLayout.CENTER);

JPanel JPKiri2 = new JPanel(new BorderLayout());


JPKiri2.add(JPKiri, BorderLayout.NORTH);
JPKiri2.add(JPKiri1, BorderLayout.CENTER);

JPanel Panel = new JPanel(new BorderLayout());


Panel.add(JPKiri2, BorderLayout.CENTER);
Panel.add(JPKanan, BorderLayout.EAST);
add(Panel);

ButtonListener listener = new ButtonListener();


Tombol[0].addActionListener(listener);
Tombol[1].addActionListener(listener);
Tombol[2].addActionListener(listener);
Tombol[3].addActionListener(listener);
Tombol[4].addActionListener(listener);
Tombol[5].addActionListener(listener);
Tombol[6].addActionListener(listener);
Tombol[7].addActionListener(listener);
Tombol[8].addActionListener(listener);
Tombol[9].addActionListener(listener);
Tombol[10].addActionListener(listener);
Tombol[11].addActionListener(listener);
Tombol[12].addActionListener(listener);
Tombol[13].addActionListener(listener);
Tombol[14].addActionListener(listener);
Tombol[15].addActionListener(listener);
Tombol[16].addActionListener(listener);
Tombol[17].addActionListener(listener);
Tombol[18].addActionListener(listener);
Tombol[19].addActionListener(listener);
Tombol[20].addActionListener(listener);
Tombol[21].addActionListener(listener);
Tombol[22].addActionListener(listener);
Tombol[23].addActionListener(listener);

public static void main(String[] args){


Kalkulator frame = new Kalkulator();
frame.setTitle("Kalkulator");
frame.setSize(500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
class ButtonListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e){
String component = e.getActionCommand();
System.out.println("component");
if(component.equals("C")){
textField.setText("");
bil1 =0;
bil2 =0;
jumlah =0;
angka="";
}
else if(component.equals("0")){
angka +="0";
textField.setText(angka);
}
else if(component.equals("1")){
angka +="1";
textField.setText(angka);
}
else if(component.equals("2")){
angka +="2";
textField.setText(angka);
}
else if(component.equals("3")){
angka +="3";
textField.setText(angka);
}
else if(component.equals("4")){
angka +="4";
textField.setText(angka);
}
else if(component.equals("5")){
angka +="5";
textField.setText(angka);
}
else if(component.equals("6")){
angka +="6";
textField.setText(angka);
}
else if(component.equals("7")){
angka +="7";
textField.setText(angka);
}
else if(component.equals("8")){
angka +="8";
textField.setText(angka);
}
else if(component.equals("9")){
angka +="9";
textField.setText(angka);
}
else if(component.equals(".")){
angka +=".";
textField.setText(angka);
}
else if(component.equals("+")){
bil1=Double.parseDouble(angka);
textField.setText("+");
angka="";
pilih=1;
}
else if(component.equals("-")){
bil1=Double.parseDouble(angka);
textField.setText("-");
angka="";
pilih=2;
}
else if(component.equals("x")){
bil1=Double.parseDouble(angka);
textField.setText("x");
angka="";
pilih=3;
}
else if(component.equals("/")){
bil1=Double.parseDouble(angka);
textField.setText("/");
angka="";
pilih=4;
}
else if(component.equals("=")){
switch(pilih){
case 1:
bil2=Double.parseDouble(angka);
jumlah=bil1+bil2;
System.out.println(jumlah);
angka=Double.toString(jumlah);
textField.setText(angka);
break;
case 2:
bil2=Double.parseDouble(angka);
jumlah=bil1-bil2;
System.out.println(jumlah);
angka=Double.toString(jumlah);
textField.setText(angka);
break;
case 3:
bil2=Double.parseDouble(angka);
jumlah=bil1*bil2;
System.out.println(jumlah);
angka=Double.toString(jumlah);
textField.setText(angka);
break;
case 4:
bil2=Double.parseDouble(angka);
jumlah=bil1/bil2;
System.out.println(jumlah);
angka=Double.toString(jumlah);
textField.setText(angka);
break;
}
}
}
}
}
>

Screenshot:

<
Tampilan Awal
Tampilan Tambah

Tampilan Kurang
Tampil Perkalian

Tampil Pembagian

>

You might also like