เครื่องคิดเลขด้วย awt java
Size:2737 Byte Create: 31/1/2548 Time: 18:32:18

import java.awt.*;
import java.awt.event.*;
public class calc implements ActionListener {
Frame s = new Frame("Screen");
TextField txt = new TextField(10);
TextField txtkeep = new TextField(10);
TextField txtsign = new TextField(10);
Button bclose = new Button("Exit");
Button b1 = new Button("1");
Button b2 = new Button("2");
Button b3 = new Button("3");
Button beq = new Button("=");
Button bpl = new Button(" ");
Button bml = new Button("*");
public static void main(String[] args) {
new calc().init();
}
public void init( ) {
s.setSize(300,300);
s.setLayout(null);
s.setVisible(true);
s.setBackground(Color.yellow);
txt.setBounds(80,40,70,20); s.add(txt); txt.addActionListener(this);
txtkeep.setBounds(80,60,70,20); s.add(txtkeep); txtkeep.addActionListener(this);
txtsign.setBounds(80,80,70,20); s.add(txtsign); txtsign.addActionListener(this);
bclose.setBounds(10,40,70,20); s.add(bclose); bclose.addActionListener(this);
b1.setBounds(10,60,70,20); s.add(b1); b1.addActionListener(this);
b2.setBounds(10,80,70,20); s.add(b2); b2.addActionListener(this);
b3.setBounds(10,100,70,20); s.add(b3); b3.addActionListener(this);
beq.setBounds(10,200,70,20); s.add(beq); beq.addActionListener(this);
bpl.setBounds(10,220,70,20); s.add(bpl); bpl.addActionListener(this);
bml.setBounds(10,240,70,20); s.add(bml); bml.addActionListener(this);
s.show();
}
public void actionPerformed(ActionEvent a) {
if(a.getSource()==bclose) { System.exit(0); }
if(a.getSource()==b1) {
String aa = txt.getText() "1";
txt.setText(aa);
}
if(a.getSource()==b2) {
String aa = txt.getText() "2";
txt.setText(aa);
}
if(a.getSource()==b3) {
String aa = txt.getText() "3";
txt.setText(aa);
}
if(a.getSource()==bpl) {
String aa = txt.getText();
txtkeep.setText(aa);
txt.setText("0");
txtsign.setText(" ");
}
if(a.getSource()==bml) {
String aa = txt.getText();
txtkeep.setText(aa);
txt.setText("0");
txtsign.setText("*");
}
if(a.getSource()==beq) {
String bb = txtsign.getText();
String aa = "";
if (bb.equals(" ")) {
aa = Integer.toString(Integer.parseInt(txt.getText()) Integer.parseInt(txtkeep.getText()));
}
if (bb.equals("*")) {
aa = Integer.toString(Integer.parseInt(txt.getText()) * Integer.parseInt(txtkeep.getText()));
}
txt.setText(aa);
}
}
}