jwos
jwos

Reputation: 39

what changing my code from label to JLabel work?

this is my origin code and it doesn't work:

//this code is to draw a panel and when you click "start", the Panel p2 changes color

      JFrame frm = new JFrame("1");


frm.setBackground(new Color(150,130,110));
      frm.setSize(400,300);
      frm.setLocation(200,200); 
      Panel p1=new Panel();
      frm.add(p1,BorderLayout.NORTH);
      final Panel p2=new Panel();
      frm.add(p2,BorderLayout.EAST);
      Panel p3=new Panel();
      frm.add(p3,BorderLayout.CENTER);
      p1.setLayout(b2);
      p2.setLayout(b3);
      p3.setLayout(g1);
          });
      Label l1=new Label("max:"+sum);
      p2.add(l1,BorderLayout.NORTH);
      Label l2=new Label("blood:"+blood);
      p2.add(l2,BorderLayout.CENTER);
      bu1.setBackground(new Color(200,100,100));
      p2.setBackground(new Color(100,200,100));
      l1.setBackground(new Color(100,100,200));

frm.setVisible(true);

    }

}

and i find out that if i can make it work just by changing :

  JLabel l1=new JLabel("max:"+sum);
  p2.add(l1,BorderLayout.NORTH);
  JLabel l2=new JLabel("blood:"+blood);

can make it work,so i wonder why?

is it related to the

  final Panel p2=new Panel();

or difference between swing and awt?

Upvotes: 0

Views: 37

Answers (1)

Rogue
Rogue

Reputation: 11483

Swing and AWT are entirely different component layouts. Swing is a lightweight framework provided by java, while AWT relies more on native libraries and is considered "heavyweight". Mixing the two frameworks is almost always buggy, which is likely why you had problems.

Upvotes: 1

Related Questions