140 likes | 339 Views
GUI. Graphical User Interface (pronounced "gooey") Typically used with applications written for Windows or OSX Event-driven, responding to events such as a mouse click. Creating a GUI. A GUI must begin with a top-level container, such as a frame, for holding a content frame
E N D
GUI • Graphical User Interface (pronounced "gooey") • Typically used with applications written for Windows or OSX • Event-driven, responding to events such as a mouse click
Creating a GUI • A GUI must begin with a top-level container, such as a frame, for holding a content frame • A content frame, such as a panel, holds and displays components and gets added to a container • Components include labels
frame panel label A Swing GUI
JFrame • Part of the java.swing package • The JFrame class is used to create a frame for a GUI • JFrame methods include:setDefaultLookAndFeelDecorated() setDefaultCloseOperation() getContentPane() setContentPane() pack() setVisible()
JPanel • Part of the java.swing package • The JPanel class is used to create a content frame for a GUI • JPanel methods include:add() remove()
JLabel • Part of the java.swing package • The JLabel class is used to create an object for displaying text • JLabel constructors and methods include:JLabel() setText()
JButton • Part of the java.swing package • The JButton class is used to create an object that can be clicked by the user. Provides a means of communication between the user and the interface. • Requires a listener object to determine when the user has clicked the button • JButton constructors and methods include:JLabel() setActionCommand() getActionCommand() addActionListener()
Events • A listener object responds to an event by executing an event handler named actionPerformed() • The actionPerformed() method has a parameter for the action command string, which is passed by the event • The component requiring a listener must set the action command and add an action listener • The class containing a listener must implement the ActionListener interface
Layouts • FlowLayout manager places components one next to the other in a row • BoxLayout manager places components one after the other in a column • GridLayout manager places components into a grid of rows and columns • Borders and alignment can be changed to control layout within a layout manager
JTextField • Part of the java.swing package • The JTextField class is used to create an object where users can enter data • JTextField constructors and methods include:JTextField() getText() addActionListener()
Numeric Data • Data typed into a text field is a string • Class methods in the Double and Integer classes can be used to convert string data to numeric data and the reverse • Double class methods include: parseDouble() toString() • Integer class methods include:parseInteger() toString()
JComboBox • Part of the java.swing package • The JComboBox class is used to create an object where users select a choice from a list of choices • JComboBox constructors and methods include:JComboBox() setSelectedIndex() getSelectedItem() setEditable() addActionListener()
Colors • Swing Components have methods for changing their colors:setBackground() setForeground() • The methods for changing color require a parameter from the Color class • How the color changes varies between components
Images • Many Swing components support images • GIF and JPG image formats work best • The JLabel and JButton classes include the setIcon() method for adding an image to a component • The JLabel and JButton constructors may also specify an image