Download 15-GUI Programming V

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
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
GUI 5Lecture 7
1
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
GUI 5Lecture 7
2
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)
GUI 5Lecture 7
3
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);
}
}
GUI 5Lecture 7
4
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.
GUI 5Lecture 7
5
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.
GUI 5Lecture 7
6
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)
GUI 5Lecture 7
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);
}
GUI 5Lecture 7
8
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);
GUI 5Lecture 7
}
9
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
GUI 5Lecture 7
10
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);}
GUI 5Lecture 7
11
More on Handling Mouse Events
The following example can run as an applet and
as an application.

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.
GUI 5Lecture 7
12
Example Moving message using Mouse
// 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
// 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");
instance for drawing a message
MoveMessagePanel p = new
MoveMessagePanel("Welcome to
Java");
GUI 5Lecture 7
13
MoveMessageDemo
// Create an instance of the applet
MoveMessageDemo applet = new MoveMessageDemo();
// Tell the panel how to draw things
public void paintComponent(Graphics g)
frame.getContentPane().add(applet, BorderLayout.CENTER);
{
// Invoke init() and start()
// Invoke the paintComponent method in the
applet.init();
MessagePanel class
applet.start();
super.paintComponent(g);
// Display the frame
}
frame.setSize(300, 300);
// Handler for mouse moved event
//
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLO
public void mouseMoved(MouseEvent e)
SE);
{
frame.setVisible(true);
}
}
// Handler for mouse dragged event
// MoveMessagePanel draws a message
// This class is defined as inner class
public void mouseDragged(MouseEvent e)
class MoveMessagePanel extends MessagePanel
{
implements MouseMotionListener
// Get the new location and repaint the screen
{
setXCoordinate(e.getX());
// Construct a panel to draw string s
setYCoordinate(e.getY());
public MoveMessagePanel(String s)
repaint();
{
super(s);
}
this.addMouseMotionListener(this);
}
14
// Add the applet instance to the frame
}
GUI 5Lecture 7
More Handling Keyboard Events

Handling Keyboard Events
To process a keyboard event, use the following handlers in the KeyListener
interface:
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
GUI 5Lecture 7
15
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 getKeyCode()
method
Key Constants:
Home
End
Page Up
Page Down
The enter key
The right-arrow key
VK_HOME
VK_End
VK_PGUP
VK_PGDN
VK_ENTER
VK_RIGHT
etc...
GUI 5Lecture 7
16
Example Keyboard Events Demo

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
public static void main(String[] args)
{
import java.awt.*;
import java.awt.event.*;
// Create a frame
import javax.swing.*;
JFrame frame = new
public class KeyboardEventDemo
JFrame("KeyboardEvent Demo");
extends JApplet
// Create an instance of the applet
{
KeyboardEventDemo applet = new
private KeyboardPanel keyboardPanel = KeyboardEventDemo();
new KeyboardPanel();
// Add the applet instance to the frame
// Main method used if run as an application
frame.getContentPane().add(applet,
BorderLayout.CENTER);
// Invoke init() and start()
applet.init();
applet.start();
GUI 5Lecture 7
17
Example Keyboard Event (Cont)
// Request focus
focus();
}
// Set focus on the panel
public void focus()
{
// Display the frame
frame.setSize(300, 300);
//
frame.setDefaultCloseOperation(JFrame.
EXIT_ON_CLOSE);
frame.setVisible(true);
// It is required for receiving key input
// Set focus on the keyboardPanel
keyboardPanel.requestFocus();
applet.focus();
}
}
// Initialize UI
public void init()
{
}
// Add the keyboard panel to accept and display user input
getContentPane().add(keyboardPanel);
GUI 5Lecture 7
18
Example Keyboard Event (Cont)
// 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)
{
}
GUI 5Lecture 7
19
Example Keyboard Event (Cont
public void keyTyped(KeyEvent e)
repaint();
{
}
}
public void keyPressed(KeyEvent e)
// Draw the character
{
public void paintComponent(Graphics g)
switch (e.getKeyCode())
{
{
super.paintComponent(g);
case KeyEvent.VK_DOWN: y +=
10; break;
g.setFont(new Font("TimesRoman",
case KeyEvent.VK_UP: y -= 10;
Font.PLAIN, 24));
break;
case KeyEvent.VK_LEFT: x -= 10;
g.drawString(String.valueOf(keyChar),
break;
x, y);
case KeyEvent.VK_RIGHT: x += 10; }
break;
}
default: keyChar = e.getKeyChar();
20
}
GUI 5Lecture 7