File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Help! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Help!" Watch "Help!" New topic


Christopher McLaughlin

Joined: Aug 10, 2005
Posts: 3
Alright here is the situation, I am trying to create a Calculator applet that accepts complex number expressions, does the calculation and returns the results in a seperate text area in the form of:

expression = "rectangular form" or "polar form"

also there are three radio buttons which change the format of the expression.

at this point I can only get the calculate button to work half-arsed, and none of the radio buttons to work at all.

here is my file

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

public class CalcApplet extends JApplet implements ActionListener
protected JTextField entry;
protected JTextArea results;
protected JScrollPane resultsPane;
protected JButton calculate, clear, clearResults;
protected JRadioButton infix, prefix, postfix;
protected JPanel input, output, mode;
protected ButtonGroup group;

static String sInfix = "Infix Notation";
static String sPrefix = "Prefix Notation";
static String sPostfix = "Postfix Notation";

public void init()
// create GUI components
entry = new JTextField(35);
results = new JTextArea(10, 40);
JScrollPane resultsPane = new JScrollPane(results);
calculate = new JButton("Calculate");
clear = new JButton("Clear");
clearResults = new JButton("Clear Results");
infix = new JRadioButton(sInfix);
prefix = new JRadioButton(sPrefix);
postfix = new JRadioButton(sPostfix);
group = new ButtonGroup();

//create and fill mode JPanel
mode = new JPanel();

//create and fill the input JPanel
input = new JPanel();

//create and fill the output JPanel
output = new JPanel();

//add the JPanels to the content pane
getContentPane().add(mode, BorderLayout.NORTH);
getContentPane().add(input, BorderLayout.CENTER);
getContentPane().add(output, BorderLayout.SOUTH);

//Setup ActionListener
calculate.addActionListener( this );
clear.addActionListener( this );
clearResults.addActionListener( this );
infix.addActionListener ( this );
prefix.addActionListener ( this );
postfix.addActionListener ( this );

public Term createTermObject()

String text = entry.getText();
Term expr = ExprParser.parse( text );
return expr;

public void actionPerformed( ActionEvent e )

Object src = e.getSource();

if (src == calculate)
Term term = createTermObject();
results.append( term + "=" + term.toString() + " or " /* + term.toPolarString*/ + "\n" );
else if (src == clear)
else if (src == clearResults)


and here is the website it is currently on:

and this is the site where the instructor put up what a finished product should look like and do:

As you can see if you put in 5*i on his version the result is proper,
but when you use mine it comes out wrong...

any ideas?
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955


Please don't post the same question in multiple forums. It's exremely frustrating when somebody takes the time to help you, and then finds out you've already gotten an answer in a different place. Also, you may get confused when different people start giving different suggestions as to what to try...

i imagine a moderator will close one of these posts soon...

Link to other thread

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
I agree. Here's the link:
subject: Help!
It's not a secret anymore!