![]() ![]() ![]() ![]() |
Creating an Applet User Interface |
Applets use the AppletgetParameter()
method to get user-specified values for applet parameters. ThegetParameter()
method is defined as follows:public String getParameter(String name)Note that
getParameter()
always returns a string value. Your applet might need to convert this value into another form, such as an integer. The java.lang package provides classes such as Integer that you can use to help with converting strings to primitive types. Here's an example of converting a parameter's value into an integer:Note that if the user doesn't specify a value for the WINDOWWIDTH parameter, the above code uses a default value of 0, which tells the program to use the window's preferred size. It's important that you supply default values wherever possible.int requestedWidth = 0; . . . String windowWidthString = getParameter("WINDOWWIDTH"); if (windowWidthString != null) { try { requestedWidth = Integer.parseInt(windowWidthString); } catch (NumberFormatException e) { requestedWidth = 0; } }An Example: AppletButton
Below is the AppletButton code that gets its parameters. For more information on AppletButton, see the previous page.String windowClass; String buttonText; String windowText; int requestedWidth = 0; int requestedHeight = 0; . . . public void init() { windowClass = getParameter("WINDOWTYPE"); if (windowClass == null) { windowClass = "TestWindow"; } buttonText = getParameter("BUTTONTEXT"); if (buttonText == null) { buttonText = "Click here to bring up a " + windowClass; } windowText = getParameter("WINDOWTEXT"); if (windowText == null) { windowText = windowClass; } String windowWidthString = getParameter("WINDOWWIDTH"); if (windowWidthString != null) { try { requestedWidth = Integer.parseInt(windowWidthString); } catch (NumberFormatException e) { requestedWidth = 0; } } String windowHeightString = getParameter("WINDOWHEIGHT"); if (windowHeightString != null) { try { requestedHeight = Integer.parseInt(windowHeightString); } catch (NumberFormatException e) { requestedHeight = 0; } }
![]() ![]() ![]() ![]() |
Creating an Applet User Interface |