import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class CalcGUI extends JFrame implements ActionListener{
public static void main(String args[]){
new CalcGUI();
}
public String bil="",hasil="";
public char operator;
JPanel p1=new JPanel();
JTextField tfbilangan=new JTextField();
JButton b1=new JButton("1");
JButton b2=new JButton("2");
JButton b3=new JButton("3");
JButton b4=new JButton("4");
JButton b5=new JButton("5");
JButton b6=new JButton("6");
JButton b7=new JButton("7");
JButton b8=new JButton("8");
JButton b9=new JButton("9");
JButton b0=new JButton("0");
JPanel p2=new JPanel();
JPanel p3=new JPanel();
JButton btclear=new JButton("C");
JButton btequal=new JButton("=");
JRadioButton rbtambah=new JRadioButton("+");
JRadioButton rbkurang=new JRadioButton("-");
JRadioButton rbbagi=new JRadioButton("/");
JRadioButton rbkali=new JRadioButton("x");
ButtonGroup bg=new ButtonGroup();
JLabel nm=new JLabel("OLEH : XFIRE");
Container c;
public CalcGUI() {
c=getContentPane();
c.setLayout(null);
p1.setLayout(null);
tfbilangan.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
tfbilangan.setEditable(false);
p1.add(rbtambah).reshape(0, 3, 50, 25);
p1.add(rbkurang).reshape(50, 3, 50, 25);
p1.add(rbkali).reshape(100, 3, 50, 25);
p1.add(rbbagi).reshape(150, 3, 40, 25);
bg.add(rbtambah);
bg.add(rbkurang);
bg.add(rbkali);
bg.add(rbbagi);
p2.setLayout(new GridLayout(4, 3, 2, 2));
p2.add(b7);
p2.add(b8);
p2.add(b9);
p2.add(b4);
p2.add(b5);
p2.add(b6);
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(b0);
p2.add(btclear);
p2.add(btequal);
//p2.add(nm).reshape(130, 140, 100, 25);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b0.addActionListener(this);
rbtambah.addActionListener(this);
rbkurang.addActionListener(this);
rbkali.addActionListener(this);
rbbagi.addActionListener(this);
btclear.addActionListener(this);
btequal.addActionListener(this);
c.add(new JLabel("DIRA KALKULATOR")).reshape(25, 10, 190, 30);
c.add(tfbilangan).reshape(25, 35, 190, 30);
c.add(p1).reshape(25, 300, 190, 30);
c.add(p2).reshape(25, 90, 190, 200);
p1.setBackground(Color.DARK_GRAY);
p2.setBackground(Color.DARK_GRAY);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(250,400);
setLocation(200, 200);
setVisible(true);
setResizable(false);
setTitle("Dira Calculator");
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==b1){
if(bil.equals("")){
bil="1";
tfbilangan.setText(bil);
}
else{
bil=bil+"1";
tfbilangan.setText(bil);
}
}
if(e.getSource()==b2){
if(bil.equals("")){
bil="2";
tfbilangan.setText(bil);
}
else{
bil=bil+"2";
tfbilangan.setText(bil);
}
}
if(e.getSource()==b3){
if(bil.equals("")){
bil="3";
tfbilangan.setText(bil);
}
else{
bil=bil+"3";
tfbilangan.setText(bil);
}
}
if(e.getSource()==b4){
if(bil.equals("")){
bil="4";
tfbilangan.setText(bil);
}
else{
bil=bil+"4";
tfbilangan.setText(bil);
}
}
if(e.getSource()==b5){
if(bil.equals("")){
bil="5";
tfbilangan.setText(bil);
}
else{
bil=bil+"5";
tfbilangan.setText(bil);
}
}
if(e.getSource()==b6){
if(bil.equals("")){
bil="6";
tfbilangan.setText(bil);
}
else{
bil=bil+"6";
tfbilangan.setText(bil);
}
}
if(e.getSource()==b7){
if(bil.equals("")){
bil="7";
tfbilangan.setText(bil);
}
else{
bil=bil+"7";
tfbilangan.setText(bil);
}
}
if(e.getSource()==b8){
if(bil.equals("")){
bil="8";
tfbilangan.setText(bil);
}
else{
bil=bil+"8";
tfbilangan.setText(bil);
}
}
if(e.getSource()==b9){
if(bil.equals("")){
bil="9";
tfbilangan.setText(bil);
}
else{
bil=bil+"9";
tfbilangan.setText(bil);
}
}
if(e.getSource()==b0){
if(bil.equals("")){
bil="0";
tfbilangan.setText(bil);
}
else{
bil=bil+"0";
tfbilangan.setText(bil);
}
}
if(e.getSource()==rbtambah){
if(bil.equals("")){
JOptionPane.showMessageDialog(null, "Bilangan belum di masukkan");
}else{
hasil=bil;
bil="";
operator='+';
tfbilangan.setText(hasil+operator);
}
}
if(e.getSource()==rbkurang){
if(bil.equals("")){
JOptionPane.showMessageDialog(null, "Bilangan belum di masukkan");
}else{
hasil=bil;
bil="";
operator='-';
tfbilangan.setText(hasil+operator);
}
}
if(e.getSource()==rbkali){
if(bil.equals("")){
JOptionPane.showMessageDialog(null, "Bilangan belum di masukkan");
}else{
hasil=bil;
bil="";
operator='x';
tfbilangan.setText(hasil+operator);
}
}
if(e.getSource()==rbbagi){
if(bil.equals("")){
JOptionPane.showMessageDialog(null, "Bilangan belum di masukkan");
}else{
hasil=bil;
bil="";
operator='/';
tfbilangan.setText(hasil+operator);
}
}
if(e.getSource()==btclear){
tfbilangan.setText("");
bil="";
}
if(e.getSource()==btequal){
if(bil.equals("")){
JOptionPane.showMessageDialog(null, "Bilangan belum di masukkan");
}else{
switch(operator){
case '+':
try{
tfbilangan.setText(String.valueOf(Double.valueOf(hasil)+Double.valueOf(bil)));
}catch(ArithmeticException ae){
JOptionPane.showMessageDialog(null, "Error");
}
bil="";
hasil="";
break;
case '-':
try{
tfbilangan.setText(String.valueOf(Double.valueOf(hasil)-Double.valueOf(bil)));
}catch(ArithmeticException ae){
JOptionPane.showMessageDialog(null, "Error");
}
bil="";
hasil="";
break;
case '*':
try{
tfbilangan.setText(String.valueOf(Double.valueOf(hasil)*Double.valueOf(bil)));
}catch(ArithmeticException ae){
JOptionPane.showMessageDialog(null, "Error");
}
bil="";
hasil="";
break;
case '/':
try{
tfbilangan.setText(String.valueOf(Double.valueOf(hasil)/Double.valueOf(bil)));
}catch(ArithmeticException ae){
JOptionPane.showMessageDialog(null, "Error");
}
bil="";
hasil="";
break;
}
}
}
}
}
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class CalcGUI extends JFrame implements ActionListener{
public static void main(String args[]){
new CalcGUI();
}
public String bil="",hasil="";
public char operator;
JPanel p1=new JPanel();
JTextField tfbilangan=new JTextField();
JButton b1=new JButton("1");
JButton b2=new JButton("2");
JButton b3=new JButton("3");
JButton b4=new JButton("4");
JButton b5=new JButton("5");
JButton b6=new JButton("6");
JButton b7=new JButton("7");
JButton b8=new JButton("8");
JButton b9=new JButton("9");
JButton b0=new JButton("0");
JPanel p2=new JPanel();
JPanel p3=new JPanel();
JButton btclear=new JButton("C");
JButton btequal=new JButton("=");
JRadioButton rbtambah=new JRadioButton("+");
JRadioButton rbkurang=new JRadioButton("-");
JRadioButton rbbagi=new JRadioButton("/");
JRadioButton rbkali=new JRadioButton("x");
ButtonGroup bg=new ButtonGroup();
JLabel nm=new JLabel("OLEH : XFIRE");
Container c;
public CalcGUI() {
c=getContentPane();
c.setLayout(null);
p1.setLayout(null);
tfbilangan.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
tfbilangan.setEditable(false);
p1.add(rbtambah).reshape(0, 3, 50, 25);
p1.add(rbkurang).reshape(50, 3, 50, 25);
p1.add(rbkali).reshape(100, 3, 50, 25);
p1.add(rbbagi).reshape(150, 3, 40, 25);
bg.add(rbtambah);
bg.add(rbkurang);
bg.add(rbkali);
bg.add(rbbagi);
p2.setLayout(new GridLayout(4, 3, 2, 2));
p2.add(b7);
p2.add(b8);
p2.add(b9);
p2.add(b4);
p2.add(b5);
p2.add(b6);
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(b0);
p2.add(btclear);
p2.add(btequal);
//p2.add(nm).reshape(130, 140, 100, 25);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b0.addActionListener(this);
rbtambah.addActionListener(this);
rbkurang.addActionListener(this);
rbkali.addActionListener(this);
rbbagi.addActionListener(this);
btclear.addActionListener(this);
btequal.addActionListener(this);
c.add(new JLabel("DIRA KALKULATOR")).reshape(25, 10, 190, 30);
c.add(tfbilangan).reshape(25, 35, 190, 30);
c.add(p1).reshape(25, 300, 190, 30);
c.add(p2).reshape(25, 90, 190, 200);
p1.setBackground(Color.DARK_GRAY);
p2.setBackground(Color.DARK_GRAY);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(250,400);
setLocation(200, 200);
setVisible(true);
setResizable(false);
setTitle("Dira Calculator");
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==b1){
if(bil.equals("")){
bil="1";
tfbilangan.setText(bil);
}
else{
bil=bil+"1";
tfbilangan.setText(bil);
}
}
if(e.getSource()==b2){
if(bil.equals("")){
bil="2";
tfbilangan.setText(bil);
}
else{
bil=bil+"2";
tfbilangan.setText(bil);
}
}
if(e.getSource()==b3){
if(bil.equals("")){
bil="3";
tfbilangan.setText(bil);
}
else{
bil=bil+"3";
tfbilangan.setText(bil);
}
}
if(e.getSource()==b4){
if(bil.equals("")){
bil="4";
tfbilangan.setText(bil);
}
else{
bil=bil+"4";
tfbilangan.setText(bil);
}
}
if(e.getSource()==b5){
if(bil.equals("")){
bil="5";
tfbilangan.setText(bil);
}
else{
bil=bil+"5";
tfbilangan.setText(bil);
}
}
if(e.getSource()==b6){
if(bil.equals("")){
bil="6";
tfbilangan.setText(bil);
}
else{
bil=bil+"6";
tfbilangan.setText(bil);
}
}
if(e.getSource()==b7){
if(bil.equals("")){
bil="7";
tfbilangan.setText(bil);
}
else{
bil=bil+"7";
tfbilangan.setText(bil);
}
}
if(e.getSource()==b8){
if(bil.equals("")){
bil="8";
tfbilangan.setText(bil);
}
else{
bil=bil+"8";
tfbilangan.setText(bil);
}
}
if(e.getSource()==b9){
if(bil.equals("")){
bil="9";
tfbilangan.setText(bil);
}
else{
bil=bil+"9";
tfbilangan.setText(bil);
}
}
if(e.getSource()==b0){
if(bil.equals("")){
bil="0";
tfbilangan.setText(bil);
}
else{
bil=bil+"0";
tfbilangan.setText(bil);
}
}
if(e.getSource()==rbtambah){
if(bil.equals("")){
JOptionPane.showMessageDialog(null, "Bilangan belum di masukkan");
}else{
hasil=bil;
bil="";
operator='+';
tfbilangan.setText(hasil+operator);
}
}
if(e.getSource()==rbkurang){
if(bil.equals("")){
JOptionPane.showMessageDialog(null, "Bilangan belum di masukkan");
}else{
hasil=bil;
bil="";
operator='-';
tfbilangan.setText(hasil+operator);
}
}
if(e.getSource()==rbkali){
if(bil.equals("")){
JOptionPane.showMessageDialog(null, "Bilangan belum di masukkan");
}else{
hasil=bil;
bil="";
operator='x';
tfbilangan.setText(hasil+operator);
}
}
if(e.getSource()==rbbagi){
if(bil.equals("")){
JOptionPane.showMessageDialog(null, "Bilangan belum di masukkan");
}else{
hasil=bil;
bil="";
operator='/';
tfbilangan.setText(hasil+operator);
}
}
if(e.getSource()==btclear){
tfbilangan.setText("");
bil="";
}
if(e.getSource()==btequal){
if(bil.equals("")){
JOptionPane.showMessageDialog(null, "Bilangan belum di masukkan");
}else{
switch(operator){
case '+':
try{
tfbilangan.setText(String.valueOf(Double.valueOf(hasil)+Double.valueOf(bil)));
}catch(ArithmeticException ae){
JOptionPane.showMessageDialog(null, "Error");
}
bil="";
hasil="";
break;
case '-':
try{
tfbilangan.setText(String.valueOf(Double.valueOf(hasil)-Double.valueOf(bil)));
}catch(ArithmeticException ae){
JOptionPane.showMessageDialog(null, "Error");
}
bil="";
hasil="";
break;
case '*':
try{
tfbilangan.setText(String.valueOf(Double.valueOf(hasil)*Double.valueOf(bil)));
}catch(ArithmeticException ae){
JOptionPane.showMessageDialog(null, "Error");
}
bil="";
hasil="";
break;
case '/':
try{
tfbilangan.setText(String.valueOf(Double.valueOf(hasil)/Double.valueOf(bil)));
}catch(ArithmeticException ae){
JOptionPane.showMessageDialog(null, "Error");
}
bil="";
hasil="";
break;
}
}
}
}
}
0 komentar:
Post a Comment