Professional Documents
Culture Documents
Graphical User Interfaces (Guis)
Graphical User Interfaces (Guis)
Graphical User Interfaces (Guis)
AWT and Swing are each a collection of Java classes for GUI development
Java GUI facilities are large and complex. We focus only on a small subset
AWT vs. Swing
• AWT advantages over Swing
1.Simpler to learn and use
2.Less idiosyncracies
• Swing advantages over AWT
1.Much greater functionality
2.Portability. Consistent look and feel across platforms
3.Vendor support. AWT functionality is frozen
4.Built-in double buffering overlaps processing with I/O
Containers and Components
Java GUI applications principally consist of containers and components
• Container
– A Java class instantiated to hold groups of components
– Examples: JApplet, JFrame, JPanel, JTabbedPanel,
JScrollPane
• Component
– A Java class instantiated to create a type of GUI object
– Examples: JButton, JCheckBox, JComboBox,
JColorChooser, JFileChooser, JLabel, JList, JMenu,
JOptionPane, JPasswordField, JRadioButton, JSlider,
JTextArea, JTextField, JToggleButton, JTree
Layout Managers
Objects that controls how a container's components display
Designers likely would create a class for each of the five sections
General Comments
• It is not hard to create a GUI application, but it can
be tedious
• There is a drag and drop capability to create GUI
components but it creates unreadable code
• GUIs in java have many single line Statements
– To create GUI components
– To set GUI properties
– To call special methods
• Good design
– Break up GUI applications to a set of panel or
component classes.
– This makes the code easier to maintain
Steps to create a GUI Application
1. Create a class hierarchy diagram for the application
2. Determine components should be in their own Java class
3. Instantiate the application's JFrame with its title
4. Configure the window close procedure
5. Instantiate the components and set their properties
6. Call the JFrame getContentPane() method to get the
default application container.
7. Add the components to the JFrame application container
8. Set the size of the frame
9. Make the frame visible
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("example.fxml"));
stage.setTitle("FXML Welcome");
stage.setScene(new Scene(root, 300, 275));
stage.show();
}
}
Simple FXML Example
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import fxml.MyGroup?>
<MyGroup xmlns:fx="http://javafx.com/fxml">
<children>
<Button text="Click Me!" onAction="#handleButtonAction" />
<Label fx:id="label" translateX="0" translateY="30" text=""/>
</children>
</MyGroup >