Results 1 to 2 of 2
- 12-09-2012, 07:24 PM #1
Member
- Join Date
- Jul 2012
- Posts
- 4
- Rep Power
- 0
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);
}
}
- 12-09-2012, 08:06 PM #2
Re: need help with "scanner to label sting"
Moved from a staff-only section.
Please go through http://www.java-forums.org/forum-gui...w-members.html 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.
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
Similar Threads
-
access denied("java.net.SocketPermission" "127.0.0.1:1099" "connect,resolve")
By klspepper in forum New To JavaReplies: 0Last Post: 12-07-2012, 09:29 AM -
Convert string operation symbols "+", "-", "/", "*" etc.
By Googol in forum New To JavaReplies: 3Last Post: 10-30-2012, 04:06 PM -
Scanner "nextLine" Command [Homework]
By btgrant in forum New To JavaReplies: 1Last Post: 10-08-2011, 03:45 AM -
trouble with Scanner(new File("input"));
By ronyosi in forum New To JavaReplies: 9Last Post: 10-28-2010, 12:34 AM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 08:35 AM
Bookmarks