Pregunta

I am new to gui in java, i spent 3 hours trying to figure out what i have done wrong or misunderstood, i should get this:

enter image description here

but the text in my code is displayed after textfields

   textPanel = new JPanel();
     textPanel.setLayout(new GridLayout(3,0)); 

     fName = new JTextField( 15 ) ; 
     textPanel.add(fName);
     jlbName = new JLabel ( "Firstname" );
    jlbName.setHorizontalAlignment(JLabel.RIGHT);
    textPanel.add(jlbName);

     lName = new JTextField( 15 ) ; 
    textPanel.add(lName);
    jlbName = new JLabel ( "LastName" );

    jlbName.setHorizontalAlignment(JLabel.RIGHT);
    textPanel.add(jlbName);



    libNo = new JTextField( 15 ) ; 
    textPanel.add(libNo);
       libNo.setEditable(false);
    jlbName = new JLabel ( "Library Number" );

    jlbName.setHorizontalAlignment(JLabel.RIGHT);
    textPanel.add(jlbName);


   add(textPanel,BorderLayout.EAST);
   JButton jbtN = new JButton("Add borrower");
   add(jbtN ,BorderLayout.SOUTH);
¿Fue útil?

Solución

You are inserting the components into the panel in the wrong order. You first insert the text fields and later the labels. Do the opposite, i.e. instead of:

textPanel.add(fName);
...
textPanel.add(jlbName);
...

do:

textPanel.add(jlbName);
...
textPanel.add(fName);
...
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top