Sagewire Logo

JLebel as a container

2 Message(s) by 2 Author(s) originally posted in java gui


From: Chanchal Date:   Friday, October 26, 2007
Hi All,

I'm trying to add a JLable 'jLable2 to another JLabel 'jLabel1'. But
jLabel2 isn't getting displayed.

My code is

import JAVAx.swing.*;

public class LabelTest extends JFrame {

public LabelTest(){

setDefaultCloseOperation(JAVAx.swing.WindowConstants.EXIT_ON_CLOSE);
JLabel jLabel1 = new JAVAx.swing.JLabel();
jLabel1.setText("jLabel1");
JLabel jLabel2 = new JLabel();
jLabel2.setText(" TEXT ");
jLabel1.add(jLabel2);
getContentPane().add(jLabel1, JAVA.awt.BorderLayout.CENTER);
pack();
} public static void main(String args[]) {
LabelTest lTest = new LabelTest ();
lTest.setSize(400,300);
lTest.setVisible(true);
}

}

Please advice on how I can make jLabel2 visible.
Thanks in advance

Chanchal


From: Andrew Thompson Date:   Friday, October 26, 2007
wrote in message:
..
I'm trying to add a JLable 'jLable2 to another JLabel 'jLabel1'.



Why?

..But
jLabel2 isn't getting displayed.



Try this variant that adds both JLabels directly to the JFrame.
If that isn't the effect you are after, there are many alternatives,
including adding them both to a JPanel that is itself added to
one particular area of the BorderLayout of the JFrame.

<sscce>
import JAVA.awt.BorderLayout;
import JAVAx.swing.*;

public class LabelTest extends JFrame {

public LabelTest(){

setDefaultCloseOperation(
WindowConstants.EXIT_ON_CLOSE);
JLabel jLabel1 = new JLabel();
jLabel1.setText("jLabel1");
JLabel jLabel2 = new JLabel();
jLabel2.setText(" TEXT ");
//jLabel1.add(jLabel2);
getContentPane().add(
jLabel1, BorderLayout.CENTER);
getContentPane().add(
jLabel2, BorderLayout.EAST);
pack();
}

public static void main(String args[]) {
LabelTest lTest = new LabelTest ();
lTest.setSize(400,300);
lTest.setVisible(true);
}
}
</sscce>

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JAVAKB.com
http://www.JAVAkb.com/Uwe/Forums.aspx/JAVA-gui/200710/1



Next Message: Image on a button



Programming | Sports | Autos

copyright 2006
Valid XHTML 1.0 Transitional