Docsity

GUIs - Introduduction to Jave Programming - Lecture Slides, Slides of Java Programming

Partial preview of the text

Download GUIs - Introduduction to Jave Programming - Lecture Slides and more Slides Java Programming in PDF only on Docsity!

  • GUIs part

Announcements

  • Zach is having an additional office hour: Friday, Dec 7. 10:15 - 11:15am

Sequential vs. Event driven

So far, we have done sequential programming This means we start at the beginning of main() and go one line at a time (See: Sequential.java) higher up runs first

Sequential vs. Event driven

Exceptions no longer obeyed this rule:

  • You could hop to the catch block no matter where the exception was thrown from (inside the try block) This was our first experience of event driven programming, the user could change the order our program ran in (See: SemiSequential.java) Docsity.com

JFrame

The basis of GUIs are windows (or frames) To make a window, you need to make a JFrame object

  • Need to import javax.swing.JFrame In order to actually see it, we need to run the setVisible() method on the JFrame (arg: true) (See: BasicWindow.java) name

JFrame

There are a few other methods that you should normally use to setup the JFrame window: setSize(int width, int height) - Makes the window size width by height pixels setDefaultCloseOperation() - Sets up what clicking the X on top does (default behavior hides the program!) (See: ClosingWindow.java) Docsity.com

ActionListener

The previous code example is rather worthless since the button does not do anything In order to get a useful button you need to implement the ActionListener interface So far we have not strayed too far from sequential programming, but now we will (Note: main() returning does not end the program) Docsity.com

ActionListener

Let's make a class that implements the ActionListener interface: Then we need to override the actionPerfromed() method: (See: RanomDigit.java)

JLabel

JLabel

JLabel is simply some text in a window that does not interact with the user, used for...

  • Helping the user navigate your GUI
  • Convey information To do this, you simply make a JLabel object and then add it to your window (like a JButton) (See: LabelsAndButtons.java)

Make it stop!

(See: MakeItStop.java)

MouseListener

ActionListeners work when you press a button, but it is much more fun to work with a mouse You can implement MouseListener to get information from the mouse: This works very similar to ActionListener, except with more methods