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.

1

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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
    }

3 Comments

that's not only the fix for him
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
oh,i gues ihaveset the inputsequence wrong,but that doesnt explain the uncertain sizesof widgets
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?

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.