Graphical User Interface Programming Using Java

Ace your homework & exams now with Quizwiz!

displayPanel

(private static class HelloworldDisplay extends JPanel{ public void paintComponent(Graphics g){ super.paintComponent(g); g.drawing("Hello world!", 20, 30); } } )

ActionEvent

A button has been clicked or menu item has been double-clicked. This is considered a "high-level" or "semantic" event because additional processing logic was applied to mouse or keyboard primitive events.

Java Foundation Classes(JFC). This classes form java graphical framework for building robust graphical user interfaces. The foundation classes consist of:

Abstract Windows Toolkit(AWT) Swing Classes Java 2D

Common events

ActionEvent AdjustmentEvent ItemEvent FocusEvent MouseEvent KeyEvent TextEvent WindowEvent

The Layered Pane

Allow components to be layered on top of each other in what is known to the Z order.

JSplitPane

An intermediate container which allows the programmer the luxury of splitting up a GUI and been able to re-size the split. It allows you to place components side by side in a horizontal split(JSplit.Horizontal_SPLIT) or one on top of the other in a vertical split within a single pane.

There are two basic types of GUI programs in Java

Applets and Stand-alone application

Java's Layered Approach to Designing GUIs

Atomic Java components(JButtons, JLabels etc ) are place in an intermediate container(JPanel) and the intermediate container is place in the contentPane of top level container(JFrame/JApplet/JDailog) JPanel and contentPane have by default layout managers to help position components.

Setting properties of the JFrame window

Before a window could be set visible some its properties are set. The properties includes: window.setContentPane(content); window.setSize(250,100); window.setLocation(100,100);

FocusEvent

Components have gained or lost focus.WindowEvent. The status of the window has changed.

KeyEvent

Depressing or release=ingany key creates a KeyEvent.

Top level containers

Each time either a JFrame, JApplet or JDailog is constructed that object in turn constructs for itself a rootPane, layeredPaned, contentPane and glassPane.

Setlayout Method (SetLayout())

Every container has an instance of the method,(setLayout), that takes

Three layout managers to be discussed

FlowLayout BorderLayout GridLayout

AWT components

Frame Panel TabbedPane Button Label TextField

Event Handling

GUI are largely event driven; that is the program waits for events that are generated by the user's actions(or by some other cause) When an event occurs, the program respond by executing the event handling method. In order to program the behavior of a GUI, you must write the event handling methods to respond to the events that you are interested in.

The Glass Pane

Hidden, by default. It's completely transparent and it can be used to intercept input events for the root pane.

Intermediate containers

In Java intermediate containers help display and arrange atomic components through the use of Layouts managers.

JFrame, JApplet and JDialog object

In a Java GUI programs, each GUI component in the interface is represented by an object in the program. On of the most fundamental types example of a component is a window.

An Applet?

Is a program that runs in a rectangular or square area on a Web page.

A stand-alone application

Is a program that runs on its own without depending on a Web browser. Any class that have the main method (main()) routine defines a stand alone program; running the program means executing the main method (main()) routine.

Event Listeners

Is an object that includes one or more event-handling methods. When an event is detected by the another object, such as a button or menu, the listener object is notified and it responds by running the appropriate event-handling method. The event is represented by the event an object belonging to the class ActionEvent.

JPanel

Is another fundamental classes of the swing. It is known as the secondary container in that, itself it does not have the capability of displaying the anything on the screen.

JTabbedPane

Is used essentially to switch between tabbed JPanel quickly.

Containers in Java GUI Programming Top level containers It is important to remember that only a top level container has the ability to display any graphic. There are three top level containers:

JFrame JApplet JDialog

Swing compenets

JFrame JPanel JTabbedPane JButtons JLabels JTextFiled

Declaring a variable of JFrame

JFrame window = new JFrame("GUI Test"); The parameters in the constructor,"GUI Test", specifies that the title will be displayed in the title of the window. The line creates the window object but the window itself is not yet visible on the screen.

Values of the default close operation

JFrame.DO_NOTHING _ON_CLOSE JFrame.HIDE_ON_CLOSE JFrame.DISPOSE_ON_CLOSE

Special Purpose Containers

JInternalFrame JLayedPane JRootPane

Atomic Components

JLabel JButton JTextField JCheckbox JRadioButton JTextArea JPasswordField JDesktopPane JFileChooser JcolorChooser JSpinner JTree JEditorPane JComboBox JList JSeperator JToolTip JSlider JMenu JProgressBar JTable JTextEditor

List of intermediate containers

JPanel JTabbedPane JSplitPane JscrollPane JMenuBar JToolBar

JFrame window

Java has a built-in class to represent windows. There are several types of windows but the common type is represented by the JFrame class which is included in the package javax.swing. JFrame is an independent window that can act as the main window of the application.

Layout managers

Java has a variety of standard layout managers that can be used as parameters in the setLayout() method. They are defined in the package java.awt.

JFrame inheritance

Object Component Container Window Frame JFrame

WindowEvent

Occurs when a window is open or close.

A GUI program

Offers a much richer type of user interface, where the user uses a mouse and keyboard to interact with the GUI components such as windows, menus, buttons, check boxes, text inputs boxes, scroll bars and son on. The main method(main()) routine of a GUI program creates one or more such components and display the on a computer screen.

JScrollPane

Provides a view port with scrollbars that allow the user to size the container without losing the ability to see all the contents.

Two basic types of events

Sematic (High level) Low level

setContentPane method (window.setContentPane(content))

Sets the content of the window. This example assumes that the content was created before this method was call.

setSize method (window.setSize(250,100))

Sets the dimensions of the window. The window is set to have 250pixels width and 100pixels wide.

setLocation method (window.setLocation())

Sets the location of the window on the screen. The window will be set 100 pixels away from the left edge of the screen and 100 pixels down from the top.

setDefaultCloseOperation method (setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) )

Sets the window to be closed when the user clicks the close button.

setVisible method (window.setVisible(true))

Sets the window to be visible on the screen. If the method was not called the nothing will be displayed on the screen.

Swing classes

Sun Micro-systems advice that all java programs should use the light weight Swing components. These components are written in Java and and are essentially manipulated first by the JVM before the operating system has to do all the hard of rendering them.

Two ways of useful panel:

The basic panel is just a blank rectangle.To make use of the panel you can: Add components to the panel. Draw something in the panel.

Event handlers

The common technique for handling events in Java is to is to use event listeners.

Registering a listener object

The object listener must be registered to a button using the following statement: okButton.addActionListener(listener); Without this statement, no action will be taken when a button is clicked.

AdjustmentEvent

The setting of scrollbar has been changed.

ItemEvent

The status of an item in a list, checkbox, or checkable menu has been changed. This is also considered to be high level event because of the additional processing.

TextEvent

The text in an object such as a TextField or TextArea has been changed.

MouseEvent

The user has moved, clicked or dragged the mouse within the area of the Java components.

AWT(Abstract Windows Toolkit)

These classes were used in the early versions of Java and they were heavily criticized for being slow as they were known for being slow as they all graphics from the operating system.

Event-handling method

This class implements the ActionListener interface, a requirement for listener object that handle events from buttons. The event handling method is called names actionPerformed, as specified by the ActionListener interface. The method actionPerformed contains the code that will be executed when the button is clicked, in this case, System.exit(0), which terminates the program. private static class ButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { System.exit(0); } }

The content Pane

This is the pane in which you will place your graphical widgets such as panels, buttons, labels, and the like. This pane uses a default layout manager called the BorderLayout in order to aid you in placing components without having to use the x y co-ordinate system. This default layout manager may be changed.

Common methods of the JComponents

This list assumes that we have component called comp: comp.getWidth() and getHeight comp.setEnabled(true) and comp.setEnabled(false) comp.setVisible(true) and comp.setVisible(false) comp.setFont(true) comp.setBackground(color) and setForeground(color) comp.setOpaque(true) comp.setToolTipText(string) comp.setToolTipText(string) comp.setPreferredSize(size)

The Root Pane

This pane manages the content pane and the menu bar.

JPanels in a program

You will find two JPanels in the program: Content, used to contain other components , displayPanel, used for drawing surfaces.

Some frequently used methods of the JSplitPane

public void setDivideLocation(int location) public void setDivideSize(int newSize) public void setContinousLayout(boolean newContinousLayout)


Related study sets

HINF 351 - Chapt. 6 Cost/Benefit Analysis

View Set

Western Civilizations Final Exam Study Sheet

View Set

PSYCHIATRIC/MENTAL HEALTH NURSING

View Set