Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.event.*;
- import java.awt.*;
- class Kalkulator implements ActionListener{
- //=====================================
- private final byte NULL=9;
- private final byte TAMBAH=0;
- private final byte KURANG=1;
- private final byte KALI=2;
- private final byte BAGI=3;
- //=====================================
- private final JFrame mainF;
- private JPanel io;
- private JPanel inputP;
- private JPanel operatorP;
- private JButton[]angka;
- private JButton[]operator;
- private JTextField in;
- private JTextField out;
- //=====================================
- private double inNum=0.0;
- private double hasil=0.0;
- private byte lastOperator=NULL;
- Kalkulator(){
- setup();
- mainF=new JFrame("Kalkulator");
- mainF.add(inputP);
- mainF.add(operatorP);
- mainF.add(io);
- mainF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- mainF.setSize(500, 500);
- mainF.setLayout(null);
- mainF.setResizable(false);
- mainF.setLocationByPlatform(true);
- mainF.setVisible(true);
- }
- private void setup(){
- //Setup Panel
- inputP=new JPanel();
- inputP.setBounds(5,50,180,115);
- inputP.setLayout(null);
- inputP.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 3));
- operatorP=new JPanel();
- operatorP.setBounds(190,50,125,115);
- operatorP.setLayout(null);
- operatorP.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 3));
- io=new JPanel();
- io.setBounds(5,5,(190+120),40);
- io.setLayout(null);
- io.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 3));
- /*Setup input*/{
- String[]txt={
- " 1 "," 2 "," 3 "," 4 "," 5 ",
- " 6 "," 7 "," 8 "," 9 "," 0 "
- };
- angka=new JButton[10];
- Rectangle pos=new Rectangle(10,10,50,20);
- for(byte i=0;i<10;i++){
- angka[i]=new JButton(txt[i]);
- angka[i].setBounds(pos);
- if((i+1)%3==0){
- pos.y+=25;
- pos.x-=(55*3);
- }
- pos.x+=55;
- inputP.add(angka[i]);
- angka[i].addActionListener(this);
- }
- }
- /*setup operator*/{
- String[]txt={
- " + "," - "," * "," / ",
- " = ","bs"," C "
- };
- operator=new JButton[7];
- Rectangle pos=new Rectangle(10,10,50,20);
- for(byte i=0;i<4;i++){
- operator[i]=new JButton(txt[i]);
- operator[i].setBounds(pos);
- if((i+1)%2==0){
- pos.y+=25;
- pos.x-=(55*2);
- }
- if(i==6)pos.width+=100;
- pos.x+=55;
- operatorP.add(operator[i]);
- operator[i].addActionListener(this);
- }
- pos=new Rectangle(operator[2].getBounds());
- pos.width=105;
- pos.y+=25;
- operator[4]=new JButton(txt[4]);
- operator[4].setBounds(pos);
- operator[4].addActionListener(this);
- operatorP.add(operator[4]);
- pos.y+=25;
- pos.width=operator[0].getBounds().width;
- for(byte i=5;i<7;i++){
- operator[i]=new JButton(txt[i]);
- operator[i].setBounds(pos);
- operatorP.add(operator[i]);
- operator[i].addActionListener(this);
- pos.x+=55;
- }
- }
- /*setup field text*/{
- Rectangle pos=new Rectangle(5,10,170,20);
- in=new JTextField(100);
- out=new JTextField(100);
- in.setBounds(pos);
- pos=new Rectangle(190,10,115,20);
- out.setBounds(pos);
- io.add(in);
- io.add(out);
- in.setForeground(Color.red);
- //=======================================
- // * Sementara
- //in.setEnabled(false);
- //out.setEnabled(false);
- }
- }
- @Override
- public void actionPerformed(ActionEvent e){
- if(in.getText().equals(""))in.setText("0");
- String input_num=e.getActionCommand();
- //==============================================
- if(input_num.charAt(1)>='0'&&input_num.charAt(1)<='9'){
- in.setText(in.getText()+input_num.charAt(1));
- }
- //==============================================
- if(input_num.equals("bs"))
- in.setText(in.getText().substring(0, in.getText().length()-1));
- //==============================================
- if(input_num.equals(" C ")){
- in.setText("");
- hasil=0;
- lastOperator=NULL;
- }
- //==============================================
- if(input_num.charAt(1)=='+'){
- if(lastOperator==NULL){
- hasil=Double.parseDouble(in.getText());
- }else{
- inNum=Double.parseDouble(in.getText());
- hitung();
- }
- lastOperator=TAMBAH;
- }else
- if(input_num.charAt(1)=='-'){
- if(lastOperator==NULL){
- hasil=Double.parseDouble(in.getText());
- }else{
- inNum=Double.parseDouble(in.getText());
- hitung();
- }
- lastOperator=KURANG;
- }else
- if(input_num.charAt(1)=='*'){
- if(lastOperator==NULL){
- hasil=Double.parseDouble(in.getText());
- }else{
- inNum=Double.parseDouble(in.getText());
- hitung();
- }
- lastOperator=KALI;
- }else
- if(input_num.charAt(1)=='/'){
- if(lastOperator==NULL){
- hasil=Double.parseDouble(in.getText());
- }else{
- inNum=Double.parseDouble(in.getText());
- hitung();
- }
- lastOperator=BAGI;
- }else
- if(input_num.charAt(1)=='='){
- if(lastOperator==NULL)
- hasil=Double.parseDouble(in.getText());
- else
- inNum=Double.parseDouble(in.getText());
- //-----------------------------------------
- hitung();
- Double h=hasil;
- //-----------------------------------------
- out.setText(h.toString());
- hasil=inNum=0;
- in.setText("");
- lastOperator=NULL;
- }
- }
- private void hitung(){
- switch(lastOperator){
- case TAMBAH:
- hasil+=inNum;
- break;
- case KURANG:
- hasil-=inNum;
- break;
- case BAGI:
- hasil/=inNum;
- break;
- case KALI:
- hasil*=inNum;
- break;
- }
- }
- public static void main(String[]args){
- Kalkulator k=new Kalkulator();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement