GUI

  • JAVA Is popular because of the abilities to easily make GUis
  • AWT: Abstract Windowing Toolkit
    • The early Java way to create GUIs
    • Very east and mostly intuitive
    • AWT stuff is in java.awt.
  • Swing
    • Newer GUI toolkit
    • Built on top of AWT, and lots of similar functionality to AWT
    • Swing stuff is in javax.swing.
    • Swing components start with a J to distinguish from AWT counterparts
  • This class will talk mostly about Swing - older existing programs will use AWT though

JLabel, JButton, and FlowLayout

  • a JLabel is just a bit of text that is dsiplayed
  • User doesn't modify a label directly
  • Jbutton is a button that can be clicked on
  • When designing an interface, decide how you want the components displayed
  • Several different layout managers, available for common ways
  • FlowLayout adds a component immediately to the right of the previous component until theres no more room, then the component is placed on the next row
  • Theses components acan be added to a layout in a frame or jPanel with the add method

Event Handling

  • Java uses an event handling scheme for dealing with users interacting with the components
  • When a user clicks a button, an event is triggered
  • If you don't listen for th event, and write some code to do something with it, your button is still useless
  • There are several different types of event listeners
  • When a user clicks a mouse button, a MouseEvent is triggered, and you can handle it by implementing a MouseListener interface
  • There are also keyboard listeners, window listeners, etc.
  • After implementing the MouseListener interface, you have to register the listener with a component, so it knows it should be listening for events
  • Since listeners are very ...

Adding a Mouse Listener

  • MouseListener is an interface with 5 methods that need to be implemented
  • mouseClicked
  • mouseEntered
  • mouseExited
  • mouseRelease
  • Recall,m an interface is similar to an abstract class with all abstract functions
    • Thereforre all functions need to be implemented
  • Inside the AndresFrame definition, create an inter clas that implments the MousteListener interface

Making GUIS Look Like They're Supposed To

  • What if I ahve a certain layout of my GUI compeonetns I need to implement?

Other Layout Managers

  • FlowLayout:
    • components flow from left to right
    • Things move whne the window is resized
  • GridLayout:
    • Must specify how many rows an columns of components you want
    • Each grid space is the same ize, so some components can come out looking out of proportion
  • BorderLayout:
    • Most commonly used, most useful
    • Anchored to the top, right, bottom, left, or middle of the area
  • BoxLayout:
    • Aligns components horizontally or vertically

BoxLayout and Box

  • BoxLayout is aonother layout manager that is often useful
    • Places components in a straight line that won't wrap like FlowLayout
    • Box is not a layout manager - it is a component
      • It can be placed anywhere any component cant by a layout manager
      • Box is sort of like a JPanel with BoxLauouy as its default manager

Changing JLabels

results matching ""

    No results matching ""