Results 1 to 7 of 7
  1. #1
    Eleeist is offline Member
    Join Date
    May 2011
    Posts
    84
    Rep Power
    0

    Default Getting "cannot resolve" errors with java.swing

    Hello,

    I am trying to create a simple window using swing.

    I get this errors when trying to compile:

    Java Code:
    Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
        SwingUtilities cannot be resolved
        JFrame cannot be resolved to a type
        JLabel cannot be resolved to a type
        JButton cannot be resolved to a type
    
        at SwingExample.main(SwingExample.java:12)
    Could someone please explain to me what is happening?

    PHP Code:
    import java.awt.FlowLayout;
    
    import java.swing.JButton;
    import java.swing.JFrame;
    import java.swing.JLabel;
    import java.swing.SwingUtilities;
    
    public class SwingExample {
        
        public static void main(String[] args) {
            
            SwingUtilities.invokeLater(new Runnable() {
                
                public void run() {
                    
                    JFrame f = new JFrame("Swing Example Window");
                    
                    f.setLayout(new FlowLayout());
                    
                    f.add(new JLabel("Hello world!"));
                    
                    f.add(new JButton("Press me!"));
                    
                    f.pack();
                    
                    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    
                    f.setVisible(true);
                    
                }
                
            });
            
        }
        
    }

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

    Default Re: Getting "cannot resolve" errors with java.swing

    I'm surprised that the imports didn't cause errors because Swing imports shouldn't be java.swing.XXX but javax.swing.XXX.

    So change this:
    Java Code:
    import java.awt.FlowLayout;
     
    import java.swing.JButton;
    import java.swing.JFrame;
    import java.swing.JLabel;
    import java.swing.SwingUtilities;
    to this:
    Java Code:
    import java.awt.FlowLayout;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.SwingUtilities;
    or
    Java Code:
    import java.awt.FlowLayout;
     
    import javax.swing.*;
    Note that the awt imports do not derive from javax but rather from java as you are doing now -- so don't change them.

  3. #3
    Eleeist is offline Member
    Join Date
    May 2011
    Posts
    84
    Rep Power
    0

    Default Re: Getting "cannot resolve" errors with java.swing

    It works now. Thank you :)

    Hmm.. Surprisingly I took the code from Wikipedia. Do they have the wrong code there?

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

    Default Re: Getting "cannot resolve" errors with java.swing

    Quote Originally Posted by Eleeist View Post
    It works now. Thank you :)
    You're welcome!


    Hmm.. Surprisingly I took the code from Wikipedia. Do they have the wrong code there?
    Look for yourself -- what do you see?

  5. #5
    Eleeist is offline Member
    Join Date
    May 2011
    Posts
    84
    Rep Power
    0

    Default Re: Getting "cannot resolve" errors with java.swing

    I could swear these x were not there an hour ago...

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

    Default Re: Getting "cannot resolve" errors with java.swing

    Quote Originally Posted by Eleeist View Post
    I could swear these x were not there an hour ago...
    Queue the music to The Twilight Zone...

  7. #7
    Skreen32 is offline Member
    Join Date
    Mar 2014
    Posts
    1
    Rep Power
    0

    Default Re: Getting "cannot resolve" errors with java.swing

    Quote Originally Posted by Eleeist View Post
    I could swear these x were not there an hour ago...
    Wikipedia is a user editable encyclopedia. Anyone, and their grandmother, can edit the articles :P

    Check it out.

    Quote Originally Posted by Fubarable View Post
    Queue the music to The Twilight Zone...
    Thanks for the help, fix'd my problem as well.
    Last edited by Skreen32; 03-29-2014 at 06:26 AM. Reason: I'm a grammar Nazi // Sorry for Necro-posting a useless post :P

Similar Threads

  1. Replies: 5
    Last Post: 12-21-2011, 07:44 PM
  2. How can I fix "Cannot resolve symbol" error
    By yma16 in forum IntelliJ IDEA
    Replies: 4
    Last Post: 05-16-2011, 01:28 PM
  3. "Cannot find symbol" errors in Java
    By 23Zone in forum New To Java
    Replies: 1
    Last Post: 02-17-2010, 08:13 AM
  4. genjar - "Unable to resolve:"
    By angryboy in forum New To Java
    Replies: 0
    Last Post: 06-28-2009, 07:48 AM
  5. Replies: 1
    Last Post: 10-20-2008, 08:35 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
  •