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 8 of 8

Thread: How do i fix my code?

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation How do i fix my code?

    Hi. I am trying to create a calculator and I've started adding in the action listener but when i add it eclipse wont let me run it and it says that there is nothing wrong with it.HELP

    import javax.swing.JPanel;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    import java.sql.*;
     
    public class Calculator extends JFrame implements ActionListener
    {
    		public Calculator()
    		{
    			super("Calculator");
    			LoadUI();
    		}
    		public void LoadUI()
    		{
    			final JFormattedTextField field = new JFormattedTextField("");
    			JButton add = new JButton("+");
    			JButton subtract = new JButton("-");
    			JButton divide = new JButton("/");
    			JButton multiply = new JButton("*");
    			JButton one = new JButton("1");
    			JButton two = new JButton("2");
    			JButton three = new JButton("3");
    			JButton four = new JButton("4");
    			JButton five = new JButton("5");
    			JButton six = new JButton("6");
    			JButton seven = new JButton("7");
    			JButton eight = new JButton("8");
    			JButton nine = new JButton("9");
    			JButton zero = new JButton("0");
    			JButton equals = new JButton("=");
    			JButton ac = new JButton("AC");
    			JPanel panel = new JPanel(null);
    			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			setResizable(false);
    			setBounds(0,0,400,500);
    			setLocationRelativeTo(null);
     
    			field.setBounds(0,0,500,50);
    			one.setBounds(150,120,50,50);
    			two.setBounds(200,120,50,50);
    			three.setBounds(250,120,50,50);
    			four.setBounds(150,170,50,50);
    			five.setBounds(200,170,50,50);
    			six.setBounds(250,170,50,50);
    			seven.setBounds(150,220,50,50);
    			eight.setBounds(200,220,50,50);
    			nine.setBounds(250,220,50,50);
    			zero.setBounds(200,270,50,50);
    			add.setBounds(100,120,50,50);
    			subtract.setBounds(100,170,50,50);
    			divide.setBounds(100,220,50,50);
    			multiply.setBounds(100,270,50,50);
    			equals.setBounds(250,270,50,50);
    			ac.setBounds(150,320,150,50);
    			ImageIcon imageBack = new ImageIcon("albert.JPG");
    			JLabel background = new JLabel(imageBack);
    			background.setBounds(0,0,400,500);
    			getLayeredPane().add(background, new Integer(Integer.MIN_VALUE));
     
    			panel.add(ac);
    			panel.add(equals);
    			panel.add(multiply);
    			panel.add(divide);
    			panel.add(subtract);
    			panel.add(add);
    			panel.add(field);
    			panel.add(one);
    			panel.add(two);
    			panel.add(three);
    			panel.add(four);
    			panel.add(five);
    			panel.add(six);
    			panel.add(seven);
    			panel.add(eight);
    			panel.add(nine);
    			panel.add(zero);
    			panel.add(background);
    			add(panel);
    			setVisible(true);    
    		one.addActionListener ( new ActionListener()
    		{
    		public void actionPerformed(ActionEvent arg0) 
    		{
    			field.setText("1");
    		}
    		});
    	}
    		public void actionPerformed(ActionEvent e) 
    		{
     
    		}
     
    }
    Last edited by KevinWorkman; August 18th, 2011 at 10:11 AM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How do i fix my code?

    I added highlight tags for you this time. Don't forget them in the future.

    Slow down. What is your goal? What do you expect this code to do? What exactly does it do instead? You'd probably be better off asking a more specific question and posting an SSCCE instead of just dumping all your code here. For example, if your question is about adding an ActionListener, then create a program with a single button and add an ActionListener to it.

    Edit- Good lord, why don't you use a layout manager like a sane person?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Aug 2011
    Posts
    48
    My Mood
    Fine
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: How do i fix my code?

    I am trying to create a calculator and I've started adding in the action listener but when i add it eclipse wont let me run it and it says that there is nothing wrong with it.HELP
    Your class is missing main method:

    	public static void main(String[] args) {
    		Calculator cal = new Calculator();
    	}

    immutable objects
    Last edited by ha.minh.nam; December 4th, 2011 at 07:33 PM.

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: How do i fix my code?

    Quote Originally Posted by ha.minh.nam View Post
    Your class is missing main method:
    It is possible that the main method is in a different class. Not all classes need a main method.
    Improving the world one idiot at a time!

  5. #5
    Member
    Join Date
    Aug 2011
    Posts
    86
    My Mood
    Lurking
    Thanks
    16
    Thanked 4 Times in 4 Posts

    Default Re: How do i fix my code?

    You forgot to extend the class you are using super for.

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How do i fix my code?

    Quote Originally Posted by Spidey1980 View Post
    You forgot to extend the class you are using super for.
    Huh? Where? He extends JFrame.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    Aug 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do i fix my code?

    Lol i forgot about this Looking back i feel so stupid

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How do i fix my code?

    Quote Originally Posted by beebee007 View Post
    Lol i forgot about this Looking back i feel so stupid
    That happens to every programmer, all the time.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!