Ellen Walker Hiram College Java GUI Programming 1 Ellen Walker Hiram College
What is a GUI? Graphical User Interface Easier (for most people) to use More complex to program
Why Java? Java compiles for a virtual machine “Write once run anywhere” One program looks nice on: Linux Windows Macintosh Can also generate “applets” to run from Web pages (not today)
GUI Programs are Event Driven Signal that something “interesting” has happened Example: user presses a button, user moves the mouse Main program reacts to events while(true){ Wait for an event React to it }
Sequential vs Event-Driven Programmer is in complete control Program causes a sequence of actions Event driven User is in complete control User causes events, events cause program actions “flow of control” - control structures vs. events
Major Libraries Used Abstract Window Toolkit (java.awt.*) - for generating graphics Rectangle, Polygon, etc. AWT events (java.awt.event.*) ActionEvent, MouseEvent, etc. SWING (javax.swing.*) - for generating units to interact with (some units generate events) Frames (windows) Buttons Labels & Text information
Some Swing Classes JFrame JLabel JTextField JTextArea JButton Represents window with title & border JLabel Displays text or an image (cannot be changed) JTextField Area for the user to enter one line of text JTextArea Area for the user to enter multiline text JButton A push button (to connect to an action)
Structure of a GUI Program Main method Creates an instance of a GUI class GUI class (implements ActionListener) Constructor configures components Action Performer processes events Event dispatcher The while loop that waits for events and calls the action performer You don’t have to write this. Other listeners exist, e.g. window listener, mouse listener
WindChill Calculator Puts up a window with 1 JTextArea (not to be edited) 3 Jlabels (F temp., wind, windchill) 3 JTextFields (one for each label) 1 Jbutton (“Run) When “Run” is clicked, windchill temp. is computed & displayed in its field, using text from F temp & wind fields Run this.
WindChill Calculator Pseudocode public class Windchill implements ActionListener { static final variables for setup info instance variables for components window, labels, textfields, etc. public Windchill() //constructor public void actionPerformed(ActionEvent e) //event processor }
Constructor Configure GUI window.setVisible(true); //after all that! Set window size & default close operation Specify properties of objects, e.g. legendArea is not editable Register event listener (associate it with the button that sends the event) Arrange components in the GUI window.setVisible(true); //after all that!
Registering Event Listeners Every object that can send an event needs to be associated with a Listener object that processes that event (using its ActionPerformed method) Usually, the code to set this up is in the Listener’s constructor Example (“this” is the ActionListener class): runButton.addActionListener(this);
Arranging Interface Objects Every JFrame has a LayoutManager that determines locations of new components FlowLayout - each added object goes to the right of previous object (or below if there isn’t room to the right) GridLayout - objects are arrayed in a specified number of columns and/or rows JFrame’s add method places each interface component according to the layout E.g. window.add(legendArea);
ActionPerformed Takes one parameter, an ActionEvent In this case: One ActionPerformed method can handle several different actions (using a case statement) In this case: Event is when runButton is pressed Action is to compute the windchill and display it in the appropriate text area