Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 GUI 5Lecture 7 GUI - V Overview  Using No Layout Manager  Message Dialog Boxes  Running a Program as an Applet and an Application  More on Handling.

Similar presentations


Presentation on theme: "1 GUI 5Lecture 7 GUI - V Overview  Using No Layout Manager  Message Dialog Boxes  Running a Program as an Applet and an Application  More on Handling."— Presentation transcript:

1 1 GUI 5Lecture 7 GUI - V Overview  Using No Layout Manager  Message Dialog Boxes  Running a Program as an Applet and an Application  More on Handling Mouse Events  More on Handling Keyboard Events  Example Keyboard Events Demo

2 2 GUI 5Lecture 7 Using No Layout Manager Java enables you to place components in a container without using a layout manager. In this case, the component must be palced using the component’s instance method setBounds(), as follows: public void setBounds(int x, int y, int width, int height); This sets the location and size for the components as in the following example: JButton jbt =new JButton(“Help”); jbt.setBounds(10,10,40,20,); The upper-left corner of the Help button, is placed at(10,10); the button width is 40, and the height is 20

3 3 GUI 5Lecture 7 Using No Layout Manager You perform the following steps in order not to use a layout manager: 1-Use the following statements to specify no layout manager: setLayout(null); 2- Add the component to the container: add(component); 3-Specify the location where the component is to be placed, using the setBounds() method as follows: JButton jbt =new JButton(“Help”); jbt.setBounds(10,10,40,20)

4 4 GUI 5Lecture 7 Using No Layout Manager(Example) // ShowNoLayout.java: Place components without using a layout manager import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ShowNoLayout extends JFrame { private JLabel jlbl = new JLabel("Resize the Window and Study No Layout", JLabel.CENTER);; private JTextArea jta1 = new JTextArea("Text Area", 5, 10 ); private JTextArea jta2 = new JTextArea("Text Area", 5, 10 ); private JTextField jtf = new JTextField("TextField"); private JPanel jp = new JPanel(); private JButton jbt1 = new JButton("Cancel" ); private JButton jbt2 = new JButton("Ok" ); public static void main(String[] args) { ShowNoLayout frame = new ShowNoLayout(); frame.setSize(400,200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public ShowNoLayout() { setTitle("Show No Layout"); // Set background color for the panel jp.setBackground(Color.red); // Specify no layout manager getContentPane().setLayout(null); // Add components to frame getContentPane().add(jlbl); getContentPane().add(jp); getContentPane().add(jta1); getContentPane().add(jta2); getContentPane().add(jtf); getContentPane().add(jbt1); getContentPane().add(jbt2); // Put components in the right place jlbl.setBounds(0, 10, 400, 40); jta1.setBounds(0, 50, 100, 100); jp.setBounds(100, 50, 200, 100); jta2.setBounds(300, 50, 100, 50); jtf.setBounds(300, 100, 100, 50); jbt1.setBounds(100, 150, 100, 50); jbt2.setBounds(200, 150, 100, 50); }

5 5 GUI 5Lecture 7 Using No Layout Manager If you run the previous example on Windows with 640 x 480 resolution, the layout size is just right. When the program is run on Windows with a higher resolution, the components appear very small and clump together. When it is run on Windows with a lower resolution, the components cannot be shown in their entirety. If you resize the window, you will see that the location and size of the components are not changes With no layout, the components size and positions are fixed, and can only be changed in the frame with a layout manager. * If you use this approach, your program may look fine on one machine and be useless on others. For this reason, it is advisable to use the layout managers to develop a platform-independent graphical user interface.

6 6 GUI 5Lecture 7 Message Dialog Boxes A dialog box is normally used as a temporary window to receive additonal information form the user or to provide notification that some event has occurred. You can build a variety of dialog boxes in Java. To display a message dialog box, use the static showMessageDialog method in the JOptionPane class public static void showMessageDialog(Component parentComponent,Object message, String title, int messageType) The parentComponent is the parent component of the dialog box, form which the dialog box is launched. The message is the object to display. Often you use a string for message. The title is the title of the dialog box. The messageType determines the type of message to be displayed.

7 7 GUI 5Lecture 7 Message Dialog Boxes There are five message type : 1- ERROR_MESSAGE 2-INFORMATION_MESSAGE 3-WARNING_MESSAGE 4-QUESTION_MESSAGE 5-PLAIN_MESSAGE Each type, except for the PLAIN_MESSAGE type, has an associated icon. You can use the following method to supply your own icons: public static void showMessageDialog(Componet parentComponent, Object message, String title, int messageType, Icon icon)

8 8 GUI 5Lecture 7 Example of Dialog Box import java.awt.*; import java.awt.event.*; import javax.swing.*; public class DialogMessage extends JFrame implements ActionListener { private JButton jbt1, jbt2, jbt3, jbt4, jbt5, jbt6; public static void main(String[] args) { DialogMessage frame = new DialogMessage(); frame.pack(); frame.setTitle("Message Dialog Boxes"); frame.setVisible(true); } public DialogMessage() { // Create panel p1 add three buttons JPanel p1 = new JPanel(); p1.setLayout(new FlowLayout()); p1.add(jbt1 = new JButton("Button 1")); p1.add(jbt2 = new JButton("Button 2")); p1.add(jbt3 = new JButton("Button 3")); // Create Panel p2 and add three buttons JPanel p2 = new JPanel(); p2.setLayout(new FlowLayout()); p2.add(jbt4 = new JButton("Button 4")); p2.add(jbt5 = new JButton("Button 5")); p2.add(jbt6 = new JButton("Button 6")); // Place p1 and p2 in the frame getContentPane().setLayout(new BorderLayout()); getContentPane().add(p1, BorderLayout.SOUTH); getContentPane().add(p2, BorderLayout.CENTER); // Register listeners jbt1.addActionListener(this); jbt2.addActionListener(this); jbt3.addActionListener(this); jbt4.addActionListener(this); jbt5.addActionListener(this); jbt6.addActionListener(this); }

9 9 GUI 5Lecture 7 Example of Dialog Box public void actionPerformed(ActionEvent e) { if(e.getSource() ==jbt1) { JOptionPane.showMessageDialog(this,"You have pressed button 1 ", "For you Information-INFORMATION_MESSAGE-",JOptionPane.INFORMATION_MESSAGE);} else if(e.getSource()==jbt2) { JOptionPane.showMessageDialog(this,"You SHOULD NOT pressed THIS button ", "Wrong Button-ERROR_MESSAGE-",JOptionPane. ERROR_MESSAGE);} else if(e.getSource()==jbt3){ JOptionPane.showMessageDialog(this,"You SHOULD NOT pressed THIS button ", "-WARNING_MESSAGE-",JOptionPane.WARNING_MESSAGE); } else if(e.getSource() ==jbt4) { JOptionPane.showMessageDialog(this,"DID you press button#4 ? ? ", "-QUESTION_MESSAGE-",JOptionPane.QUESTION_MESSAGE); } else if(e.getSource() ==jbt5) {JOptionPane.showMessageDialog(this,"Plain message with no icon ? ? ", "--PLAIN_MESSAGE-",JOptionPane.PLAIN_MESSAGE); } }

10 10 GUI 5Lecture 7 Example of Dialog Box The message dialog box is modal, which means that no other window can be accessed before the message dialog is dismissed

11 11 GUI 5Lecture 7 Running a Program as an Applet and an Application You can implement a main method in an applet that will run as an application or as an applet using the same program. Suppose you have an applet named TestApplet. To enable it to run as an application, all you need to do is add a main method in the applet with the implementation, as follows: public static void main(String[] args) {//create a frame JFrame frame=new JFrame(“Running a program”); // create an instance of TestApplet TestApplet applet =new TestApplet(); //Add the applet instance to the frame Frame.getContentPane().add(applet, BorderLayout.CENTER); //Invoke init and start Applet.init(); Applet.start(); // display the frame Frame.setSize(300,300); Frame.setVisible(true);}

12 12 GUI 5Lecture 7 More on Handling Mouse Events Java provides two listener interfaces, MouseListener and MouseMotionListener, to handle mouse events. The MouseListener listens for actions such as when the mouse is pressed, released, entered, exited, or clicked. The MouseMotionListener listens for actions such as dragging or moving the mouse. The following example can run as an applet and as an application.

13 13 GUI 5Lecture 7 // MoveMessageDemo.java: Move a message in a panel // by dragging the mouse import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MoveMessageDemo extends JApplet { // Initialize the applet public void init() { // Create a MoveMessagePanel instance for drawing a message MoveMessagePanel p = new MoveMessagePanel("Welcome to Java"); // Place the message panel in the frame getContentPane().setLayout(new BorderLayout()); getContentPane().add(p); } // This main method enables the applet to run as an application public static void main(String[] args) { // Create a frame JFrame frame = new JFrame("Move Message Using Mouse"); Example Moving message using Mouse

14 14 GUI 5Lecture 7 // Create an instance of the applet MoveMessageDemo applet = new MoveMessageDemo(); // Add the applet instance to the frame frame.getContentPane().add(applet, BorderLayout.CENTER); // Invoke init() and start() applet.init(); applet.start(); // Display the frame frame.setSize(300, 300); // frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE); frame.setVisible(true); } // MoveMessagePanel draws a message // This class is defined as inner class class MoveMessagePanel extends MessagePanel implements MouseMotionListener { // Construct a panel to draw string s public MoveMessagePanel(String s) { super(s); this.addMouseMotionListener(this); } // Tell the panel how to draw things public void paintComponent(Graphics g) { // Invoke the paintComponent method in the MessagePanel class super.paintComponent(g); } // Handler for mouse moved event public void mouseMoved(MouseEvent e) { } // Handler for mouse dragged event public void mouseDragged(MouseEvent e) { // Get the new location and repaint the screen setXCoordinate(e.getX()); setYCoordinate(e.getY()); repaint(); } MoveMessageDemo

15 15 GUI 5Lecture 7 l Handling Keyboard Events To process a keyboard event, use the following handlers in the KeyListener interface: More Handling Keyboard Events keyPressed(KeyEvent e) Called when a key is pressed. keyReleased(KeyEvent e) Called when a key is released. keyTyped(KeyEvent e) Called when a key is pressed and then released

16 16 GUI 5Lecture 7 The KeyEvent Class The keys captured in the event are integers rpresenting Unicode character values, Every keyboard event has an associated key character or key code that is returned by the getKeyChar() method get KeyCode() method Key Constants: Home VK_HOME End VK_End Page Up VK_PGUP Page Down VK_PGDN The enter keyVK_ENTER The right-arrow keyVK_RIGHT etc...

17 17 GUI 5Lecture 7 Example Keyboard Events Demo l Objective: Display a user-input character. The user can also move the character up, down, left, and right using the arrow keys. // KeyboardEventDemo.java: Receive key input import java.awt.*; import java.awt.event.*; import javax.swing.*; public class KeyboardEventDemo extends JApplet { private KeyboardPanel keyboardPanel = new KeyboardPanel(); // Main method used if run as an application public static void main(String[] args) { // Create a frame JFrame frame = new JFrame("KeyboardEvent Demo"); // Create an instance of the applet KeyboardEventDemo applet = new KeyboardEventDemo(); // Add the applet instance to the frame frame.getContentPane().add(applet, BorderLayout.CENTER); // Invoke init() and start() applet.init(); applet.start();

18 18 GUI 5Lecture 7 // Display the frame frame.setSize(300, 300); // frame.setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE); frame.setVisible(true); // Set focus on the keyboardPanel applet.focus(); } // Initialize UI public void init() { // Add the keyboard panel to accept and display user input getContentPane().add(keyboardPanel); // Request focus focus(); } // Set focus on the panel public void focus() { // It is required for receiving key input keyboardPanel.requestFocus(); } Example Keyboard Event (Cont)

19 19 GUI 5Lecture 7 // KeyboardPanel for receiving key input class KeyboardPanel extends JPanel implements KeyListener { private int x = 100; private int y = 100; private char keyChar = 'A'; // Default key public KeyboardPanel() { addKeyListener(this); // Register listener } public void keyReleased(KeyEvent e) { } Example Keyboard Event (Cont)

20 20 GUI 5Lecture 7 public void keyTyped(KeyEvent e) { } public void keyPressed(KeyEvent e) { switch (e.getKeyCode()) { case KeyEvent.VK_DOWN: y += 10; break; case KeyEvent.VK_UP: y -= 10; break; case KeyEvent.VK_LEFT: x -= 10; break; case KeyEvent.VK_RIGHT: x += 10; break; default: keyChar = e.getKeyChar(); } repaint(); } // Draw the character public void paintComponent(Graphics g) { super.paintComponent(g); g.setFont(new Font("TimesRoman", Font.PLAIN, 24)); g.drawString(String.valueOf(keyChar), x, y); } Example Keyboard Event (Cont


Download ppt "1 GUI 5Lecture 7 GUI - V Overview  Using No Layout Manager  Message Dialog Boxes  Running a Program as an Applet and an Application  More on Handling."

Similar presentations


Ads by Google