Results 1 to 2 of 2
  1. #1
    rosishere is offline Member
    Join Date
    Jul 2012
    Posts
    4
    Rep Power
    0

    Default need help with "scanner to label sting"

    So, this is my problem, i made this program to get to know how to use Scanners and it worked.. sort of;
    the first version worked like this:
    -type something in the console
    -frame apears with the label and the input.
    ..this was wrong so i put my frame-code above the Scanner but now it doesn't work anymore;
    the frame apears but the label doesn't seem to work or apear :(

    import java.util.Scanner;
    import javax.swing.*;


    - original code:

    public class a {

    public static void main(String[] args){

    Scanner input = new Scanner(System.in);

    String output = input.nextLine();


    JFrame frame = new JFrame("Frame!");
    JPanel panel = new JPanel();
    JLabel label = new JLabel(output + "..working?");
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setSize(200,200);

    frame.add(panel);
    panel.add(label);

    }
    }

    -new code:

    import java.util.Scanner;
    import javax.swing.*;

    public class a {

    public static void main(String[] args){

    JFrame frame = new JFrame("Frame!");
    JPanel panel = new JPanel();
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setSize(200,200);

    Scanner input = new Scanner(System.in);
    String output = input.nextLine();

    JLabel label = new JLabel(output + "..working?");

    frame.add(panel);
    panel.add(label);

    }
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,247
    Rep Power
    19

    Default Re: need help with "scanner to label sting"

    Moved from a staff-only section.

    Please go through Guide For New Members and BB Code List - Java Programming Forum and edit your post accordingly.

    Swing methods and constructors should be invoked on the EDT. Not on the Main thread.

    After adding/removing component(s) to/from an already visible container it is necessary to invoke revalidate() (and usually also repaint()) to update the UI.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 0
    Last Post: 12-07-2012, 08:29 AM
  2. Replies: 3
    Last Post: 10-30-2012, 03:06 PM
  3. Scanner "nextLine" Command [Homework]
    By btgrant in forum New To Java
    Replies: 1
    Last Post: 10-08-2011, 02:45 AM
  4. trouble with Scanner(new File("input"));
    By ronyosi in forum New To Java
    Replies: 9
    Last Post: 10-27-2010, 11:34 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Tags for this Thread

Posting Permissions

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