Solved

Java Calculator runs in BlueJ but not in Eclipse

Posted on 2011-02-13
2
1,184 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
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

726 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