Java Lecture 5 AWT
Total Page:16
File Type:pdf, Size:1020Kb
Java Lecture 5 AWT Abstract Windows Toolkit: A Java window is a set of nested components, starting from the outermost window down to the smallest user interface (UI) component. Peers: Peers are native GUI components (native to the platform). When a Java program creates and displays a Java AWT component, that component actually creates and displays a native component (called a peer). Thus, under Windows 95, a windows 95 button, or dialog, etc. is generated. Under Solaris a Motif peer and under Macintosh, a Macintosh peer. Components include seeable things such as: windows menu bars buttons text fields and non-seeable things like containers which can include other components. Containers have layout managers that deal with positioning and shaping of components. The nesting of components within containers within other components creates a hierarchy for painting and event passing. Major components: Containers: Containers are generic AWT components that can contain other components, including other containers. The most common form of container is the panel, which represents a container that can be displayed on screen. Applets are a form of panel (in fact, the "Applet" class is a subclass of the "Panel" class). Canvases: A canvas is a simple drawing surface. Although you can draw on panels, (as you've been doing all along), canvases are good for painting images or other graphics operations. UI components: These can include buttons, lists, simple popup menus, checkboxes test fields, and other typical elements of a user interface. Window construction components: These include windows, frames, menu bars, and dialogs. These are listed separately from the other UI components because you'll use these less often--particularly in applets. In applets, the browser provides the main window and menu bar, so you don't have to use these. Your applet may create a new window, however, or you may want to write your own Java application that uses these components. Here’s a hierarchy: UI Components: An applet is a container (see the hierarchy). So we can add UI components directly. Create a simple applet (call it See). Then do the following: public String getAppletInfo() { return ""; } public void paint(Graphics g) { } Button: public void init() { resize(520, 440); // Adds the specified component to the end of // this container. add( new Button("Stop") ); add( new Button("Start") ); add( new Button("FF") ); add( new Button("Rew") ); } Labels: public void init() { resize(520, 440); add( new Label(" right", Label.RIGHT) ); } Check boxes: public void init() { resize(520, 440); add( new Checkbox("Frogs") ); // true indicates a check, null ignore for now add( new Checkbox("Cows", null, true) ); add( new Checkbox("Toads") ); } Radio Buttons: Radio buttons are just like checkboxes EXCEPT only one in a series can be selected. public void init() { resize(520, 440); CheckboxGroup cbg = new CheckboxGroup(); add( new Checkbox("Democrat", cbg, false) ); add( new Checkbox("Republican", cbg, true) ); add( new Checkbox("Socialist", cbg, false) ); add( new Checkbox("DIS Major", cbg, false) ); } Choice Menus: Choice menus are popup or pulldown menus that enable you to select an item from that menu. Only one item can be selected. For multiple selections use a scrolling list. public void init() { resize(520, 440); Choice c = new Choice(); c.addItem("New"); c.addItem("Open"); c.addItem("Close"); c.addItem("Exit"); // now add the menu add(c); } There are several menu functions: getItem(int i) Returns ith string item. countItem() Number of items in list. getSelectedIndex() Returns selected position number. getSelectedItem() Returns selected position item. select(int i) Selects ith item. select(String s) Selects item s. Text Fields: Enables user to enter text. public void init() { resize(520, 440); // whole text field, including prompt, is 30 bytes long add(new TextField("Enter your name", 30) ); } Text Areas: Enables user to enter large amounts of text. Here's the code: public void init() { resize(520, 440); // 10 rows 30 columns add( new TextArea(10, 30) ); } Scrolling Lists: A scrolling lists contains many items. You can restrict selection to one item (exclusive) or multiple (nonexclusive). Here's the code: public void init() { resize(520, 440); List m = new List(5, true); // true = multiple selections m.addItem("Democrat"); m.addItem("Republican"); m.addItem("Socialist"); m.addItem("Ardvark"); m.addItem("Cow"); m.addItem("MBA"); m.addItem("DIS Major"); add( m ); } Panels and Layouts: In Visual C++ we had to tell where exactly UI components went by pixel positions. Since Java runs on so many platforms, we need something more flexible. Java has layout managers, insets and hints that each component can provide for helping lay out the screen. Two things determine the eventual layout: The order the component were added to the panel The panel's layout manager. Each panel can have its own layout manager. There are five different types of layout managers. Each has a different basic strategy for laying-out components. We discuss each in turn with examples. Here’s a brief description. BorderLayout A container is divided into regions: north and south or east and west, and center. CardLayout The visibility of a set of components gets controlled with this layout. This is like a deck of cards or a stack of tabbed folders. FlowLayout A container’s components fill the container from right to left, then top to bottom. GridLayout A container divides its components into a grid. GridBagLayout This allows for complex layouts. Various containers have default layout managers. They are: Container null Panel FlowLayout Window BorderLayout Dialog BorderLayout Frame BorderLayout FlowLayout: Components are added one at a time, row by row. If a component won't fit on the current row, it goes to the next. You can specify the alignment too. This is the default layout (with CENTERED). Here's how it works in code. public void init() { resize(520, 440); this.setLayout( new FlowLayout(FlowLayout.LEFT, 10, 20) ); // horizontal & vertical pixel gap CheckboxGroup cbg = new CheckboxGroup(); add( new Checkbox("Democrat", cbg, false) ); add( new Checkbox("Republican", cbg, true) ); add( new Checkbox("Socialist", cbg, false) ); add( new Checkbox("DIS Major", cbg, false) ); } Grid Layout: With a grid, you portion off the panel area into rows and columns and place a component in a cell. public void init() { resize(520, 440); // two row and two columns this.setLayout( new GridLayout(2, 2, 10, 20) ); CheckboxGroup cbg = new CheckboxGroup(); add( new Checkbox("Democrat", cbg, false) ); add( new Checkbox("Republican", cbg, true) ); add( new Checkbox("Socialist", cbg, false) ); add( new Checkbox("DIS Major", cbg, false) ); } GridBagLayout: This is the most versatile layout. You can create a grid of arbitrary size and use it to create components within the grid of arbitrary size. Each component that resides in a container that is using a GridBagLayout has an associated GridBagConstraints instance that specifies how the component is laid out. Components may span more than one grid cell and can even overlap. Here's an example: and for a different sized window: and the code is below. First some background on GridBagConstraints. This is a separate class. Each component added to the container with a GridBagLayout has a GridBagConstraints object. The typical code sequence goes as follows: GridBagLayout gbl = new GridBagLayout(); GridBagConstraints gbc = new GridBagConstraints(); setLayout(gbl); … // set constraints … gbl.setConstraints( component, gbc); add(component); A GridBagConstraints object has several variables that the user can control. They are: Variable Default value Values Means anchor CENTER CENTER, EAST, Where to anchor a component NORTH, within its grid cells NORTHEAST, NORTHWEST, SOUTH, SOUTHEAST, SOUTHWEST, WEST fill NONE BOTH, The manner in which the HORIZONTAL, component fills the grid cells VERTICAL, it occupies. NONE gridx RELATIVE RELATIVE or The position of the gridy integer x, y position component’s upper left-hand in the grid grid cell. gridwidth 1 RELATIVE, The number of grid cells in gridheight 1 REMAINDER, or both horizontal and vertical integer values directions allotted for the representing the component. Whether or not a width and height in component fills its grid cells grid cells depends on the fill attribute. ipadx 0 Integer values Internal padding that increases ipady 0 representing the the components preferred size. number of pixels Negative values are allowed, which reduces the component’s preferred size. insets (0,0,0,0) An Insets object External padding between the edges of the component and edges of its grid cells. Negative values are allowed, which causes the components to extend beyond the grid cells. weightx 0.0 Double values How extra space is consumed weighty 0.0 representing by the component’s grid cells. weighting given to Whether or not a component a component’s grid fills its grid cells depends on cells relative to the fill attribute. Values must other components be positive. in the same row or column. import java.awt.*; import java.util.*; public class Test extends Frame { // a helper method protected void makebutton( String name, GridBagLayout gb, GridBagConstraints c) { Button b = new Button(name); // Sets the constraints for the specified // component in this layout. gb.setConstraints(b, c); add( b ); } // Constructor Test() { // constructor for Frame super("GridBag Test"); // Layout & Font GridBagLayout gb = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints();