import java.applet.*; import java.awt.*; public class GraphicsObjectPlacementWithApplet extends Applet { public void init() { // Note that using no layout (or a null layout) is considered by some to be // a bad idea; there are a variety of different types of layout managers // that can be used to avoid absolute positioning of elements; by specifying // the exact location of objects on an applet window, if the window is // resized by a user, or if the program is run on a computer with a lower // screen resolution, it's possible that not all of the elements will fit // (be visible) on the applet window this.setLayout(null); // Set the size and background color of the applet window this.setSize(400, 400); this.setBackground(Color.RED); // Create a button with a caption Button buttonDave = new Button("I am a button!"); // Manually set (in pixels) both the position and size of the button buttonDave.setBounds(5, 30, 180, 50); // Set font and color properties of the button buttonDave.setBackground(Color.YELLOW); buttonDave.setForeground(Color.BLUE); buttonDave.setFont(new Font("Arial", Font.BOLD, 24)); // Create a label and set some of its properties Label labelDave = new Label("I am a label!", Label.RIGHT); labelDave.setBounds(92, 110, 250, 36); labelDave.setBackground(Color.WHITE); labelDave.setForeground(Color.BLACK); labelDave.setFont(new Font("Courier", Font.BOLD, 18)); // Create a textfield and set some of its properties; note that because // the 'setBounds' method is used, the integer argument is optional // when creating the textfield TextField fieldDave = new TextField(20); fieldDave.setText("Type in this textfield..."); fieldDave.setBounds(210, 256, 185, 20); fieldDave.setBackground(Color.BLACK); fieldDave.setForeground(Color.WHITE); // Create a checkbox and set some of its properties Checkbox checkDave = new Checkbox(" This is a checkbox"); checkDave.setBounds(40, 322, 180, 20); checkDave.setBackground(Color.RED); checkDave.setForeground(Color.CYAN); checkDave.setFont(new Font("Cambria", Font.BOLD, 14)); // Add the objects to the (null) layout (make them visible); notice that no // explicit 'paint' method is needed, since components know how to paint // themselves; when an applet is repainted, it calls not only its own // 'paint' method, but also the 'paint' methods for all of the components // that it contains this.add(buttonDave); this.add(labelDave); this.add(fieldDave); this.add(checkDave); } }