Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog.

Similar presentations


Presentation on theme: "Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog."— Presentation transcript:

1 Introduction to GUI in Java 1

2 Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog boxes that you can use them to make your program attractive and user friendly GUI-based programs are implemented by using classes from the javax.swing and java.awt packages. 2

3 Creating GUI Objects // Create a button with text OK JButton jbtOK = new JButton("OK"); // Create a label with text "Enter your name: " JLabel jlblName = new JLabel("Enter your name: "); // Create a text field with text "Type Name Here" JTextField jtfName = new JTextField("Type Name Here"); // Create a check box with text bold JCheckBox jchkBold = new JCheckBox("Bold"); // Create a radio button with text red JRadioButton jrbRed = new JRadioButton("Red"); // Create a combo box with choices red, green, and blue JComboBox jcboColor = new JComboBox(new String[]{"Red", "Green", "Blue"}); 3 LabelText field Chec k Box Radio Butto n Combo Box

4 Frames  Frame is a window that is not contained inside another window. Frame is the basis to contain other user interface components in Java GUI applications.  The JFrame class can be used to create windows.  For Swing GUI programs, use JFrame class to create widows. 4

5 Example 5

6 Creating Frames 6 import javax.swing.*; public class MyFrame { public static void main(String[] args) { JFrame frame = new JFrame("Test Frame"); frame.setSize(400, 300); frame.setVisible(true); }

7 JFrame Class 7

8 Creating a JFrame  a JFrame class have the following component:  setResizable(boolean var);  setDefaultCloseOperation(JFrame. *********);  EXIT_ON_CLOSE  DO_NOTHING_ON_CLOSE 8

9 Example 9

10 10 After Pressing OK or closing the “How are you?” dialog, the “Good Bye” dialog appears

11 Using Dialog Boxes for Input/Output JOptionPane JOptionPane this class is contained in the package javax.swing We will use two methods of this class : showInputDialog showMessageDialog 11

12 ShowInputDialog 12 General syntax Str= JOptionPane.showInputDialog(ParentComponent,string expression, box title, message type); This method returns the input as a String value Example: str=JOptionPane.showInputDialog(FrameOne,"Enter your name“, “Input”,QUESTION_MESSAGE);

13 showMessageDialog 13 General syntax JOptionPane.showMessageDialog(ParentComponent, message string, box title, message type);

14 Adding pane to a Frame 14

15 15

16 Example 2 16

17 17

18 Message Type 18 JOptionPane.showMessageDialog(Jframe/null,"Hello","Title", JOptionPane.*********); INFORMATION_MESSAGE ERROR_MESSAGEWARNING_MESSAGE QUESTION_MESSAGE PLAIN_MESSAGE

19 Example for adding two numbers 19

20 20

21 Subclassing JFrame 21 To create a customized frame window, we define a subclass of the JFrame class. class TESTGUI extends JFrame { Public TESTGUI () { // constructor // necessary code //set the frame default properties} } write a code to do the following : An instance of TESTGUI will have the following default characteristics: The title is set to My First Subclass. The program terminates when the close box is clicked. The size of the frame is 300 pixels wide by 200 pixels high. The frame is positioned at screen coordinate (150, 250).

22 Jframe Subclass 22

23 23 This gray area is the content pane of this frame. The content pane is where we put GUI objects such as buttons, labels, scroll bars, and others. We access the content pane by calling the frame’s getContentPane method in class Container.

24 Accessing the content pane of a JFrame  To retrieve the content pane of a JFrame: frame.getContentPane();  To add a component to the content pane: frame.getContentPane().add(yellowLabel);  To remove a component from the content pane: frame.getContentPane().remove(yellowLabel); 24

25 25 Using Panels as Sub-Containers  Panels act as sub-containers for grouping user interface components.  It is recommended that you place the user interface components in panels and place the panels in a frame. You can also place panels in a panel.  To add a component to JFrame, you actually add it to the content pane of JFrame. To add a component to a panel, you add it directly to the panel using the add method.

26 26 Creating a JPanel You can use new JPanel() to create a panel with a default FlowLayout manager or new JPanel(LayoutManager) to create a panel with the specified layout manager. Use the add(Component) method to add a component to the panel. For example, JPanel p = new JPanel(); p.add(new JButton("OK"));

27 Layouts 27

28 28 Layout Managers  Java’s layout managers provide a level of abstraction to automatically map your user interface on all window systems.  The UI components are placed in containers. Each container has a layout manager to arrange the UI components within the container.  Layout managers are set in containers using the setLayout(LayoutManager) method in a container.

29 29 Kinds of Layout Managers  FlowLayout  GridLayout  BorderLayout

30 30 FlowLayout Example FlowLayout is the default layout manager for every JPanel. It simply lays out components in a single row, starting a new row if its container is not sufficiently wide.  If container is more than wide enough, components are centered  Can change alignment using FlowLayout.setAlignment()

31 31 The FlowLayout Class

32 GridLayout 32 GridLayout simply makes a bunch of components equal in size and displays them in the requested number of rows and columns.

33 GridLayout 33

34 BorderLayout 34 Every content pane is initialized to use a BorderLayout. the content pane is the main container in all frames, applets, and dialogs.) ABorderLayout places components in up to five areas: top, bottom, left, right, and center.

35 BorderLayout 35

36 36 1. Create it  Instantiate object: JButton b = new JButton(); 2. Configure it  Methods: b.setText(“Press me”); 3. Add it  panel.add(b); 4. Listen to it  Events: Listeners Press me Adding components to Frame

37 37 import javax.swing.*; class Hello { public static void main(String[] args){ JFrame f = new JFrame(“Hello World”); JPanel p = new JPanel(); JButton b = new JButton(“press me”); p.add(b);// add button to panel f.getContentPane().add(p); // add panel to frame } press me


Download ppt "Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog."

Similar presentations


Ads by Google