Frame import java. awt. public class my Frame extends frame i public static void main(String argali My Frame fr =new My Frame("Hello Out There) fr setSize(200, 200); fr set Background( Color. red) fr set visible(true) 無 Hello Out There!上| public My Frame(String str)( super(str)
11 Frame import java.awt.*; public class MyFrame extends Frame { public static void main(String args[ ]) { MyFrame fr = new MyFrame("Hello Out There"); fr.setSize(200,200); fr.setBackground(Color.red); fr.setVisible(true); } public MyFrame (String str) { super(str); } }
Panel Panel is the simplest container class a panel provides space in which an application can attach any other component, including other panelS java. lang Object iava. awt Component +----java.awt container +----java awt Panel 12
12 Panel • Panel is the simplest container class. • A panel provides space in which an application can attach any other component, including other panels
Panel import java. awt. " public class Frame WithPanel extends Frame public Frame WithPanel(String str) super(str); public static void main(String argsDi Frame withPanel fr= new Frame WithPanel(Frame with Panel); Panel pan=new Panel(; fr setsize(200, 200); fr setBackground ( color red); fr. setLayout(nuD); Frame with Panel上口x pan setsize(100, 100); pan setBackground( coloryellow); fr. add(pan); //add pan to frame fr fr set Visible(true);
13 Panel import java.awt.*; public class FrameWithPanel extends Frame{ public FrameWithPanel(String str){ super(str); } public static void main(String args[]){ FrameWithPanel fr = new FrameWithPanel("Frame with Panel"); Panel pan = new Panel(); fr.setSize(200,200); fr.setBackground(Color.red); fr.setLayout(null); pan.setSize(100,100); pan.setBackground(Color.yellow); fr.add(pan);// add pan to frame fr fr.setVisible(true); } }
Layout managers associated with containers automate the layout of elements When elements are added to the container When the window is resized automatically adjust the positions and sizes of the elements
14 Layout Managers • Associated with containers • Automate the layout of elements – When elements are added to the container – When the window is resized • automatically adjust the positions and sizes of the elements
hierarchy of layout managers Container LayoutManager BorderLayout Layout anagen CardLayout FlowLayout GridBaglayout GridLayout Q: Can you identify the design pattern used here? 15
15 Hierarchy of Layout Managers Q: Can you identify the design pattern used here?