Transcript Lab10

Simple Phone Applet
Lab 10
1
Mobile Phone
Display Area
Send, Menu and End
Numbers 0-9
* and #
2
Layout of Mobile Phone
Display Area
Send, Menu and
End
Numbers 0-9
* and #
3
You can type in the display area
4
You can press numbers
5
Panel
Panel
to make Applet or Frame layout easier, we
break a frame up into regions and compose
each of them separately.
Each region is called a Panel.
Each can have its own different
LayoutManager. Panels don't have any visible
bounding lines.
We can delimit them with differing
background colours.
If we want something to draw on with
drawString and drawLine normally you would
use a Canvas.
6
Button
7
Code of red color
import java.applet.*;
import java.awt.*;
public class lab10 extends java.applet.Applet {
public void init() {
setLayout(new FlowLayout());
setBackground(Color.red);
add(new Label("<<"));
add(new Button("How are you DCO students"));
add(new Label(">>"));
}
}
8
Button of mobile phones
9
Source Code
import java.applet.*;
import java.awt.*;
public class lab101 extends java.applet.Applet {
public void init() {
String str;
setLayout(new FlowLayout());
setBackground(Color.red);
add(new Button("send"));
add(new Button("menu"));
add(new Button("stop"));
for (int i = 1; i <9; i++) {
str = "" +i; //Integer to String
add(new Button(str));
}
add(new Button("*"));
add(new Button("0"));
add(new Button("#"));
}
}
10
HTML with different width
<html>
<head>
<title>Maximum Using Array</title>
</head>
<body>
<applet code = "lab101.class" width =
100 height = 350>
</applet>
</body>
</html>
11
Use GridLayout
String str;
setLayout( new
GridLayout( 5, 3 )); // row 5:
col :3
setBackground(Color.red);
add(new Button("send"));
add(new Button("menu"));
add(new Button("stop"));
for (int i = 1; i <10; i++) {
str = "" +i; //Integer to String
add(new Button(str));
}
add(new Button("*"));
add(new Button("0"));
add(new Button("#"));
12
The source code
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Grid_JApplet1 extends JApplet
{
public void init()
{
// Swing adds JComponents to the container's
// contentPane rather than directly to the panel
// as with the AWT.
Container contentPane = getContentPane();
// Modify and add code between this line
//-------------------------------------------// Create an instance of a JPanel sub-class
GridPanel gridPanel = new GridPanel();
// And add one or more panels to the JApplet panel.
contentPane.add(gridPanel);
}
//-------------------------------------------// and this line.
}
// A sample JPanel class for holding components
class GridPanel extends JPanel
{
GridPanel()
{
// Modify and add code between this line
//-------------------------------------------String str;
setLayout( new GridLayout( 5, 3 )); // row 5: col :3
setBackground(Color.red);
add(new Button("send"));
add(new Button("menu"));
add(new Button("stop"));
for (int i = 1; i <10; i++) {
str = "" +i; //Integer to String
13
The HTML
14
Text Field
TextField
A TextField is a scrollable text display
object with one row of characters.
The preferred width of the field may
be specified during construction and
an initial string may be specified.
15
Simple Text Field
16
Source Code
17
Source
import java.awt.*;
import java.applet.Applet;
public class TextFieldSimpleTest extends Applet {
public void init() {
TextField f1 = new TextField("type something");
add(f1);
}}
18
Text Area
TextArea
A TextArea is a multi-row text field
that displays a single string of
characters, where newline ('\n' or
'\n\r' or '\r', depending on platform)
ends each row. The width and height
of the field is set at construction, but
the text can be scrolled up/down and
left/right.
19
Text Area
20
Source Code
import java.awt.*;
import java.applet.Applet;
public class TextAreaSimpleTest extends Applet {
TextArea disp;
public void init() {
disp = new TextArea("Code goes here", 10, 30);
add(disp);
}
}
21
Scroll
22
Source code
import java.awt.*;
import java.applet.Applet;
public class TextAreaScroll extends Applet {
String s =
"This is a very long message " +
"How are you DCO students " +
"are you learning Java?";
public void init() {
add(new TextArea (s, 4, 15,
TextArea.SCROLLBARS_NONE));
add(new TextArea (s, 4, 15,
TextArea.SCROLLBARS_BOTH));
}
}
23
Source code of Phone
24
Diagram
25