Reputation: 1
I have developed the calc using swing but i have a problem in equal to button. I have written the code for addition,subtraction,multiplication and division in the equal to button but the problem is that when i subtract two numbers I get the o/p but when I add or multiply or divide I don't get the o/p. So help me in the code of equal to button:
package com.dss;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JEditorPane;
import javax.swing.SwingConstants;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.BevelBorder;
public class JCalcEx extends JFrame {
private JPanel contentPane;
private JTextField value1;
private JTextField value2;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JCalcEx frame = new JCalcEx();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public JCalcEx() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 395, 680);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
value1 = new JTextField();
value1.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null,
null, null));
value1.setHorizontalAlignment(SwingConstants.RIGHT);
value1.setFont(new Font("Tahoma", Font.PLAIN, 20));
value1.setBounds(12, 69, 350, 41);
contentPane.add(value1);
value1.setColumns(10);
JTextField value3 = new JTextField();
value3.setHorizontalAlignment(SwingConstants.RIGHT);
value3.setFont(new Font("Tahoma", Font.PLAIN, 20));
value3.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null,
null, null));
value3.setBounds(12, 111, 353, 41);
contentPane.add(value3);
value3.setColumns(10);
JButton button9 = new JButton("9");
button9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
value1.setText(value1.getText()+9);
}
});
button9.setFont(new Font("Tahoma", Font.PLAIN, 20));
button9.setBounds(12, 192, 69, 70);
contentPane.add(button9);
JButton button8 = new JButton("8");
button8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0){
value1.setText(value1.getText()+8);
}
});
button8.setFont(new Font("Tahoma", Font.PLAIN, 20));
button8.setBounds(105, 192, 69, 70);
contentPane.add(button8);
JButton button7 = new JButton("7");
button7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
value1.setText(value1.getText()+7);
}
});
button7.setFont(new Font("Tahoma", Font.PLAIN, 20));
button7.setBounds(199, 192, 69, 70);
contentPane.add(button7);
JButton button6 = new JButton("6");
button6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
value1.setText(value1.getText()+6);
}
});
button6.setFont(new Font("Tahoma", Font.PLAIN, 20));
button6.setBounds(293, 192, 69, 70);
contentPane.add(button6);
JButton button5 = new JButton("5");
button5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
value1.setText(value1.getText()+5);
}
});
button5.setFont(new Font("Tahoma", Font.PLAIN, 20));
button5.setBounds(12, 293, 69, 70);
contentPane.add(button5);
JButton button4 = new JButton("4");
button4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
value1.setText(value1.getText()+4);
}
});
button4.setFont(new Font("Tahoma", Font.PLAIN, 20));
button4.setBounds(105, 293, 69, 70);
contentPane.add(button4);
JButton button3 = new JButton("3");
button3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
value1.setText(value1.getText()+3);
}
});
button3.setFont(new Font("Tahoma", Font.PLAIN, 20));
button3.setBounds(199, 293, 69, 70);
contentPane.add(button3);
JButton button2 = new JButton("2");
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
value1.setText(value1.getText()+2);
}
});
button2.setFont(new Font("Tahoma", Font.PLAIN, 20));
button2.setBounds(293, 293, 69, 70);
contentPane.add(button2);
JButton button1 = new JButton("1");
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
value1.setText(value1.getText()+1);
}
});
button1.setFont(new Font("Tahoma", Font.PLAIN, 20));
button1.setBounds(12, 392, 69, 70);
contentPane.add(button1);
JButton buttonc = new JButton("C");
buttonc.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
value2.setText("");
value1.setText("");
actionRecieved.setText("");
value3.setText("");
}
});
buttonc.setFont(new Font("Tahoma", Font.PLAIN, 20));
buttonc.setBounds(293, 392, 69, 70);
contentPane.add(buttonc);
JButton button0 = new JButton("0");
button0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
value1.setText(value1.getText()+0);
}
});
button0.setFont(new Font("Tahoma", Font.PLAIN, 20));
button0.setBounds(105, 392, 69, 70);
contentPane.add(button0);
JButton buttoneql = new JButton("=");
buttoneql.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int one = Integer.parseInt(value2.getText());
int two = Integer.parseInt(value1.getText());
if(actionRecieved.getText().equals("sub"))
{
int sub = one - two;
value3.setText(String.valueOf(sub));
}
if(actionRecieved.getText().equals("add"))
{
int add = one + two;
value3.setText(String.valueOf(add));
}
if(actionRecieved.getText().equals("mul"))
{
int mul = one * two;
value1.setText(String.valueOf(mul));
}
if(actionRecieved.getText().equals("div"))
{
int div = one / two;
value1.setText(String.valueOf(div));
}
}
});
buttoneql.setFont(new Font("Tahoma", Font.PLAIN, 20));
buttoneql.setBounds(199, 392, 69, 70);
contentPane.add(buttoneql);
JButton buttonplus = new JButton("+");
buttonplus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
value2.setText(value1.getText());
actionRecieved.setText("plus");
value1.setText(null);
}
});
buttonplus.setFont(new Font("Tahoma", Font.PLAIN, 20));
buttonplus.setBounds(12, 489, 69, 70);
contentPane.add(buttonplus);
JButton buttonminus = new JButton("-");
buttonminus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
value2.setText(value1.getText());
actionRecieved.setText("sub");
value1.setText(null);
}
});
buttonminus.setFont(new Font("Tahoma", Font.PLAIN, 20));
buttonminus.setBounds(105, 489, 69, 70);
contentPane.add(buttonminus);
JButton buttonmultiply = new JButton("x");
buttonmultiply.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
value2.setText(value1.getText());
actionRecieved.setText("mul");
value1.setText(null);
}
});
buttonmultiply.setFont(new Font("Tahoma", Font.PLAIN, 20));
buttonmultiply.setBounds(199, 489, 69, 70);
contentPane.add(buttonmultiply);
JButton buttondivide = new JButton("/");
buttondivide.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
value2.setText(value1.getText());
actionRecieved.setText("div");
value1.setText(null);
}
});
buttondivide.setFont(new Font("Tahoma", Font.PLAIN, 20));
buttondivide.setBounds(293, 489, 69, 70);
contentPane.add(buttondivide);
value2 = new JTextField();
value2.setFont(new Font("Tahoma", Font.PLAIN, 20));
value2.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null,
null, null));
value2.setHorizontalAlignment(SwingConstants.RIGHT);
value2.setBounds(229, 29, 133, 41);
contentPane.add(value2);
value2.setColumns(10);
JEditorPane editorPane = new JEditorPane();
editorPane.setEditable(false);
editorPane.setBounds(12, 29, 350, 81);
contentPane.add(editorPane);
actionRecieved = new JLabel("");
actionRecieved.setBounds(12, 572, 118, 48);
contentPane.add(actionRecieved);
}
private JLabel actionRecieved;
}
Upvotes: 0
Views: 369
Reputation: 44813
You have the following problems
int div = one - two;
- should be /
actionRecieved.setText("plus");
but expect add
value1
and in others value3
Upvotes: 4