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
import java.awt.*; | |
import java.awt.event.*; | |
class Calculate extends Frame implements ActionListener{ | |
TextField tf1,tf2; | |
Label lbResult,lb; | |
Button bt; | |
Choice ch; | |
List lt; | |
CheckboxGroup cg; | |
Checkbox cb1,cb2,cb3,cb4; | |
Calculate(){ | |
setTitle("AWT in Java 16-02-17"); | |
//first text field | |
tf1 = new TextField(); | |
tf1.setBounds(100,100,70,30); | |
//secound text field | |
tf2 = new TextField(); | |
tf2.setBounds(250,100,70,30); | |
lb = new Label(); | |
lb.setBounds(350,90,150,30); | |
//label as a result | |
lbResult = new Label("Result Here"); | |
lbResult.setBounds(350,120,100,30); | |
//choice as add, sub,mul,div | |
ch = new Choice(); | |
ch.setBounds(100,150,60,70); | |
ch.addItem("add"); | |
ch.addItem("sub"); | |
ch.addItem("mul"); | |
ch.addItem("div"); | |
//list as add, sub,mul,div | |
lt = new List(); | |
lt.setBounds(250,150,60,70); | |
lt.add("add"); | |
lt.add("sub"); | |
lt.add("mul"); | |
lt.add("div"); | |
lt.select(0); | |
//check box or group as radio button | |
cg = new CheckboxGroup(); | |
cb1 = new Checkbox("add",true,cg); | |
cb2 = new Checkbox("sub",false,cg); | |
cb3 = new Checkbox("mul",false,cg); | |
cb4 = new Checkbox("div",false,cg); | |
cb1.setBounds(100,200,60,30); | |
cb2.setBounds(100,240,60,30); | |
cb3.setBounds(100,280,60,30); | |
cb4.setBounds(100,320,60,30); | |
bt = new Button("Result"); | |
bt.setBounds(190,400,100,30); | |
bt.addActionListener(this); | |
//adding in frame all component | |
add(tf1); | |
add(tf2); | |
add(bt); | |
add(lb); | |
add(lbResult); | |
add(ch); | |
add(lt); | |
add(cb1); | |
add(cb2); | |
add(cb3); | |
add(cb4); | |
setBackground(Color.YELLOW); | |
setSize(500,500); | |
setLayout(null); | |
setVisible(true); | |
} | |
public void actionPerformed(ActionEvent ae){ | |
int t1,t2; | |
Font f = new Font("Arial",Font.BOLD,16); | |
if(ae.getSource() == bt){ | |
if(tf1.getText().equals("")){ | |
tf1.setText("0"); | |
} | |
if(tf2.getText().equals("")){ | |
tf2.setText("0"); | |
} | |
t1 = Integer.parseInt(tf1.getText()); | |
t2 = Integer.parseInt(tf2.getText()); | |
if(ch.getSelectedItem().equals("add") && lt.getSelectedItem().equals("add") && cb1.getState()==true){ | |
lb.setFont(f); | |
lb.setText("Adding"); | |
lbResult.setBackground(Color.GREEN); | |
lbResult.setText(Integer.toString(t1+t2)); | |
} | |
else if(ch.getSelectedItem().equals("sub") && lt.getSelectedItem().equals("sub") && cb2.getState()==true){ | |
lb.setText("Substruction"); | |
lbResult.setBackground(Color.GREEN); | |
lbResult.setText(Integer.toString(t1-t2)); | |
} | |
else if(ch.getSelectedItem().equals("mul") && lt.getSelectedItem().equals("mul") && cb3.getState()==true){ | |
lb.setText("Multiplication"); | |
lbResult.setBackground(Color.GREEN); | |
lbResult.setText(Integer.toString(t1*t2)); | |
} | |
else if(ch.getSelectedItem().equals("div") && lt.getSelectedItem().equals("div") && cb4.getState()==true){ | |
lbResult.setBackground(Color.GREEN); | |
lb.setText("Divition"); | |
if(t1 == 0 && t2 == 0){ | |
lbResult.setText(" Text box 0"); | |
}else{ | |
lbResult.setText(Integer.toString(t1/t2)); | |
} | |
} | |
else{ | |
lb.setText(""); | |
lbResult.setBackground(Color.RED); | |
lbResult.setText("Wrong Selected"); | |
} | |
} | |
} | |
public static void main(String arg[]){ | |
Calculate calculate = new Calculate(); | |
} | |
} |