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
|
|
Wed May 15, 2024 10:50 am by ali001
» Fort Lock
Sat May 04, 2024 11:49 am by ali001
» house disrepair claims
Thu May 02, 2024 4:29 pm by Ibad Khan Buledi
» Bounce n Bang: Physics puzzler
Thu May 02, 2024 11:39 am by ali001
» AIChatSY - AIChatbot Assistant
Wed May 01, 2024 10:48 am by ali001
» Storybook Magic App
Tue Apr 30, 2024 7:06 pm by ali001
» Flower Book Match3 Puzzle Game
Tue Apr 30, 2024 12:17 pm by ali001
» Avian Influenza Symptom in Chickens "Bird Flu H5N1 Virus" Vet learning materials, Poultry Farming
Sat Apr 27, 2024 9:57 am by Ibad Khan Buledi
» Aloha Planner - Note-Taker
Thu Apr 11, 2024 4:52 pm by ali001