Solved

Java Calculator runs in BlueJ but not in Eclipse

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
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:

920 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now