In all of the previous steps, we have worked within the context of the browser's window. There are times, however, when you may wish for your applet to have its own window. The code that follows does this. This is done using aFrame
as the outer container, rather than aPanel
.Example Applet
import java.applet.Applet; import java.awt.*; public class step7 extends Applet{ // provides separate window for components and drawing TextArea ta = new TextArea ("My Text Area", 5, 40); Button button = new Button ("Button"); MyPanel drawPanel = new MyPanel (); Panel topPanel = new Panel ( ); Frame outerBox = new Frame ( ); public void init ( ){ setBackground (Color.white); setForeground (Color.red); topPanel.add (ta); topPanel.add (button); outerBox.setLayout (new BorderLayout ( ) ); outerBox.add ("North", topPanel); outerBox.add ("Center", drawPanel); outerBox.resize (600, 600); outerBox.setBackground (Color.white); outerBox.show( ); } // end init }// end step7 class MyPanel extends Panel { public void paint (Graphics g ) { g.fillRect (50, 50, 100, 100); g.setColor (Color.blue); g.setFont (new Font ("Helvetica", Font.BOLD, 24) ); g.drawString ("Hello, World!", 200, 200); g.setColor (Color.yellow); g.drawOval (300, 50, 100, 100); g.setColor (Color.green); g.fillArc (50, 300, 200, 200, 180, -90); g.setColor (Color.cyan); g.fill3DRect (300, 300, 100, 100, true ); } // end paint } // end MyPanelRun the applet
Discussion
Because the new window does not "inherit" the window of the browser, we have to perform several additional functions, including setting its size and explicitly showing it.