Java Problem

Posts 16 of 6 · Page 1 of 1
Java Problem
I'M POSTING IN THIS SECTION BECAUSE JAVA SECTION IS DEAD, PLEASE DONT MOVE IT

I'm trying to create a window with one Label, one TextField and one Button... But I have a problem, nothing shows up unless I resize the window manually (with my mouse), or if I remove the TextField it will work...


Main file:
Code:
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;

public class App  {


	public static void main(String[] args) {
    
    	janela nova = new janela("Window Title",800,200);
    	JLabel txt = new JLabel("Please tell us your name:");
    	JButton btn = new JButton("Ok");
    	JTextField nameTxt = new JTextField("enter text here",30);
    	nova.add(txt);
    	nova.add(nameTxt);
    	nova.add(btn);

    
    	
    }
}
janela file:

Code:
import java.awt.FlowLayout;
import javax.swing.JFrame;

public class janela extends JFrame  {
	private static final long serialVersionUID = 1L;
   
	public janela(String title,int width,int height){
		super(title);
		setLayout(new FlowLayout());
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(width,height);
		setVisible(true);
	}

	
}
Help a noob out please
After a quick glance at your code, I sort of realized this.

You aren't defining where to put the things, right?

nova.add(txt);

Are you even defining where to place it?
Quote Originally Posted by Auxilium View Post
After a quick glance at your code, I sort of realized this.

You aren't defining where to put the things, right?

nova.add(txt);

Are you even defining where to place it?
I'm using the FlowLayout, it adds it automaticly, if you run it and then resize the window with your mouse everything will show up, or if you just dont add the textField everything will be in its place... strange thing..
Quote Originally Posted by Pablo Camara View Post
I'M POSTING IN THIS SECTION BECAUSE JAVA SECTION IS DEAD, PLEASE DONT MOVE IT

I'm trying to create a window with one Label, one TextField and one Button... But I have a problem, nothing shows up unless I resize the window manually (with my mouse), or if I remove the TextField it will work...


Main file:
Code:
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;

public class App  {


	public static void main(String[] args) {
    
    	janela nova = new janela("Window Title",800,200);
    	JLabel txt = new JLabel("Please tell us your name:");
    	JButton btn = new JButton("Ok");
    	JTextField nameTxt = new JTextField("enter text here",30);
    	nova.add(txt);
    	nova.add(nameTxt);
    	nova.add(btn);

    
    	
    }
}
janela file:

Code:
import java.awt.FlowLayout;
import javax.swing.JFrame;

public class janela extends JFrame  {
	private static final long serialVersionUID = 1L;
   
	public janela(String title,int width,int height){
		super(title);
		setLayout(new FlowLayout());
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(width,height);
		setVisible(true);
	}

	
}
Help a noob out please
I'm not sure..Didn't code in Java long time
Don't you have to add buttons,textboxes and that stuff on JPanel first then add that JPanel to JFrame ?
Quote Originally Posted by Lovroman View Post
I'm not sure..Didn't code in Java long time
Don't you have to add buttons,textboxes and that stuff on JPanel first then add that JPanel to JFrame ?
Nope, if that was the problem, nothing i've ever done would show up....
Anyone help?

Where are the pros? Like @Jason , @NextGen xD and the others pros i used to see there xD
Here you go

Code:
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;

public class App  {


	public static void main(String[] args) {
    
    	janela nova = new janela("Window Title",800,200);
    	JLabel txt = new JLabel("Please tell us your name:");
    	JButton btn = new JButton("Ok");
    	JTextField nameTxt = new JTextField("enter text here",30);
    	nova.add(txt);
    	nova.add(nameTxt);
    	nova.add(btn);
    	nova.validate();
    	
    }
}
I added nova.validate();.
Posts 16 of 6 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?