Dialog Boxes & GUI's
A Graphical User Interface (or GUI) is a screen element designed to assist users with their computer interactions. Common GUIs include windows, icons, checkboxes, radio buttons, sliders, buttons and the like. Like other graphical elements, programs using GUIs must import both javax.swing.* and java.awt.*
A JFrame is an alternative to an applet. Applets are designed to be embedded within the html of a webpage, whereas JFrames are more likely to be used in stand alone programs. Here's an example of Java code that creates a JFrame:
A JFrame is an alternative to an applet. Applets are designed to be embedded within the html of a webpage, whereas JFrames are more likely to be used in stand alone programs. Here's an example of Java code that creates a JFrame:
This code would produce the following JFrame with a single button:
Note that to add a GUI element to a JFrame, you must first access the area inside the JFrame. This area is called the ContentPane. So, for example, if we wanted to add a CheckBox to our JFrame, we would do so like this:
"SimpleFrame" is the name of the class the code is written in. The String inside the parenthesis, “Yes”, would appear next to the checkbox. Alternatively, we could set up a variable to represent the ContentPane and then use that variable to add the component like this:
Then we wouldn’t need to write out getContentPane( ) each time we accessed the ContentPane.
Either way, your GUI checkbox would look like this in the JFrame:
Either way, your GUI checkbox would look like this in the JFrame:
Here's a diagram showing the parts of a JFrame: