Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: New to Java..

  1. #1
    Member Staticity's Avatar
    Join Date
    Jul 2011
    Location
    Texas
    Posts
    105
    My Mood
    Inspired
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default New to Java..

    import javax.swing.JFrame;

    class calculator{
    public static void main(String[] args){
    //Created Object for Window Class
    window wo = new window();

    //Allow Program to Close on Exit
    wo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Program Window Settings
    wo.setSize(600,600);
    wo.setVisible(true);

    }
    }


    import java.awt.FlowLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JFrame;
    import javax.swing.JButton;
    import javax.swing.JOptionPane;
    import java.awt.*;

    public class window extends JFrame{

    private JButton add, sub, mult, div, sqrt, expo, quad, comp;

    public window(){
    super("Calculator");
    setLayout(new FlowLayout());

    //Addition Button
    add = new JButton("+");
    add(add, BorderLayout.CENTER);

    //Subtraction Button
    sub = new JButton("-");
    add(sub, BorderLayout.CENTER);

    //Multiplication Button
    add = new JButton("x");
    add(mult, BorderLayout.CENTER);

    //Division Button
    add = new JButton("/");
    add(div, BorderLayout.CENTER);

    //Square Root Button
    add = new JButton("Square Root");
    add(sqrt, BorderLayout.CENTER);

    //Exponential Button
    add = new JButton("Exponential");
    add(expo, BorderLayout.CENTER);

    //Quadratic Button
    add = new JButton("Quadratic");
    add(quad, BorderLayout.CENTER);

    //Compound Interest Button
    add = new JButton("Compound Interest");
    add(comp, BorderLayout.CENTER);


    //Handler Class
    HandlerClass handler = new HandlerClass();
    add.addActionListener(handler);
    sub.addActionListener(handler);
    mult.addActionListener(handler);
    div.addActionListener(handler);
    sqrt.addActionListener(handler);
    expo.addActionListener(handler);
    quad.addActionListener(handler);
    comp.addActionListener(handler);
    }
    private class HandlerClass implements ActionListener{

    //actionPerformed is the only method to override
    public void actionPerformed(ActionEvent event){
    JOptionPane.showMessageDialog(null, String.format("%s", event.getActionCommand()));

    }
    }
    }

    This runs perfectly fine with only 2 buttons.. An error occurs once the buttons mult, div, sqrt, expo, quad, comp are added.


  2. #2
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: New to Java..

    The problem looks like careless copy & paste errors - you're trying to add buttons that are null because you're creating a new 'add' button every time, instead of the buttons you should be creating.

  3. The Following User Says Thank You to dlorde For This Useful Post:

    Staticity (July 23rd, 2011)

  4. #3
    Member Staticity's Avatar
    Join Date
    Jul 2011
    Location
    Texas
    Posts
    105
    My Mood
    Inspired
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default Re: New to Java..

    Quote Originally Posted by dlorde View Post
    The problem looks like careless copy & paste errors - you're trying to add buttons that are null because you're creating a new 'add' button every time, instead of the buttons you should be creating.
    I must say, my error was quite embarrassing.. Thank you for pointing out the obvious that I cannot see