0
import javax.swing.*;
import java.awt.*;
public class gui {
    JFrame f;
    JLabel fname,fsex,age;
    JTextField t1;
    JTextField t2;
    JTextField t3;
     gui(){
        frame();
    }

    private void frame() {
        // TODO Auto-generated method stub
        f = new JFrame();
        f.setVisible(true);
        f.setSize(500,500);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        fname = new JLabel("First Name");
        fname.setSize(100,100);
        fsex = new JLabel("Sex");
        fsex.setSize(100,100);
        age = new JLabel("Age");
        age.setSize(100,100);
        t1 = new JTextField();
        t1.setSize(100,100);
        t2 = new JTextField();
        t2.setSize(100,100);
        t3 = new JTextField();
        t3.setSize(100,100);
        JPanel panel = new JPanel();
        panel.setSize(100,100);
        panel.add(fname);
        panel.add(t1);
        panel.add(fsex);
        panel.add(t2);
        panel.add(fname);
        panel.add(age);
        panel.add(t3);
        f.add(panel);


    }
}

I was wondering that if i could the reason that why am i getting a display of such kind!I am getting unsized small line shaped widgets in the top centre of the screen and not in theorder in which they have been coded. Am i not supposed to get properly shaped widgets once i have set sizes.And also the widgets arent invisible,they are visible but unordered.

nachokk
  • 14,363
  • 4
  • 24
  • 53
user2474232
  • 1
  • 1
  • 4
  • I'd also recommend you take a look at [Inital Threads](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html) and [Java Coding Conventions](http://www.oracle.com/technetwork/java/codeconv-138413.html) – MadProgrammer Jun 15 '13 at 21:54

4 Answers4

3

Am i not supposed to get properly shaped widgets once i have set sizes.

No, you are not supposed to set the sizes, that is the job of the layout manager. A panel uses a FlowLayout which just displays all the components horizontally at there preferred size.

A JLabel will have a preferred size based on the text you assign to the label.

For a text field you need to indicate the approximate size by specify the number of characters to display. You do this by using:

JTextField textField = new JTextField(10);

Start by reading the Swing tutorial on How to Use Flow Layout for an example and a better structured program. That is your code should be executed on the Event Dispatch Thread.

camickr
  • 321,443
  • 19
  • 166
  • 288
1

You have to call setVisible(true) in last.

private void frame() {
        // TODO Auto-generated method stub
        f = new JFrame();
        f.setSize(500,500);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        fname = new JLabel("First Name");
        fname.setSize(100,100);
        fsex = new JLabel("Sex");
        fsex.setSize(100,100);
        age = new JLabel("Age");
        age.setSize(100,100);
        t1 = new JTextField();
        t1.setSize(100,100);
        t2 = new JTextField();
        t2.setSize(100,100);
        t3 = new JTextField();
        t3.setSize(100,100);
        JPanel panel = new JPanel();
        panel.setSize(100,100);
        panel.add(fname);
        panel.add(t1);
        panel.add(fsex);
        panel.add(t2);
        panel.add(fname);
        panel.add(age);
        panel.add(t3);
        f.add(panel);
        f.setVisible(true); //here set visible true after adding components
    }
Alexis C.
  • 91,686
  • 21
  • 171
  • 177
  • 1
    that's not only the fix for him – pinkpanther Jun 15 '13 at 19:29
  • 1
    even after i do this,it doesnt change the display,it is not that the widgets arent visible,they are,but ordered as i have a line shaped text field,then sex label,then fsex name line shaped textfield then its label and then not the textfield of age but its label first and then the text field – user2474232 Jun 15 '13 at 19:31
  • oh,i gues ihaveset the inputsequence wrong,but that doesnt explain the uncertain sizesof widgets – user2474232 Jun 15 '13 at 19:35
0

setVisible(true) must be the last method call in the frame() method. Please see the following thread: Why shouldn't I call setVisible(true) before adding components?

Community
  • 1
  • 1
blackpanther
  • 10,998
  • 11
  • 48
  • 78
0

You have to set the visibility as on, by calling setVisible( true ) at the end of frame() method in your code

Aneeq Anwar
  • 1,282
  • 7
  • 20