Results 1 to 6 of 6
Thread: Calculator Functions
- 04-30-2011, 05:29 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 5
- Rep Power
- 0
Calculator Functions
I can get the numbers and *,+,- signs to appear, but I can't get the numbers to equal anything or solve out, and the cancel button doesn't work either. I've been doing Java for two days maybe my code is messed up.
import java.awt.FlowLayout;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class conversion_ui implements ActionListener {
JFrame frame = new JFrame("Conversion");
JPanel panel = new JPanel(new FlowLayout());
JTextArea text = new JTextArea(1,20);
JButton but1 = new JButton("1");
JButton but2 = new JButton("2");
JButton but3 = new JButton("3");
JButton but4 = new JButton("4");
JButton but5 = new JButton("5");
JButton but6 = new JButton("6");
JButton but7 = new JButton("7");
JButton but8 = new JButton("8");
JButton but9 = new JButton("9");
JButton but0 = new JButton("0");
JButton butadd = new JButton("+");
JButton butsub = new JButton("-");
JButton butmulti = new JButton("*");
JButton buteq = new JButton("=");
JButton butclear = new JButton("C");
Double number1,number2,result;
int addc=0,subc=0,multic=0;
private JButton source;
public void ui()
{
frame.setVisible(true);
frame.setSize(250,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.add(panel);
panel.add(text);
panel.add(but1);
panel.add(but2);
panel.add(but3);
panel.add(but4);
panel.add(but5);
panel.add(but6);
panel.add(but7);
panel.add(but8);
panel.add(but9);
panel.add(but0);
panel.add(butadd);
panel.add(butsub);
panel.add(butmulti);
panel.add(buteq);
panel.add(butclear);
but1.addActionListener(this);
but2.addActionListener(this);
but3.addActionListener(this);
but4.addActionListener(this);
but5.addActionListener(this);
but6.addActionListener(this);
but7.addActionListener(this);
but8.addActionListener(this);
but9.addActionListener(this);
but0.addActionListener(this);
butadd.addActionListener(this);
butsub.addActionListener(this);
butmulti.addActionListener(this);
buteq.addActionListener(this);
butclear.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
String btnText = e.getActionCommand();
if ("1234567890+-*".contains(btnText)) {
text.setText(text.getText() + btnText);
}
if(source==butclear)
{
number1=0.0;
number2=0.0;
text.getText();
}
if(source==butadd)
{
number1=number_reader();
text.setText("");
addc=1;
subc=0;
multic=0;
} if(source==butsub)
{
number1=number_reader();
text.setText("");
addc=0;
subc=1;
multic=0;
} if(source==butmulti)
{
number1=number_reader();
text.setText("");
addc=0;
subc=0;
multic=1;
}
if(source==buteq)
{
number2=number_reader();
if(addc>0)
{
result=number1+number2;
text.setText(Double.toString(result));
}
if(subc>0)
{
result=number1-number2;
text.setText(Double.toString(result));
}
if(multic>0)
{
result=number1*number2;
text.setText(Double.toString(result));
}
}
}
public double number_reader()
{
Double num1;
String s;
s=text.getText();
num1=Double.valueOf(s);
return num1;
}
}
- 04-30-2011, 11:08 PM #2
- Please use code-tags (wrap your code in [co de][/code]), and proper indentation.
- The convention is that Class-names begin with a capital.
- Your class is nor an applet nor has it a main-function so it doesn't run as posted.
- If possible post a SSCCE (small self contained compiling example).
(It is not very polite to make someone reconstruct your intensions.)
I added a class with a main():
- Conversion_UI has no constructor, so nothing happens on instantiating.
I added a call to ui() in my new main-class:
Rejoice, something happens. A gui shows up with appropriate buttons for a calculator. Not bad for a start!
(The layout appears to be rather primitive: stretching the panel aligns the components: a typical flowlayout. Something to fix later.)
Local JButton variable "source" in the start of "actionPerformed()" is supposed to magically contain a reference to the pressed button: I added
source=(JButton)e.getSource();
to make this magic come true.
Now the buttons work (check this by some println-statements), but the code produces exceptions (Numberformat and Conversion to begin with),
but the code is reached now.
Good luck repairing. Please post a complete SSCCE in your next post.Last edited by Jodokus; 04-30-2011 at 11:16 PM.
- 04-30-2011, 11:31 PM #3
After answering your question, I saw that there is another earlier thread about the same subject, WITH the code I had to reconstruct!
In your next posts, please DO NOT start a new thread, but continue the same thread while your project is developing.
-
- 05-01-2011, 01:38 PM #5
Member
- Join Date
- Apr 2011
- Posts
- 5
- Rep Power
- 0
Thanks, so if I'm using a Gui I need to post the _main part as well?? I never thought that the Calculator Fucntions and my previous post was the same, but it makes sense with the project. I'll start working on understanding how to post information more correctlly
- 05-01-2011, 01:53 PM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
If you make a post about a calculator, ask any and all questions you have in regards to that particular project, save new threads for different projects and assignments.
Similar Threads
-
Functions and Classes
By DoubleDee in forum New To JavaReplies: 12Last Post: 09-13-2010, 01:15 AM -
static functions
By jatinrai199 in forum New To JavaReplies: 6Last Post: 08-31-2010, 05:44 PM -
JSP Functions
By sysout in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 11-07-2009, 04:10 AM -
2 functions
By baze7 in forum New To JavaReplies: 3Last Post: 08-14-2009, 04:41 AM -
Functions in jsp
By samson in forum JavaServer Pages (JSP) and JSTLReplies: 3Last Post: 03-25-2009, 10:04 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks