1 / 20

GUI in Java

GUI in Java. What is GUI?. GUI- Graphic User Interface. What is GUI programming different from other programming? It is event-driven, meaning user actions such as clicking on a button or pressing a key on the keyboard generate events, and the program

Télécharger la présentation

GUI in Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. GUI in Java

  2. What is GUI? • GUI- Graphic User Interface. • What is GUI programming different from other programming? • It is event-driven, meaning user actions such as clicking on a button or pressing a key on the keyboard generate events, and the program must respond to these events as they occur.

  3. Is GUI programming OOP?(object oriented programming) • Yes, Events are objects. Colors and fonts are objects. GUI components such as buttons and menus are objects. Events are handled by instance methods contained in objects. • There are 2 types of GUI program in Java: • stand-alone applications • applets

  4. Application type of GUI • What do we need for this programming? • Java package Javax.swing • Two important classes in this package: Jframe, JPanel and JLabel.

  5. Swing components JLabel JFrame JPanel

  6. JFrame • The JFrame class is used to instantiate a frame. A frame is a window with a border, title, and buttons for minimizing, maximizing, and closing the frame. • A frame is a top-level container for a GUI, which holds and displays all the other components of an interface in a content frame

  7. setDefaultLookAndFeelDecorated(boolean) sets the frames created after this class method is called to have Java Window decorations, such as borders and a title, when the boolean argument is true. • setDefaultCloseOperation(class_constant) sets the operation that occurs when the user clicks the Close button. The class_constant argument is usually JFrame.EXIT_ON_CLOSE. • getContentPane() returns a Container object corresponding to the content pane. Jframe methods

  8. Jframe methods • setContentPane(Container contentPane) sets the content pane to content Pane. The Container class is the superclass for the JPanel class, a class for creating content panes. • pack() sizes the frame so that all of its contents are at or above their preferred sizes. • setSize(int width, int height) set the dimension of the frame • setVisible(boolean) displays the frame when the boolean argument is true.

  9. JPanel methods • add(component GUIComponent) adds a GUIcomponent to the content pane. When added, components are given an index value, with the first component at index 0. • remove(int index) removes the component with index value index.

  10. JLabel • Labels, created with the JLabel class, are used to display text that cannot be changed by the user

  11. JLabel methods • JLabel(String str) creates a JLabel object with text str. • JLabel(String str, align_constant) creates a JLabel object with text str and alignment align_constant that can be set to JLabel class constants LEADING or TRAILING, which are left and right alignment, respectively. • setText(String str) sets the text of the JLabel object to str.

  12. import javax.swing.*; public class HelloSwing { final static String LABEL_TEXT = "Hello, world!"; JFrame frame; JPanel contentPane; JLabel label; public HelloSwing(){ /* Create and set up the frame */ frame = new JFrame("HelloWorldWithGUI"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  13. /* Create a content pane */ contentPane = new JPanel(); /* Create and add label */ label = new JLabel(LABEL_TEXT); contentPane.add(label); /* Add content pane to frame */ frame.setContentPane(contentPane); /* Size and then display the frame. */ frame.pack(); frame.setVisible(true); }

  14. Other Swing classes • JButton: A button can be clicked by the user to communicate with the application. constructor - JButton(String str) • JTextField: allows a user to enter information at run time. Text fields are usually placed next to a label to prompt the user for the type of data expected in the text field. constructor - JTextField(int col)

  15. practice Use what we learned so far in the class and produce the following screen

  16. JButton class • A button can be clicked by the user to communicate with the application. • How do we implement the communication? • Swing components use listeners to determine if an event has occurred. • For example, the HelloSwing application includes a button that when clicked either hides or displays text in the label

  17. Handling events • Swing components use listeners to determine if an event has occurred. A listener is an object that listens for action events. • When an event is heard, the listener responds by executing an event handler named actionPerformed(). • The actionPerformed() method has an ActionEvent parameter passed by the GUI when an event occurs.

  18. 3 components needed for event handling. 1) interface ActionListener – class has to implement the interface 2) class to include for the interface - import java.awt.event.*; 3) the only method in the interface actionPerformed(ActionEvent event)

  19. JButton methods • Jbutton(String str) creates a JButton object displaying the text str. • setActionCommand(String cmd) sets the name of the action performed when the button is clicked to cmd . • getActionCommand() returns the name of the action that has been performed by the button. • addActionListener(Object) adds an object that

  20. JButton button = new JButton("Hide"); button.setActionCommand("Hide"); button.addActionListener(this); contentPane.add(button); • a button that displays Hide is created. • because the user will see a Hide button the action command associated with this button should be “Hide”. • a listener is needed to determine when the user clicks the button. The listener is set to the HelloSwing object itself (this). 4) the button is added to the content pane.

More Related