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);
}
}
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.
db