• Post Reply Bookmark Topic Watch Topic
  • New Topic

AWT vs SWING  RSS feed

 
Dee Reavis
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does this SWING code work:
import java.awt.*;
import javax.swing.*;
public class tcenter extends JFrame
{
tcenter(){
super("Testing AWT");
JPanel p2 =new JPanel();
Container p =getContentPane();
p.setLayout(new BorderLayout());
setBackground(Color.white);
setForeground(Color.white);
p2.setBackground(Color.red);
p2.setForeground(Color.red);
Button n = new Button("New Button");
Button s = new Button("Second Button");
n.setBackground(Color.blue);
p.add(BorderLayout.NORTH, n);
p.add(BorderLayout.SOUTH, s);
p.add(BorderLayout.CENTER, p2);
System.out.println(isVisible() + " 1 " + isShowing());
setSize(400, 400);
setVisible(true);
System.out.println(isVisible() + " 2 " + isShowing());
}
public static void main(String[] arg){
new tcenter();
}
}

But this AWT version doesn't:
import java.awt.*;
public class tcenter extends Frame
{
tcenter(){
super("Testing AWT");
Panel p2 =new Panel();
Container p =new Container();
p.setLayout(new BorderLayout());
setBackground(Color.white);
setForeground(Color.white);
p2.setBackground(Color.red);
p2.setForeground(Color.red);
Button n = new Button("New Button");
Button s = new Button("Second Button");
n.setBackground(Color.blue);
p.add(BorderLayout.NORTH, n);
p.add(BorderLayout.SOUTH, s);
p.add(BorderLayout.CENTER, p2);
System.out.println(isVisible() + " 1 " + isShowing());
setSize(400, 400);
setVisible(true);
System.out.println(isVisible() + " 2 " + isShowing());
}
public static void main(String[] arg){
new tcenter();
}
}
I know that the getContentPane() method makes the SWING work, but there isn't an equivalent method in AWT. What do I need to make the AWT work right?
 
Daniel Searson
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In AWT you just call add() and setLayout() directly (as methods of your class) with using getContentPane() first.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!