Solved

Java Calculator runs in BlueJ but not in Eclipse

Posted on 2011-02-13
2
1,196 Views
Last Modified: 2012-06-27
hi, i've written my calculator project in bluej and all works without issue but when i paste the code into eclipse it runs without error but the calculator frame doesn't appear.  i'm new to eclipse (and java in general), am i missing something?  any input would be appreciated thanks

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


/**
 * A Class that operates as both the framework for a calculator
 * and the calculator engine.
 *
 */
public class CalcFrame

{
   // private CalcEngine calc;

    private JFrame frame;
    private JTextField display;
    private JLabel status;

    //declare all buttons
    private JButton zeroButton = new JButton("0");
    private JButton oneButton = new JButton("1");
    private JButton twoButton = new JButton("2");
    private JButton threeButton = new JButton("3");
    private JButton fourButton = new JButton("4");
    private JButton fiveButton = new JButton("5");
    private JButton sixButton = new JButton("6");
    private JButton sevenButton = new JButton("7");
    private JButton eightButton = new JButton("8");
    private JButton nineButton = new JButton("9");
    private JButton plusButton = new JButton("+");
    private JButton minusButton = new JButton("-");
    private JButton divideButton = new JButton("/");
    private JButton multiplyButton = new JButton("*");
    private JButton equalsButton = new JButton("=");
    private JButton cancelButton = new JButton("C");


    private String lastNumberEntered;
    private String lastOperatorEntered;


    public CalcFrame()//CalcEngine engine)
    {

        //frame.setVisible(true);
       // calc = engine;
        makeFrame();
   
    /**
     * When an button is pressed perform that action
     */
   
   
        zeroButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                addToDisplay("0");
            }
        });

        oneButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                addToDisplay("1");
            }
        });


        twoButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                addToDisplay("2");
            }
        });
       
        threeButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                addToDisplay("3");
            }
        });
       
        fourButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                addToDisplay("4");
            }
        });
       
        fiveButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                addToDisplay("5");
            }
        });
       
        sixButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                addToDisplay("6");
            }
        });
       
        sevenButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                addToDisplay("7");
            }
        });
       
        eightButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                addToDisplay("8");
            }
        });
       
        nineButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                addToDisplay("9");
            }
        });
        plusButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                addToDisplay("+");
            }
        });
       
        minusButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                addToDisplay("-");
            }
        });
       
        divideButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                addToDisplay("/");
            }
        });
       
        multiplyButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                addToDisplay("*");
            }
        });
       
        equalsButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                float calculation = performCalculation(lastNumberEntered, display.getText(),lastOperatorEntered);
                display.setText(calculation + "");
               
                //http://java.sun.com/docs/books/tutorial/java/data/converting.html
            }
        });

        cancelButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                display.setText("");
                lastNumberEntered = null;
                lastOperatorEntered = null;
            }
        });

    }

    /**
     * This allows you to quit the calculator.
     */

    // Alows the class to quit.
    private void quit() {
        System.exit(0);


    }

    private void addToDisplay(String displayCharacter) {
        if (isAnOperator(displayCharacter)) {//if an operator is pressed, save the current number then display operator
            lastNumberEntered = display.getText();
            display.setText(displayCharacter);
            lastOperatorEntered = displayCharacter;
            //need to save what the display had here
        } else {
            if (isAnOperator(display.getText())) {//if display is an operator add a new number
                display.setText(displayCharacter);
            } else {
                display.setText(display.getText() + displayCharacter);//append displayCharacter to display
            }
        }
    }

    private float performCalculation(String firstNumberString, String secondNumberString, String operator) {
        //http://java.sun.com/docs/books/tutorial/java/data/converting.html
        float firstNumber = Float.valueOf(firstNumberString);
        float secondNumber = Float.valueOf(secondNumberString);
        if (operator.equals("+")) {
            return firstNumber + secondNumber;
        } else if (operator.equals("-")) {
            return firstNumber - secondNumber;
        } else if (operator.equals("*")) {
            return firstNumber * secondNumber;
        } else if (operator.equals("/")) {
            return firstNumber / secondNumber;
        } else {
            return -1;
        }
    }

    /**
     * Check if an input is an operator
     *
     * @return true if +,-,* or /
     */
    private boolean isAnOperator(String input) {
        if (input.equals("+") || input.equals("-") || input.equals("/") || input.equals("*")) {
            return true;
        }
        else return false;
    }

    // Calls the dialog frame with the information about the project.

    private void showAbout() {
        JOptionPane.showMessageDialog(frame,
                "Java Calculator Group Project 2011",
                "About Calculator Group Project",
                JOptionPane.INFORMATION_MESSAGE);
    }


   
    /**
     * The following creates a layout of the calculator frame.
     */
    private void makeFrame() {
        frame = new JFrame("Group Project Calculator");
        makeMenuBar(frame);

        JPanel contentPane = (JPanel) frame.getContentPane();
        contentPane.setLayout(new BorderLayout(8, 8));
        contentPane.setBorder(new EmptyBorder(10, 10, 10, 10));


        /**
         * Insert a text field
         */
        display = new JTextField("0");
        contentPane.add(display, BorderLayout.NORTH);

        //Container contentPane = frame.getContentPane();
        contentPane.setLayout(new GridLayout(4, 4));
        JPanel buttonPanel = new JPanel(new GridLayout(4, 4));
        contentPane.add(zeroButton);
        contentPane.add(oneButton);
        contentPane.add(twoButton);
        contentPane.add(threeButton);
        contentPane.add(fourButton);
        contentPane.add(fiveButton);
        contentPane.add(sixButton);
        contentPane.add(sevenButton);
        contentPane.add(eightButton);
        contentPane.add(nineButton);
        contentPane.add(plusButton);
        contentPane.add(minusButton);
        contentPane.add(divideButton);
        contentPane.add(multiplyButton);
        contentPane.add(equalsButton);
        contentPane.add(cancelButton);

        contentPane.add(buttonPanel, BorderLayout.CENTER);

       
        //contentPane.add(status, BorderLayout.SOUTH);

        frame.pack();
        frame.setVisible(true);
    }

    /**
     * Create the main frame's menu bar.
     * The frame that the menu bar should be added to.
     */
    private void makeMenuBar(JFrame frame) {
        final int SHORTCUT_MASK =
        Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();


        JMenuBar menubar = new JMenuBar();
        frame.setJMenuBar(menubar);

        JMenu menu;
        JMenuItem item;

        // create the File menu
        menu = new JMenu("File");
        menubar.add(menu);

        // create the Quit menu with a shortcut "Q" key.
        item = new JMenuItem("Quit");
        item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, SHORTCUT_MASK));
        item.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                quit();
            }
        });
       
        menu.add(item);

        // Adds an about menu.
        menu = new JMenu("About");
        menubar.add(menu);

        // Displays
        item = new JMenuItem("Calculator Project");
        item.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                showAbout();
            }
        });
        menu.add(item);

    }


    public static void main(String[] args) {
       
    }

}




0
Comment
Question by:t38
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 5

Accepted Solution

by:
aciuica earned 500 total points
ID: 34884160
Your main method is empty.
You need here at least to instantiate your CalcFrame class

public static void main(String[] args) {
          new CalcFrame();
    }
0
 

Author Comment

by:t38
ID: 34884204
Thanks, that did it.  i need to split this project into two classes, one for the gui and one for the actual calculations, i could do with some pointers on same but will post a seperate question, any advise would be appreciated.  thanks again.
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question