1

I want to add a JLable on my JDesktopePane..i wrote the below given code..but the Label is not displayed on the pane.

   {
      frame1.setContentPane(desktop);
      frame1.setSize(900,700);
      frame1.setVisible(true);
      desktop.setBackground(Color.DARK_GRAY );
      JLabel label1 = new JLabel("Main Page", SwingConstants.CENTER);
      label1.setFont(new Font("SansSerif",Font.ITALIC + Font.BOLD,54));
      desktop.add(label1);**
   }
Jakub Zaverka
  • 8,816
  • 3
  • 32
  • 48
learner
  • 331
  • 1
  • 9
  • 22

2 Answers2

5

The JDesktop is one of the few containers that does not use a traditional layout manager.

In order for any component to be added to it, that component needs to have it's position and size set manually.

Try something like label1.setBounds(new Rectangle(new Point(10, 10), label1.getPreferredSize())) before you add it

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

Use a JPanel, add JLabel to it and then add JPanel to JDesktopPane

Abubakkar
  • 15,488
  • 8
  • 55
  • 83
  • 2
    doesn't matter how deep the added hierarchy is - without explicit sizing/locating it will not show up in a layout-free container like JDesktopPane :-) – kleopatra Oct 01 '12 at 11:38