Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2013
    Posts
    1
    Rep Power
    0

    Post Exception in thread "main"

    PLEASE HELP MEEE :(

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

    class Calculator extends JFrame {
    private final Font BIGGER_FONT = new Font("monspaced",
    Font.PLAIN, 20);
    private JTextField textfield;
    private boolean number = true;
    private String equalOp = "=";
    private CalculatorOp op = new CalculatorOp();

    public Calculator() {
    textfield = new JTextField("0", 12);
    textfield.setHorizontalAlignment(JTextField.RIGHT) ;
    textfield.setFont(BIGGER_FONT);

    ActionListener numberListener = new NumberListener();
    String buttonOrder = "1234567890 ";
    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new GridLayout(4, 4, 4, 4));
    for (int i = 0; i < buttonOrder.length(); i++) {
    String key = buttonOrder.substring(i, i+1);
    if (key.equals(" ")) {
    buttonPanel.add(new JLabel(""));
    } else {
    JButton button = new JButton(key);
    button.addActionListener(numberListener);
    button.setFont(BIGGER_FONT);
    buttonPanel.add(button);
    }
    }
    ActionListener operatorListener = new OperatorListener();
    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(4, 4, 4, 4));
    String[] opOrder = {"+", "-", "*", "/","=","C"};
    for (int i = 0; i < opOrder.length; i++) {
    JButton button = new JButton(opOrder[i]);
    button.addActionListener(operatorListener);
    button.setFont(BIGGER_FONT);
    panel.add(button);
    }
    JPanel pan = new JPanel();
    pan.setLayout(new BorderLayout(4, 4));
    pan.add(textfield, BorderLayout.NORTH );
    pan.add(buttonPanel , BorderLayout.CENTER);
    pan.add(panel , BorderLayout.EAST );
    this.setContentPane(pan);
    this.pack();
    this.setTitle("Calculator");
    this.setResizable(false);
    }
    private void action() {
    number = true;
    textfield.setText("0");
    equalOp = "=";
    op.setTotal("0");
    }
    class OperatorListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
    if (number) {
    action();
    textfield.setText("0");
    } else {
    number = true;
    String displayText = textfield.getText();
    if (equalOp.equals("=")) {
    op.setTotal(displayText);
    } else if (equalOp.equals("+")) {
    op.add(displayText);
    } else if (equalOp.equals("-")) {
    op.subtract(displayText);
    } else if (equalOp.equals("*")) {
    op.multiply(displayText);
    } else if (equalOp.equals("/")) {
    op.divide(displayText);
    }
    textfield.setText("" + op.getTotalString());
    equalOp = e.getActionCommand();
    }
    }
    }
    class NumberListener implements ActionListener {
    public void actionPerformed(ActionEvent event) {
    String digit = event.getActionCommand();
    if (number) {
    textfield.setText(digit);
    number = false;
    } else {
    textfield.setText(textfield.getText() + digit);
    }
    }
    }
    public void actionPerformed(ActionEvent e){

    private int total;
    public CalculatorOp() {
    total = 0;
    }
    public String getTotalString() {
    return ""+total;
    }
    public void setTotal(String n) {
    total = convertToNumber(n);
    }
    public void add(String n) {
    total += convertToNumber(n);
    }
    public void subtract(String n) {
    total -= convertToNumber(n);
    }
    public void multiply(String n) {
    total *= convertToNumber(n);
    }
    public void divide(String n) {
    total /= convertToNumber(n);
    }
    private int convertToNumber(String n) {
    return Integer.parseInt(n);
    }

    /** Main method */

    public static void main (String [] args){
    JFrame frame = new Calculator();

    frame.setTitle("Chapter12");
    frame.setSize(250,350);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setVisible(true);
    }

    }

    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,382
    Rep Power
    5

    Default Re: Exception in thread "main"

    Please read

    Guide For New Members
    BB Code List - Java Programming Forum - Learn Java Programming

    Also place your code between [code][/code] tags. And please include the exception including the line number where it was thrown.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Exception in thread "main"

    Uhm, its probably just how you posted but this
    Java Code:
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    is NOT right, and I hope you can see for yourself what the problem is.

    Then again, it WOULD help if you told us WHAT
    Exception in thread "main"
    occurred.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,669
    Rep Power
    5

    Default Re: Exception in thread "main"

    If you're talking about that extra space, that's just the forum software. I fell into that trap once before too.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default Re: Exception in thread "main"

    Yep, it's the forum software trying to prevent long character strings from distorting the forum layout, so it adds in a space to break them up.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 1
    Last Post: 11-02-2012, 11:38 PM
  2. Replies: 2
    Last Post: 06-20-2012, 12:18 AM
  3. Replies: 5
    Last Post: 12-21-2011, 06:44 PM
  4. Replies: 14
    Last Post: 04-08-2011, 06:01 PM
  5. Replies: 5
    Last Post: 06-22-2010, 03:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •