Created
May 31, 2012 16:44
-
-
Save anonymous/2844669 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @(#)calculator.java | |
* | |
* | |
* @author | |
* @version 1.00 2012/4/26 | |
*/ | |
import java.awt.*; | |
import java.awt.event.*; | |
import javax.swing.*; | |
public class calculator { | |
/** | |
* Creates a new instance of <code>calculator</code>. | |
*/ | |
public calculator() { | |
} | |
public static class formchinh extends Frame implements ActionListener, ItemListener | |
{ | |
Label lbm=new Label(""); | |
JTextField tf=new JTextField(); | |
Button btce = new Button("CE"); | |
Button btc = new Button("C"); | |
Button btam = new Button("+/-"); | |
Button btcham = new Button("."); | |
Button btcong = new Button("+"); | |
Button bttru = new Button("-"); | |
Button btnhan = new Button("*"); | |
Button btchia = new Button("/"); | |
Button btcan = new Button("sqrt"); | |
Button btphantram = new Button("%"); | |
Button bt1tren = new Button("1/x"); | |
Button btbang = new Button("="); | |
Button bt0 = new Button("0"); | |
Button bt1 = new Button("1"); | |
Button bt2 = new Button("2"); | |
Button bt3 = new Button("3"); | |
Button bt4 = new Button("4"); | |
Button bt5 = new Button("5"); | |
Button bt6 = new Button("6"); | |
Button bt7 = new Button("7"); | |
Button bt8 = new Button("8"); | |
Button bt9 = new Button("9"); | |
GridBagLayout gb=new GridBagLayout(); | |
GridBagConstraints gbc=new GridBagConstraints(); | |
Double a=0.0,b=0.0; | |
String pheptinh=""; | |
public formchinh(String title) | |
{ | |
super(title); | |
setLayout(gb); | |
this.addWindowListener | |
( | |
new WindowAdapter() { | |
public void windowClosing(WindowEvent e) { | |
System.exit(1); | |
} | |
} | |
); | |
gbc.fill=GridBagConstraints.HORIZONTAL; | |
addComponent(tf,0,0,6,1); tf.setHorizontalAlignment(JTextField.RIGHT); | |
gbc.fill=GridBagConstraints.HORIZONTAL; | |
addComponent(lbm,0,1,1,1); | |
btce.addActionListener(this); | |
gbc.fill=GridBagConstraints.HORIZONTAL; | |
addComponent(btce,3,1,2,1); | |
btc.addActionListener(this); | |
gbc.fill=GridBagConstraints.HORIZONTAL; | |
addComponent(btc,5,1,2,1); | |
bt7.addActionListener(this); | |
gbc.fill=GridBagConstraints.HORIZONTAL; | |
addComponent(bt7,1,2,1,1); | |
bt8.addActionListener(this); | |
gbc.fill=GridBagConstraints.HORIZONTAL; | |
addComponent(bt8,2,2,1,1); | |
bt9.addActionListener(this); | |
gbc.fill=GridBagConstraints.HORIZONTAL; | |
addComponent(bt9,3,2,1,1); | |
btchia.addActionListener(this); | |
gbc.fill=GridBagConstraints.HORIZONTAL; | |
addComponent(btchia,4,2,1,1); | |
btcan.addActionListener(this); | |
gbc.fill=GridBagConstraints.HORIZONTAL; | |
addComponent(btcan,5,2,1,1); | |
bt4.addActionListener(this); | |
gbc.fill=GridBagConstraints.HORIZONTAL; | |
addComponent(bt4,1,3,1,1); | |
bt5.addActionListener(this); | |
gbc.fill=GridBagConstraints.HORIZONTAL; | |
addComponent(bt5,2,3,1,1); | |
bt6.addActionListener(this); | |
gbc.fill=GridBagConstraints.HORIZONTAL; | |
addComponent(bt6,3,3,1,1); | |
btnhan.addActionListener(this); | |
gbc.fill=GridBagConstraints.HORIZONTAL; | |
addComponent(btnhan,4,3,1,1); | |
btphantram.addActionListener(this); | |
gbc.fill=GridBagConstraints.HORIZONTAL; | |
addComponent(btphantram,5,3,1,1); | |
bt1.addActionListener(this); | |
gbc.fill=GridBagConstraints.HORIZONTAL; | |
addComponent(bt1,1,4,1,1); | |
bt2.addActionListener(this); | |
gbc.fill=GridBagConstraints.HORIZONTAL; | |
addComponent(bt2,2,4,1,1); | |
bt3.addActionListener(this); | |
gbc.fill=GridBagConstraints.HORIZONTAL; | |
addComponent(bt3,3,4,1,1); | |
bttru.addActionListener(this); | |
gbc.fill=GridBagConstraints.HORIZONTAL; | |
addComponent(bttru,4,4,1,1); | |
bt1tren.addActionListener(this); | |
gbc.fill=GridBagConstraints.HORIZONTAL; | |
addComponent(bt1tren,5,4,1,1); | |
bt0.addActionListener(this); | |
gbc.fill=GridBagConstraints.HORIZONTAL; | |
addComponent(bt0,1,5,1,1); | |
btam.addActionListener(this); | |
gbc.fill=GridBagConstraints.HORIZONTAL; | |
addComponent(btam,2,5,1,1); | |
btcham.addActionListener(this); | |
gbc.fill=GridBagConstraints.HORIZONTAL; | |
addComponent(btcham,3,5,1,1); | |
btcong.addActionListener(this); | |
gbc.fill=GridBagConstraints.HORIZONTAL; | |
addComponent(btcong,4,5,1,1); | |
btbang.addActionListener(this); | |
gbc.fill=GridBagConstraints.HORIZONTAL; | |
addComponent(btbang,5,5,1,1); | |
setSize(200,200); | |
} | |
public void addComponent(Component c, int col, int row, int ncol, int nrow) | |
{ | |
gbc.gridx=col; | |
gbc.gridy=row; | |
gbc.gridwidth=ncol; | |
gbc.gridheight=nrow; | |
gb.setConstraints(c,gbc); | |
add(c); | |
} | |
public void actionPerformed(ActionEvent ae) | |
{ | |
if(ae.getSource()==bt0) tf.setText(tf.getText()+"0"); | |
if(ae.getSource()==bt1) tf.setText(tf.getText()+"1"); | |
if(ae.getSource()==bt2) tf.setText(tf.getText()+"2"); | |
if(ae.getSource()==bt3) tf.setText(tf.getText()+"3"); | |
if(ae.getSource()==bt4) tf.setText(tf.getText()+"4"); | |
if(ae.getSource()==bt5) tf.setText(tf.getText()+"5"); | |
if(ae.getSource()==bt6) tf.setText(tf.getText()+"6"); | |
if(ae.getSource()==bt7) tf.setText(tf.getText()+"7"); | |
if(ae.getSource()==bt8) tf.setText(tf.getText()+"8"); | |
if(ae.getSource()==bt9) tf.setText(tf.getText()+"9"); | |
if(ae.getSource()==btcham) if(!tf.getText().contains(".")) tf.setText(tf.getText()+"."); | |
if(ae.getSource()==btcong) {pheptinh="+"; if(a==0.0)a=Double.parseDouble(tf.getText()); tf.setText("");} | |
if(ae.getSource()==bttru) {pheptinh="-"; if(a==0.0)a=Double.parseDouble(tf.getText()); tf.setText("");} | |
if(ae.getSource()==btnhan) {pheptinh="*"; if(a==0.0)a=Double.parseDouble(tf.getText()); tf.setText("");} | |
if(ae.getSource()==btchia) {pheptinh="/"; if(a==0.0)a=Double.parseDouble(tf.getText()); tf.setText("");} | |
if(ae.getSource()==btbang) {b=Double.parseDouble(tf.getText()); | |
if(pheptinh=="+") {a=a+b; tf.setText(String.valueOf(a));}; | |
if(pheptinh=="-") {a=a-b; tf.setText(String.valueOf(a));}; | |
if(pheptinh=="*") {a=a*b; tf.setText(String.valueOf(a));}; | |
if(pheptinh=="/") {a=a/b; tf.setText(String.valueOf(a));}; | |
if(tf.getText().endsWith(".0")) tf.setText(tf.getText().substring(0,tf.getText().length()-2)); | |
} | |
if(ae.getSource()==btc) {tf.setText("");} | |
if(ae.getSource()==btce) {pheptinh=""; tf.setText(""); a=0.0; b=0.0;} | |
if(ae.getSource()==btcan){tf.setText(String.valueOf(Math.sqrt(Double.parseDouble(tf.getText()))));} | |
if(ae.getSource()==bt1tren){tf.setText(String.valueOf(1/Double.parseDouble(tf.getText())));} | |
if(ae.getSource()==btphantram){tf.setText(String.valueOf(Double.parseDouble(tf.getText())/100));} | |
if(ae.getSource()==btam) { | |
tf.setText(String.valueOf(Double.parseDouble(tf.getText())*-1)); | |
if(tf.getText().endsWith(".0")) tf.setText(tf.getText().substring(0,tf.getText().length()-2)); | |
} | |
} | |
public void itemStateChanged(ItemEvent ie) | |
{ | |
} | |
/** | |
* @param args the command line arguments | |
*/ | |
public static void main(String[] args) throws Exception { | |
// TODO code application logic here | |
formchinh fc =new formchinh("CALCULATOR"); | |
fc.show(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment