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: Extending Jframe

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Extending Jframe

    I am trying to further understand the program I am learning and I have an "Unresolved compilation problem"
    Here is the program I am trying to run
    Please help me to fix it and thanks for helping me to fix my last attempt.

    import java.awt.FlowLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import  javax.swing.JButton;
    import  javax.swing.JFrame;
     
    public class Frames3 extends JFrame implements ActionListener
    {
    	private static final long serialVersionUID = 1L;
     
    		public static void main(String[] args)
    		{
     
    	new Frames3().setVisible(true);
    		}
    	private Frames3()
    		{
    super("Tutorial = Rhoen MoreFrame");
    setSize (600,600);
    setDefaultCloseOperation (EXIT_ON_CLOSE);
    setLayout (new FlowLayout());
    JButton button = new JButton ("Click me");
    JButton button2 = new JButton("Button 2");
    button.addActionListener(this);
    button2.addActionListener(this);
    add (button);
    	}
    public void actionPerformed(ActionEvent e)
    {
    	String name  = e.getActionCommand();
    	if (name.equals("Click me"))
    	System.out.println("Click me");
    }
         else if (name.equals("Button 2"))
    {
    System.out.println("Button 2 has been pressed");
    }
    		}
    		}

    Error message

    Exception in thread "main" java.lang.Error: Unresolved compilation problem:

    at Frames3.main(Frames3.java:12)

    The error is in this line " else if (name.equals("Button 2"))"
    Last edited by jps; August 11th, 2013 at 12:30 PM. Reason: code tags


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Extending Jframe

    Please pose your code in code tags.

    Properly indenting your code would reveal the problem:
    	public void actionPerformed(ActionEvent e)
    	{
    		String name = e.getActionCommand();
    		if (name.equals("Click me"))
    			System.out.println("Click me");
    	}
    	else if (name.equals("Button 2"))
    	{
    		System.out.println("Button 2 has been pressed");
    	}

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Extending Jframe

    Thanks. That's an attitude that I should develop

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Extending Jframe

    An attitude? Maybe a "best practice," but if it were an attitude, you might have trouble making friends.

  5. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Extending Jframe

    I tried the indentation suggested and it did not fix the code. When I click the error message it sends me to main
    I tried checking to see if I had too many brackets but when I comment one out at the brackets and more errors sprang up

    Here is the code again indented

    import java.awt.FlowLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import  javax.swing.JButton;
    import  javax.swing.JFrame;
     
    public class Frames3 extends JFrame implements ActionListener
    {
    	private static final long serialVersionUID = 1L;
     
    		public static void main (String[] args)
    		{
    	new Frames3().setVisible(true);
    		}
    	private Frames3()
    		{
    super("Tutorial = Rhoen MoreFrame");
    setSize (600,600);
    setDefaultCloseOperation (EXIT_ON_CLOSE);
    setLayout (new FlowLayout());
    JButton button = new JButton ("Click me");
    JButton button2 = new JButton("Button 2");
    button.addActionListener(this);
    button2.addActionListener(this);
    add (button);
    	     }
    public void actionPerformed(ActionEvent e)
                 {
    		String name  = e.getActionCommand();
    		if (name.equals("Click me"))
    		System.out.println("Click me");
                    }
     else  if  (name.equals("Button 2"))
                   {
    	 	System.out.println("Button 2 has been pressed");
                     }
    }
    }

    Error message

    Exception in thread "main" java.lang.Error: Unresolved compilation problem:

    at Frames3.main(Frames3.java:12)
    Last edited by jps; August 10th, 2013 at 09:42 PM. Reason: code tags

  6. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Extending Jframe

    Quote Originally Posted by runkerr View Post
    I tried checking to see if I had too many brackets but when I comment one out at the brackets and more errors sprang up
    That would depend on which one you comment out. Count the number of { vs the number of }
    In the posted code I do not get the same number of each

    Quote Originally Posted by runkerr View Post
    Here is the code again indented
    Be sure to include code tags when posting code. See the Announcements page for instructions if you need help.

    --- Update ---

    Please do not double post your questions
    Threads merged

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Extending Jframe

    I said properly indenting your code would "reveal the error," not fix it.

    The indented code you posted is not properly indented, and indenting code has neither affect on its performance nor syntax. Instead, consistently properly indenting source code allows the coder to quickly recognize syntax errors caused by misplaced or missing braces and some other syntactical errors.

    For example, in the "properly" indented code I showed in Post #2, one can see that the 'else if' is NOT following an 'if' clause as required, but instead follows the end of the actionPerformed() method, so is outside any method at all, as though it were a method on its own. That's an error.

    I say "properly" in quotes, because there are varying definitions of what is proper. You could do well to start with the Java Coding Conventions. If a student, you should format your code according to the instructor's requirements.

  8. #8
    Junior Member
    Join Date
    Jul 2013
    Location
    uk
    Posts
    15
    Thanks
    2
    Thanked 3 Times in 3 Posts

    Default Re: Extending Jframe

    hello,
    I have noticed that you did not add your button2 in your constructor and the else if statement is actually outside the actionperformed method.
    try to fix that and see what you will get.
    thanks

Similar Threads

  1. Extending Jframe
    By runkerr in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 8th, 2013, 12:41 AM
  2. Reg:extending a jpanel
    By mohamed_mashood in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 23rd, 2013, 05:40 AM
  3. Extending JFrame + having multiple windows.
    By RedCloudTsunami in forum AWT / Java Swing
    Replies: 7
    Last Post: August 5th, 2012, 01:24 PM
  4. Extending class
    By TP-Oreilly in forum Java Theory & Questions
    Replies: 8
    Last Post: January 10th, 2012, 05:06 PM
  5. class extending
    By Reem in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 18th, 2010, 01:49 AM