Results 1 to 2 of 2
- 12-09-2012, 06: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, 07:06 PM #2
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
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, 08:29 AM -
Convert string operation symbols "+", "-", "/", "*" etc.
By Googol in forum New To JavaReplies: 3Last Post: 10-30-2012, 03:06 PM -
Scanner "nextLine" Command [Homework]
By btgrant in forum New To JavaReplies: 1Last Post: 10-08-2011, 02:45 AM -
trouble with Scanner(new File("input"));
By ronyosi in forum New To JavaReplies: 9Last Post: 10-27-2010, 11:34 PM -
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, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks