?
Solved

Java Calculator runs in BlueJ but not in Eclipse

Posted on 2011-02-13
2
Medium Priority
?
1,205 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 2000 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month14 days, 6 hours left to enroll

771 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