little error in java code
Virtual Position Forum :: Members Lounge :: Sciences & Knowledge :: Programing and Design :: Java Forum
Page 1 of 1 • Share
little error in java code
- Code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class SmallCalcApp implements ActionListener{
JFrame frame;
JLabel firstOperand, secondOperand, answer;
JTextField op1, op2, ans;
JButton plus, mul;
public void initGUI(){
frame = new JFrame();
firstOperand = new JLabel("First Operand");
secondOperand = new JLabel ("Second Operand");
answer = new JLabel ("Answer");
op1 = new JTextField (15);
op2 = new JTextField (15);
ans = new JTextField (15);
mul = new JButton ("*");
mul.setPreferredSize(new Dimension(70,25));
plus = new JButton ("+");
plus.setPreferredSize(new Dimension(70,25));
Container con = frame.getContentPane();
con.setLayout(new FlowLayout());
con.add(firstOperand);
con.add(op1);
con.add(secondOperand);
con.add(op2);
con.add(plus);
con.add(ans);
plus.addActionListener(this);
mul.addActionListener(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setSize(200,220);
frame.setVisible(true);
}
public SmallCalcApp(){
initGUI();
}
public void actionPerformed(ActionEvent event){
String oper, result;
int num1, num2, res;
if (event.getSource() == plus){
oper = op1.getText();
num1 = Integer.parseInt(oper);
oper = op2.getText();
num2 = Integer.parseInt(oper);
res = num1 + num2 ;
result = res + "";
ans.setText(result);
}
else if (event.getSource() == mul){
oper = op1.getText();
num1 = Integer.parseInt(oper);
oper = op2.getText();
num2 = Integer.parseInt(oper);
res = num1 * num2;
result = res + "";
ans.setText(result);
}
}
public static void main (String args[]){
SmallCalcApp scApp = new SmallCalcApp();
}
}
sharmila farooqi- Monstars
- Posts : 35
Join date : 2011-05-29
Re: little error in java code
- Code:
Add the multiplication button as well using statement.
con.add(mul);
Corrected Code as below
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class SmallCalcApp implements ActionListener{
JFrame frame;
JLabel firstOperand, secondOperand, answer;
JTextField op1, op2, ans;
JButton plus, mul;
public void initGUI(){
frame = new JFrame();
firstOperand = new JLabel("First Operand");
secondOperand = new JLabel ("Second Operand");
answer = new JLabel ("Answer");
op1 = new JTextField (15);
op2 = new JTextField (15);
ans = new JTextField (15);
mul = new JButton ("*");
mul.setPreferredSize(new Dimension(70,25));
plus = new JButton ("+");
plus.setPreferredSize(new Dimension(70,25));
Container con = frame.getContentPane();
con.setLayout(new FlowLayout());
con.add(firstOperand);
con.add(op1);
con.add(secondOperand);
con.add(op2);
con.add(mul); /*Add the multiplication button to container as well*/
con.add(plus);
con.add(ans);
plus.addActionListener(this);
mul.addActionListener(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,220);
frame.setVisible(true);
}
public SmallCalcApp(){
initGUI();
}
public void actionPerformed(ActionEvent event){
String oper, result;
int num1, num2, res;
if (event.getSource() == plus){
oper = op1.getText();
num1 = Integer.parseInt(oper);
oper = op2.getText();
num2 = Integer.parseInt(oper);
res = num1 + num2 ;
result = res + "";
ans.setText(result);
}
else if (event.getSource() == mul){
oper = op1.getText();
num1 = Integer.parseInt(oper);
oper = op2.getText();
num2 = Integer.parseInt(oper);
res = num1 * num2;
result = res + "";
ans.setText(result);
}
}
public static void main (String args[]){
SmallCalcApp scApp = new SmallCalcApp();
}
Vuhelper- Deep Bench
- Posts : 97
Join date : 2011-05-29
Similar topics
» string java is case senstive error
» need guidness in java code
» Native code in java
» how can we run the java code ? after saving the file how run it?
» WHAT IS PDA in java
» need guidness in java code
» Native code in java
» how can we run the java code ? after saving the file how run it?
» WHAT IS PDA in java
Virtual Position Forum :: Members Lounge :: Sciences & Knowledge :: Programing and Design :: Java Forum
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
Yesterday at 12:21 pm by ali001
» Hemangiom'App
Tue Nov 05, 2024 11:25 am by ali001
» MindfulMe - Mental Health App
Mon Nov 04, 2024 10:50 am by ali001
» Learn Candlestick Patterns
Tue Oct 15, 2024 5:51 am by ali001
» Woh Pagal Si Episode 52 to 62 - Top Pakistani Drama
Sat Sep 21, 2024 6:26 pm by Mir Emmad Ali Khan Domki
» Nearu - share your socials
Sat Sep 21, 2024 1:12 pm by ali001
» Nightclub Tycoon: Idle Empire
Thu Sep 19, 2024 9:16 pm by ali001
» Carnivore - Meat Diet Recipes
Wed Sep 18, 2024 2:37 pm by ali001
» Eid Milad un Nabi Mubarak 2024 (Rabiʻ I 14, 1446 AH)
Tue Sep 17, 2024 3:44 pm by Mir Emmad Ali Khan Domki