View
215
Download
0
Tags:
Embed Size (px)
Citation preview
6
PanelPanel
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.
8
Code of red colorimport 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(">>"));
}}
10
Source Codeimport 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 Stringadd(new Button(str));
} add(new Button("*")); add(new Button("0")); add(new Button("#")); } }
11
HTML with different width<html>
<head><title>Maximum Using
Array</title></head>
<body>
<applet code = "lab101.class" width = 100 height = 350>
</applet>
</body>
</html>
12
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 Stringadd(new Button(str));
} add(new Button("*")); add(new Button("0")); add(new Button("#"));
13
The source codeimport 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 componentsclass GridPanel extends JPanel{ GridPanel() { // Modify and add code between this line //--------------------------------------------
String str;setLayout( new GridLayout( 5, 3 )); // row 5: col :3setBackground(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 Stringadd(new Button(str));
} add(new Button("*")); add(new Button("0")); add(new Button("#")); //-------------------------------------------- // and this line. }}
15
Text Field
TextFieldA 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.
18
Sourceimport java.awt.*; import java.applet.Applet; public class TextFieldSimpleTest extends Applet { public void init() {
TextField f1 = new TextField("type something");
add(f1);
} }
19
Text AreaTextAreaA 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.
21
Source Codeimport 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); } }
23
Source codeimport 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)); } }