Results 1 to 7 of 7
  1. #1
    isuru is offline Member
    Join Date
    Jan 2010
    Posts
    19
    Rep Power
    0

    Default When I create method...

    Can you find the bug in this code?
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package javaapplication61;
    import javax.swing.*;
    import java.awt.event.*;
    public class ShowDialogBox {
        JFrame frame;
    
        public static void main(String[] args) {
            ShowDialogBox db = new ShowDialogBox();
            // TODO code application logic here
        }
      [B]  public showDialogBox(){[/B]
            frame = new JFrame("Show Her");
            JButton button = new JButton("Click Me");
            button.addActionListener(new MyAction());
            frame.add(button);
            frame.setSize(200,200);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
        public class MyAction implements ActionListener{
            public void actionPerformed(ActionEvent e){
                JOptionPane.showMessageDialog(frame,"Leann is suckz!");
            }
        }
    
    }
    Is there a problem, the way I created "showDialogBox()" method?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    It's not a method, it's a constructor, and constructor names have to match class names exactly, including spelling and cApItAlIzAtIoN.

  3. #3
    isuru is offline Member
    Join Date
    Jan 2010
    Posts
    19
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    It's not a method, it's a constructor, and constructor names have to match class names exactly, including spelling and cApItAlIzAtIoN.
    Thanks. Now I have to find tutorials on "constructors". I changed constructor name to be class name. It worked.:)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by isuru View Post
    Thanks. Now I have to find tutorials on "constructors". I changed constructor name to be class name. It worked.:)
    It's one of the most basic building blocks of the language so studying up on it is a good thing. You're welcome and much luck!

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    I always wonder why people leave that generated comment in as it is ...

    kind regards,

    Jos ;-)

  6. #6
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default

    <shuffle, shuffle>...:)

  7. #7
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    try this

    Java Code:
    package javaapplication61;
    
    import javax.swing.*;
    import java.awt.event.*;
    
    public class ShowDialogBox {
    	JFrame frame;
    
    	public static void main(String[] args) {
    		ShowDialogBox db = new ShowDialogBox();
    		// TODO code application logic here
    	}
    
    	public ShowDialogBox() {
    		frame = new JFrame("Show Her");
    		JButton button = new JButton("Click Me");
    		button.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				JOptionPane.showMessageDialog(frame, "Leann is suckz!");
    			}
    		});
    		frame.add(button);
    		frame.setSize(200, 200);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
    	}
    }

Similar Threads

  1. Replies: 2
    Last Post: 03-26-2010, 05:12 PM
  2. ArrayLists compareTo method, equals method
    By random0munky in forum New To Java
    Replies: 2
    Last Post: 10-26-2009, 07:20 PM
  3. Replies: 5
    Last Post: 10-13-2009, 01:35 PM
  4. Replies: 29
    Last Post: 09-25-2008, 07:55 PM
  5. cannot call private method from static method
    By jon80 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 08:37 AM

Posting Permissions

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