import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.awt.*;
public class uas extends JFrame{
private JDesktopPane deskPane;
private JButton btn1,btn2,btn3,btn4,btn5,btn6,btn7,
btn8,btn9,btndot,btn0,btn00,btnkl,btnbg,btnkr,btnhsl,btnc,btntb;
private JTextField txt;
private JLabel lblaw,lblnm,lblnim,lbloff,lblak;
ArrayList al;
ArrayList op;
Container cc;
boolean clear;
double operand1;
double operand2;
String operator;
double result;
public uas() {
super("Program UAS");
JMenuBar mnuBar = new JMenuBar();
JMenu mnuapp = new JMenu("Application");
JMenuItem mnucal = new JMenuItem("Calculator");
JMenuItem mnuex = new JMenuItem("Keluar");
mnuapp.add(mnucal);
mnuapp.add(mnuex);
JMenu mnuhlp = new JMenu("Help");
JMenuItem mnuinf = new JMenuItem("About");
mnuhlp.add(mnuinf);
mnuBar.add(mnuapp);
mnuBar.add(mnuhlp);
setJMenuBar(mnuBar);
deskPane = new JDesktopPane();
// Menambahkan desktop pane ke frame
add(deskPane);
mnucal.addActionListener(new Calculator());
mnuex.addActionListener(new Exit());
mnuinf.addActionListener(new About());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(610, 300);
setVisible(true);
}
class Calculator implements ActionListener {
public void actionPerformed(ActionEvent e) {
JInternalFrame kalkul = new JInternalFrame(
"Calculator ", true, true, true, true );
cc=getContentPane();
al=new ArrayList();
op=new ArrayList();
txt=new JTextField(20);
btn1=new JButton("1");
btn2=new JButton("2");
btn3=new JButton("3");
btn4=new JButton("4");
btn5=new JButton("5");
btn6=new JButton("6");
btn7=new JButton("7");
btn8=new JButton("8");
btn9=new JButton("9");
btn0=new JButton("0");
btn00=new JButton("00");
btndot=new JButton(".");
btnkl=new JButton("*");
btnbg=new JButton("/");
btnkr=new JButton("-");
btnhsl=new JButton("=");
btnc=new JButton("c");
btntb=new JButton("+");
JPanel angk=new JPanel();
angk.setLayout(new GridLayout(4,3));
al.add(angk.add(btn1));
al.add(angk.add(btn2));
al.add(angk.add(btn3));
al.add(angk.add(btn4));
al.add(angk.add(btn5));
al.add(angk.add(btn6));
al.add(angk.add(btn7));
al.add(angk.add(btn8));
al.add(angk.add(btn9));
al.add(angk.add(btn0));
al.add(angk.add(btn00));
al.add(angk.add(btndot));
JPanel ops=new JPanel();
ops.setLayout(new GridLayout(3,2));
op.add(ops.add(btnkl));
op.add(ops.add(btnbg));
op.add(ops.add(btnkr));
op.add(ops.add(btnhsl));
op.add(ops.add(btnc));
op.add(ops.add(btntb));
kalkul.add(txt, "North");
kalkul.add(angk, "West");
kalkul.add(ops, "East");
kalkul.setSize(240, 240);
kalkul.setLocation(0,0);
deskPane.add(kalkul );
kalkul.setVisible(true);
Iterator i = al.iterator();
while(i.hasNext()){
JButton temp=(JButton)i.next();
temp.addActionListener(new BtnListener());
}
Iterator j = op.iterator();
while(j.hasNext()){
JButton temp=(JButton)j.next();
temp.addActionListener(new OpListener());
}
btnhsl.addActionListener(new ResListener());
}
}
class Exit implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
class About implements ActionListener {
public void actionPerformed(ActionEvent e) {
JInternalFrame abt = new JInternalFrame(
"About", true, true, true, true );
lblaw=new JLabel("Program ini dibuat oleh:");
lblnm=new JLabel("Nama : Baiq Fiya Hidayani Azhar");
lblnim=new JLabel("NIM : 208533414714");
lbloff=new JLabel("Off : D");
lblak=new JLabel("Untuk UAS praktikum Matakuliah Pemrograman Visual");
JPanel info=new JPanel();
info.setLayout(new GridLayout(5,1));
info.add(lblaw);
info.add(lblnm);
info.add(lblnim);
info.add(lbloff);
info.add(lblak);
JLabel img=new JLabel(new ImageIcon("a.GIF"));
abt.add(info, "Center");
abt.add(img, "East");
abt.setSize(360, 240);
abt.setLocation(240,120/122);
deskPane.add(abt);
abt.setVisible(true);
}
}
class BtnListener implements ActionListener{
public void actionPerformed(ActionEvent ae){
String action = ae.getActionCommand();
if(clear) {
txt.setText("");
clear = false;
}
if (action.equals(".")){
if (txt.getText().indexOf(".")<0){ opr =" ae.getActionCommand();" operator="+" operator="-" operator="/" operator="*" operand1 =" Double.parseDouble(txt.getText());" clear =" true;" opr =" ae.getActionCommand();" operand2 =" Double.parseDouble(txt.getText());" result="operand1+operand2;" result="operand1-operand2;" result="operand1/operand2;" result="operand1*operand2;" clear="true;">
Kalkulator menggunakan Program Java
Posted by Fiya Phyong
Sabtu, Maret 13, 2010, under
Kuliah
|
0
komentar
0 Responses So Far: