Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JFrame Calculator. Help I'm a beginner.

866338Jun 2 2011 — edited Jun 3 2011
I need the calculator to add more than just single digit numbers. I need it to add at least three digit numbers. All it can do right now is do the 4 basic functions with just single-digit numbers. Please help me!!!!!









import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

@SuppressWarnings("serial")
public class Calculator extends JFrame
{
double sum=0;
double subtraction=0;
double division=0;
double multiplication=0;
@SuppressWarnings("unused")
private final Font BIGGER_FONT = new Font("TimesNewRoman", Font.PLAIN, 20);
JTextField textField1;
private JMenu jmenuFile, jmenuHelp;
private JMenuItem jmenuitemExit, jmenuitemAbout;


public static void main(String args[])
{

Calculator c = new Calculator();
@SuppressWarnings("unused")
JFrame f = new JFrame("Calculator");
Container lm = c.getContentPane();
c.setSize(800,500);
c.setLocation(100,100);
lm.setBackground(Color.blue);
c.setVisible(true);

c.setTitle("Calculator");


}

public Calculator()
{
setLayout(new GridLayout(4,4,4,4));

Font textFieldfont = new Font("Times New Roman", 0, 20);

textField1 = new JTextField();
add(textField1);
textField1.setSize(500,500);
textField1.setBounds(3, 3, 300, 200);
textField1.setFont(textFieldfont);

Font f12 = new Font("Times New Roman", 0, 12);
Font f121 = new Font("Times New Roman", 1, 12);

jmenuFile = new JMenu("File");
jmenuFile.setFont(f121);
jmenuFile.setMnemonic(KeyEvent.VK_F);

jmenuitemExit = new JMenuItem("Exit");
jmenuitemExit.setFont(f12);
jmenuitemExit.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_X,
ActionEvent.CTRL_MASK));
jmenuFile.add(jmenuitemExit);

jmenuHelp = new JMenu("Help");
jmenuHelp.setFont(f121);
jmenuHelp.setMnemonic(KeyEvent.VK_H);

jmenuitemAbout = new JMenuItem("About Calculator");
jmenuitemAbout.setFont(f12);


jmenuHelp.add("Me");
JMenuBar ac = new JMenuBar();
ac.add(jmenuFile);
ac.add(jmenuHelp);
setJMenuBar(ac);

//This marks the beginning of the addition of button into my calculator.

Font bigf = new Font("Times New Roman", 0, 23);
Font dotFont = new Font("Times New Roman", 0, 40);
setSize(100,150);
JButton num0 = new JButton("0");
add(num0);
num0.setFont(bigf);
num0.addActionListener(new bListener());
num0.setLayout(new GridLayout(1,6));

setSize(100,150);
JButton num1 = new JButton("1");
add(num1);
num1.setFont(bigf);
num1.addActionListener(new bListener());
num1.setLayout(new GridLayout(0,1));

setSize(100,150);
JButton num2 = new JButton("2");
add(num2);
num2.setFont(bigf);
num2.addActionListener(new bListener());
num2.setLayout(new GridLayout(0,2));

setSize(100,150);
JButton num3 = new JButton("3");
add(num3);
num3.setFont(bigf);
num3.addActionListener(new bListener());
num3.setLayout(new GridLayout(0,3));

setSize(100,150);
JButton num4 = new JButton("4");
add(num4);
num4.setFont(bigf);
num4.addActionListener(new bListener());
num4.setLayout(new GridLayout(0,4));


setSize(100,150);
JButton num5 = new JButton("5");
add(num5);
num5.setFont(bigf);
num5.addActionListener(new bListener());
num5.setLayout(new GridLayout(0,5));

setSize(100,150);
JButton num6 = new JButton("6");
add(num6);
num6.setFont(bigf);
num6.addActionListener(new bListener());
num6.setLayout(new GridLayout(0,6));

setSize(100,150);
JButton num7 = new JButton("7");
add(num7);
num7.setFont(bigf);
num7.addActionListener(new bListener());
num7.setLayout(new GridLayout(0,7));

setSize(100,150);
JButton num8 = new JButton("8");
add(num8);
num8.setFont(bigf);
num8.addActionListener(new bListener());
num8.setLayout(new GridLayout(0,8));

setSize(100,150);
JButton num9 = new JButton("9");
add(num9);
num9.setFont(bigf);
num9.addActionListener(new bListener());
num9.setLayout(new GridLayout(1,0));

setSize(100,150);
JButton add = new JButton("+");
add(add);
add.setFont(bigf);
add.addActionListener(new bListener());
add.setLayout(new GridLayout(1,1));

setSize(100,150);
JButton minus = new JButton("-");
add(minus);
minus.setFont(bigf);
minus.addActionListener(new bListener());
minus.setLayout(new GridLayout(1,2));

setSize(100,150);
JButton multiply = new JButton("*");
add(multiply);
multiply.setFont(bigf);
multiply.addActionListener(new bListener());
multiply.setLayout(new GridLayout(1,3));

setSize(100,150);
JButton divide = new JButton("/");
add(divide);
divide.setFont(bigf);
divide.addActionListener(new bListener());
divide.setLayout(new GridLayout(1,4));

setSize(100,150);
JButton enter = new JButton("=");
add(enter);
enter.setFont(bigf);
enter.addActionListener(new EnterListener());
enter.setLayout(new GridLayout(2,0));

setSize(100,150);
JButton deleteB = new JButton("C");
add(deleteB);
deleteB.setFont(bigf);
deleteB.addActionListener(new bListener());
deleteB.setLayout(new GridLayout(2,0));

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

//User Interface code ends here
}

class bListener implements ActionListener //Button listener to show text in JTextField and provide the C button with a clear function.
{
public void actionPerformed(ActionEvent e) {
JButton temp = (JButton)e.getSource();
String btext = temp.getText();
String tftext = textField1.getText();

if(btext.equals("C"))
textField1.setText("");
else
textField1.setText(tftext + btext);
}

}
private class EnterListener implements ActionListener //ActionListener for the Enter button which will perform all the operations according to the text on the JTextField.
{

public void actionPerformed(ActionEvent e)
{
String temp = e.getActionCommand();
String previousOperation = "";
Double result = null;

if(temp.equals("="))
{
String text = textField1.getText();
int marker = 0;//marks the line where the splitting should occur.
double r = 0;
for(int i=0;i<text.length();i++)
{

String t = text.substring(marker, i+1);

marker = i+1;
try{
r = Double.parseDouble(t);
}
catch(Exception exception)
{

}
marker = i+1;

if(previousOperation.equals("+"))
{
if(result==Double.NaN){
result = r;
}else{
result+=r;
}
}
else if(previousOperation.equals("-"))
{
if(result==Double.NaN)
{
result = r;
}
else
{
result-=r;
}
}
else if(previousOperation.equals("*"))
{
if(result==Double.NaN)
{
result = r;
}
else
{
result*=r;
}
}
else if(previousOperation.equals("/"))
{
if(result==Double.NaN)
{
result = r;
}else
{
result/=r;
}
}
if(!Character.isDigit(text.charAt(i)))
{
previousOperation = ""+text.charAt(i);
}
if(result==null)
{
result = r;
}

}
textField1.setText(""+result);
}else{
textField1.setText("Error");
}

}

}

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 16 2023
Added on Jun 2 2011
3 comments
272 views