JFrame issues

Discussion in 'Java' started by K, Dec 3, 2012.

  1. K

    K Guest

    Here is my script does anyone know why this isn't working

    import javax.swing.JFrame;
    import javax.swing.ImageIcon;
    import javax.swing.JLabel;
    import javax.swing.Continer;

    class Jframe {

    public static void main(String[] args) {
    JFrame frame = new JFrame();
    ImageIcon icon = new ImageIcon(puffin.jpg);
    JLabel label = new JLable (icon);
    Container contentPane = frame.getContentPane();

    contentPane.add(label);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);

    }

    }
    K, Dec 3, 2012
    #1
    1. Advertising

  2. K

    Lew Guest

    K wrote:
    > Here is my script


    It's not a script.

    > does anyone know why this isn't working


    What is "working" for this program?

    What happens instead?

    Copy and paste your error messages so we don't have to
    tire out our crystal balls.

    You should place your program in a package, rather than the
    "default" (empty) package.

    > import javax.swing.JFrame;
    > import javax.swing.ImageIcon;
    > import javax.swing.JLabel;
    > import javax.swing.Continer;

    ^
    You should get a compilation error on this import.

    > class Jframe {


    Get in the habit of declaring classes 'public', and not naming them so
    similarly to standard API types.

    > public static void main(String[] args) {
    > JFrame frame = new JFrame();


    You failed to put the GUI work on the Event Dispatch Thread (EDT).

    > ImageIcon icon = new ImageIcon(puffin.jpg);

    ^
    This line should raise a compilation error.

    > JLabel label = new JLable (icon);

    ^
    This line should raise a compilation error.

    > Container contentPane = frame.getContentPane();
    > contentPane.add(label);
    > frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    > frame.pack();
    > frame.setVisible(true);
    > }
    > }


    You are getting compilation errors that tell you exactly where you are being
    careless.

    Use 'invokeLater()' to do GUI things (like defining your frame and all that).
    Doing GUI off the EDT and doing non-GUI on the EDT are the twin sins of Swing.

    Repent your sins.

    --
    Lew
    Lew, Dec 3, 2012
    #2
    1. Advertising

  3. K

    Eric Sosman Guest

    On 12/2/2012 11:34 PM, K wrote:
    > Here is my script does anyone know why this isn't working
    >
    > import javax.swing.JFrame;
    > import javax.swing.ImageIcon;
    > import javax.swing.JLabel;
    > import javax.swing.Continer;


    Because there's no Continer class in javax.swing.

    > class Jframe {
    >
    > public static void main(String[] args) {
    > JFrame frame = new JFrame();


    Because Swing components should be constructed and
    manipulated on the Event Dispatch Thread.

    > ImageIcon icon = new ImageIcon(puffin.jpg);


    Because puffin.jpg is a syntax error.

    > JLabel label = new JLable (icon);


    Because no JLable class has been declared.

    > Container contentPane = frame.getContentPane();


    Because no Container class has been declared.

    > contentPane.add(label);
    > frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    > frame.pack();
    > frame.setVisible(true);
    >
    > }
    >
    > }


    --
    Eric Sosman
    d
    Eric Sosman, Dec 3, 2012
    #3
  4. K

    Arne Vajhøj Guest

    On 12/2/2012 11:34 PM, K wrote:
    > Here is my script does anyone know why this isn't working
    >
    > import javax.swing.JFrame;
    > import javax.swing.ImageIcon;
    > import javax.swing.JLabel;
    > import javax.swing.Continer;
    >
    > class Jframe {
    >
    > public static void main(String[] args) {
    > JFrame frame = new JFrame();
    > ImageIcon icon = new ImageIcon(puffin.jpg);
    > JLabel label = new JLable (icon);
    > Container contentPane = frame.getContentPane();
    >
    > contentPane.add(label);
    > frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    > frame.pack();
    > frame.setVisible(true);
    >
    > }
    >
    > }


    You need to learn to look at the error messages and fix
    bugs following the information in those.

    Then you can help yourself in the future.

    Arne
    Arne Vajhøj, Dec 4, 2012
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Passero
    Replies:
    6
    Views:
    3,171
    Tassilo Horn
    Jul 5, 2003
  2. Replies:
    9
    Views:
    1,395
    Raymond DeCampo
    Jul 1, 2005
  3. kc
    Replies:
    3
    Views:
    8,339
  4. jakester
    Replies:
    3
    Views:
    1,801
    jakester
    Apr 3, 2007
  5. Replies:
    2
    Views:
    400
    markspace
    Aug 27, 2012
Loading...

Share This Page