Thứ Sáu, 17 tháng 10, 2008

thực hành

-programming gui:
/*
* Created on Nov 8, 2008 by huu truc
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day18_2008nov8;

/**
* @author Administrator
*
* TODO programming gui
* page 115
*/
import java.awt.*;
import java.awt.event.*;
public class WindowClosingDemo {

public static void main(String[] args) {
//ten Frame la Window closing Demo
Frame f = new Frame("Window Closing Demo");
//WindowCloser lay tu tap tin WindowCloser.java
WindowCloser closer = new WindowCloser();
f.addWindowListener(closer);
//dinh kich thuoc Frame
f.setBounds(10, 10, 300, 200);
//hien thi Frame
f.setVisible(true);
}
}
/*
* Created on Nov 8, 2008 by huu truc
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day18_2008nov8;

/**
* @author Administrator
*
* TODO programming gui
* page 116
*/
import java.awt.event.*;
class WindowCloser implements WindowListener{
public void windowClosing(WindowEvent e){
System.out.println("WindowClosing ...");
//thoat ung dung
System.exit(0);
}
public void windowActivated(WindowEvent e){
System.out.println("WindowActivated ...");
}
public void windowClosed(WindowEvent e){
System.out.println("WindowClosed...");
}
public void windowDeactivated(WindowEvent e){
System.out.println("windowDeactivated...");
}
public void windowDeiconified(WindowEvent e){
System.out.println("windowDeiconified...");
}
public void windowIconified(WindowEvent e){
System.out.println("windowIconified...");
}
public void windowOpened(WindowEvent e){
System.out.println("windowOpened...");
}
}


- progaming gui :
/*
* Created on Nov 5, 2008 by huu truc
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day17_2008sep6;

/**
* @author Administrator
*
* TODO programing gui
* page 112
*/
import java.awt.*;
import java.awt.event.*;
//sau implement la mot interface.
//implement thuc hien hanh dong cua interface
public class KeyDemo extends Frame implements KeyListener{
//private chi truy cap ben trong ban than lop khai bao
private String line1 = "", line2 = "";
private String line3 = "";
private TextArea textArea;
//set up GUI
//ham constructor
public KeyDemo(){
//super la khi lop con muon nho lop cha lam giup mot viec do.
//tao tieu de cua so ung dung.
super("Demonstrating Keystroke Events");
//set up Area
textArea = new TextArea(10, 15);
//tao dong chu trong giao dien chuong trinh khoi dong.
textArea.setText("Press any key on the keyboard...");
//(false) thuc hien chuong trinh nhap mot phim
//(true) nhap nhu van ban, giong word.
textArea.setEnabled(false);
this.add(textArea);
//allow Frame to process Key event
addKeyListener(this);
//dinh kich thuoc cua frame
setSize(350,100);
//hien thi khung frame
setVisible(true);
}
//handle press any key
public void keyPressed(KeyEvent event){
line1 = "Key pressed:" + event.getKeyText(event.getKeyCode());
setLines2and3(event);
}
//handle release of any key
public void keyReleased(KeyEvent event){
//hien thi dong 1 trong ung dung
line1 = "Key release : " + event.getKeyText(event.getKeyCode());
setLines2and3(event);
}
//handle press of an action key
public void keyTyped(KeyEvent event){
//hien thi dong 1 trong ung dung
line1 = "Key typed:" + event.getKeyChar();
setLines2and3(event);
}
//set second and third lines of out put
private void setLines2and3(KeyEvent event){
//hien thi dong 2 trong cua so
line2= "This key is" + (event.isActionKey()? " " : " not ") +
"an action key";
String temp = event.getKeyModifiersText(event.getModifiers());
//hien thi dong 3 trong cua so
line3 = "Modifier keys pressed : " + (temp.equals(" ")?
"none" : temp);
textArea.setText(line1 + "\n" + line2 + "\n" + line3 + "\n");
//create command close window
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
//execute application
public static void main(String arg[]){
//tao doi tuong trong lop KeyDemo,
//de application thuc hien cac nhiem vu cua lop
KeyDemo application = new KeyDemo();
}
}//end class KeyDemo

- Practicse programing gui :
/*
* Created on Nov 4, 2008 by huu truc
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day16_2008sep5;

/**
* @author Administrator
*
* TODO programing gui
* page 109
*/
import java.awt.*;
import java.awt.event.*;

import Day5_2008oct22.stringArray;
public class MouseTracker extends Frame implements MouseListener, MouseMotionListener{
private Label statusBar;
//set up GUI and register mouse event handlers
public MouseTracker(){
super("Demonstrating Mouse Events");
statusBar = new Label();
this.add(statusBar, BorderLayout.SOUTH);
//Application listens to its own mouse events
addMouseListener(this);
addMouseMotionListener(this);
setSize(275, 100);
setVisible(true);
}
//MouseListener event handlers
//handle event when mouse release immediately after press
public void mouseClicked(MouseEvent event){
statusBar.setText("Clicked at [" + event.getX() +
"," + event.getY() + "]");
}
//handle event when mouse pressed
public void mousePressed(MouseEvent event){
statusBar.setText("Pressed at [" + event.getX() +
"," + event.getY() + "]");
}
// handle event when mouse pressed
public void mouseReleased(MouseEvent event){
statusBar.setText("Released at [" + event.getX() +
"," + event.getY() + "]");
}
//handle event when mouse enters area
public void mouseEntered(MouseEvent event){
statusBar.setText("Mouse in window");
}
//handle event when mouse exit area
public void mouseExited(MouseEvent event){
statusBar.setText("Mouse outside window");
}
//MouseMotionListener event handlers
//handle event when user drags mouse with button pressed
public void mouseDragged(MouseEvent event){
statusBar.setText("Dragged at [" + event.getX() +
"," + event.getY() + "]");
}
//handle event when user moves mouse
public void mouseMoved(MouseEvent event){
statusBar.setText("Moved at [" + event.getX() +
", " + event.getY() + "]");
//create command close window
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
//execute application
public static void main(String args[]){
MouseTracker application = new MouseTracker();
}
}//end class MouseTracker

- practise programing gui:
/*
* Created on Nov 4, 2008 by huu truc
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day16_2008sep5;

/**
* @author Administrator
*
* TODO programing gui
* page 102
*/
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class PanelDemo extends Frame{
private Button next, prev, fisrt;
private List li;
public PanelDemo(String sTitle){
super(sTitle);
//tao 3 nut bam
next = new Button("Next >>");
prev = new Button("Prev <<"); fisrt = new Button(" Fisrt "); Panel southPanel = new Panel(); southPanel.add(next); southPanel.add(prev); southPanel.add(fisrt); //BoderLayout.SOUTH: vung duoi this.add(southPanel, BorderLayout.SOUTH); Panel northPanel = new Panel(); //gan ten cho Label northPanel.add(new Label("Make a selection")); //BoderLayout.NORTH: vung tren this.add(northPanel, BorderLayout.NORTH); //tao danh sach doc Selection i li = new List(); for(int i=0; i< f =" new" i =" 2;">b){
System.out.println("Min is : "+ b);
}
else{
System.out.println("Min is : "+ a);
}
}
public static void min2(int a, int b, int c){
if(a<=b && a<=c){ System.out.println("gia tri nho nhat:"+a); } if(b<=a && b<=c){ System.out.println("gia tri nho nhat:"+b); } else{ System.out.println("gia tri nho nhat:"+c); } } } SẮP XẾP NỔI BỌT: /* * Created on Oct 20, 2008 by huu truc * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */ package Day3_2008oct20; /** * @author Administrator * * TODO sap xep mang dung phuong phap sap xep noi bot. * Window - Preferences - Java - Code Style - Code Templates */ public class bubbleSort { public static void main(String[] args) { int nums[]={99, -10, 100123, 18, -978, 5623, 463, -9, 278, 49}; int a,b,t; //khai bao 3 bien a, bien b, va bien t. int size; size = 10; //number of element to sort. // display original array System.out.print("Original array is:"); for(int i=0;i=a; b--){/**vong for sau b giam con lai 8,
va cu tiep tuc. nếu điều kiện b>a không thôi thì kết quả sắp xếp sai.*/
if(nums[b-1] > nums[b]){
//if out of order(neu ngoai thu tu).
//exchange element(thay doi yeu to).
t = nums[b-1];
nums[b-1] = nums[b];//gan chuyen, dua nums[8]thanh nums[9]
nums[b] = t;//chuyen nums[9]thanh t tuc la nums[8].
}
}
//display sorted array.
System.out.print("\n sorted array is:");
for(int i=0; i
System.out.print(" " + nums[i]);
System.out.print(" ");
}
}
}
}
IN BẢNG CỬU CHƯƠNG:
/*
* Created on Oct 21, 2008 by huu truc
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day4_2008oct21;

/**
* @author Administrator
*
* TODO in bang cuu chuong 2 den 9
* Window - Preferences - Java - Code Style - Code Templates
*/
public class inCuuchuong {

public static void main(String[] args) {
int i, t;
String m = "";
for(i=2; i<10; t="1;">=10) m = "" ;//canh le so ben trai.
else m = " ";//canh le so ben trai, day so nho hon 10 ra bang
//cac so lon hon.
System.out.print(t + " x " + i + " = " + i * t + m+ " | " );
}
System.out.println("");
}

}
}


IN DÃY SỐ TỪ 1 ĐẾN 100 THÀNH 10 HÀNG 10 CỘT:
/*
* Created on Oct 20, 2008
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day4_2008oct21;

/**
* @author Administrator
*
* TODO in day so tu nhien tu 1 den 100 thanh 10 dong.
* moi dong chua 10 so.
* Window - Preferences - Java - Code Style - Code Templates
*/
public class indayso {

public static void main(String[] args) {
int i, t;
String m = "";
for(i=1; i<=100; i++){ System.out.print(" " + i); if (i % 10 == 0){ System.out.println(" "); } if(i<10){system.out.print(""+m+" in =" new" str =" in.readLine();//doc" i=" str.length()-1;i">= 0; i--){
//vi java dem tu vi tri dau la: 0
System.out.print(str.charAt(i));
//in ra ky tu tai vi tri thu i trong chuoi str
//tinh tu vi tri cuoi
}
}catch(IOException e){// e doi tuong Exception
System.out.println(e.toString());//in ra loi neu sai Try
}
}
}

LẤY CHUỖI CON CỦA MỘT CHUỖI:
chú ý lý thuyết: java luôn đếm ký tự đầu tiên là 0.
/*
* Created on Oct 22, 2008 by huu truc
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day5_2008oct22;

/**
* @author Administrator
*
* TODO Lay chuoi con cua mot chuoi.
* Window - Preferences - Java - Code Style - Code Templates
*/
public class SubStr {

public static void main(String[] args) {
String orgstr = "mot hai ba bon";
//lay chuoi con dung ham.
//public String substring(int beginIndex, int endindex)
String substr= orgstr.substring(4,7);
//lay tu ky tu 4 den 7 trong chuoi.
//vi chuoi cung dem tu so 0 nhu mang tai vi tri dau tien.
//java luon dem tu 0.
System.out.println("Chuoi goc:"+ orgstr);
System.out.println("Chuoi con:"+substr);
System.out.print("ky tu dau tien la 0: "+orgstr.charAt(0));
}
}


THỰC HÀNH CÁC PHÉP GÁN:
/**
* @author Administrator
*
* TODO day 2 trong 21 ngay code.
* Window - Preferences - Java - Code Style - Code Templates
*/
public class VolcanoRobot {
String status;
int speed;
float temperature;//la kieu so thuc nen co so le la :510.0
//neu chon kieu int thi cho ra so nguyen la :510
void checkTemperature(){
if(temperature > 660){ //cau truc dieu khien
status = "returning home";//thuc hien khoi lenh.
speed = 5;//nen thuc hien luon dong nay.
}
}
void showAttribute(){//khong tra ve gi het, nhung thuc hien cac lenh trong {}.
System.out.println("Status: " + status);
System.out.println("Speed: " + speed);
System.out.println("temperature: " + temperature);
}
public static void main(String[]arguments){
VolcanoRobot dante = new VolcanoRobot();
dante.status = "exploring";//
dante.speed = 2;
dante.temperature = 510;
dante.showAttribute();//dung de thuc hien trong ham void
// khi duoc goi void showAttribute(){} o tren.
System.out.println("increasing speed to 3.");
dante.speed = 3;//gan lenh moi
dante.showAttribute();//thuc hien lenh voi phep gan gia tri moi.
System.out.println("changing temperature to 670.");
dante.temperature = 670;//phep gan cho lenh duoi,moi.
dante.showAttribute();//thuc hien lenh voi phep gan gia tri moi.
System.out.println("Checking the temperture" );
dante.checkTemperature();//goi ham checkTemperature o tren.
dante.showAttribute();
}
}
SO SÁNH BẰNG VÀ KHÔNG BẰNG TRONG CHUỔI VỀ ĐỐI TƯỢNG VÀ GIÁ TRỊ:
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class equalsTest {

public static void main(String[] arguments) {
String str1, str2;
str1 = "Free the bound periodicals.";
str2 = str1;//phep gan thi bang
System.out.println("string1: " + str1);
System.out.println("string2: " + str2);
System.out.println("Same object? " + (str1==str2));
System.out.println("Same value? " + (str1.equals(str2)));
str2 = new String(str1);
// them phan duoi nay de cho biet khoi tao doi tuong thi so sanh
//khong bang ve doi tuong nhung co the bang ve gia tri
System.out.println("string1 :" + str1);
System.out.println("String2 :" + str2);
System.out.println("Same object? " + (str1==str2));
System.out.println("Same value? " + str1.equals(str2));
}
}

KIỂM TRA CHUỔI, IN RA DẤU NHÁY KÉP (") CỦA CHUỔI: TẠI TỪ IBM.
/*
* Created on Oct 26, 2008 by huu truc
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day8_2008oct27;

/**
* @author Administrator
*
* TODO Day 4 trong 21 ngay code
* Window - Preferences - Java - Code Style - Code Templates
*/
public class checkString {

public static void main(String[] arguments) {
String str = "Nobody ever went broke by buying IBM";
System.out.println("The string is:" + str);
System.out.println("Length of this string: " + str.length());
System.out.println("The character at position 5:" + str.charAt(5));
System.out.println("The substring from 26 to 32:"
+str.substring(26,32));;
System.out.println("The index of the character v:"+ str.indexOf('v'));
System.out.println("The index of the beginning of the "
+ "substring \"IBM\": " + str.indexOf("IBM"));
//\" dung de in ra dau nhay kep.
System.out.println("The string in upper case: "
+ str.toUpperCase());
System.out.print("The string in lower case: "
+ str.toLowerCase());
}
}

GÁN TOẠ ĐỘ XY HÌNH HỌC PHẲNG:
/*
* Created on Oct 26, 2008 by huu truc
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day8_2008oct27;

/**
* @author Administrator
*
* TODO Day 4 trong 21 ngay code
* Window - Preferences - Java - Code Style - Code Templates
*/
import java.awt.Point;//thu vien do hoa, chua cac thanh phan lap
//giao dien nhu Form, checkbox, button,...
public class preferencesTest {

public static void main(String[] args) {
Point pt1,pt2;//point la diem co toa do x y tren man hinh
pt1 = new Point(100,100);//toa do x = 100, y = 100
pt2 = pt1;
pt1.x = 200;//toa do x cua pt1 la 200.
pt1.y = 200;//toa do y cua pt1 la 200.
System.out.println("Point1: " + pt1.x + ", " + pt1.y);
System.out.println("Point2: " + pt2.x + ", " + pt2.y);
}
}

THÊM PHẦN ĐIỂM TOẠ ĐỘ:
/*
* Created on Oct 26, 2008
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day8_2008oct27;

/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
import java.awt.Point;//thu vien do hoa, chua cac thanh phan lap
//giao dien nhu Form, checkbox, button,...
public class setPoints {

public static void main(String[] arguments) {
Point location = new Point(4,13);
System.out.println("Starting location:");
System.out.println("X equals " + location.x);
System.out.println("Y equals " + location.y);
System.out.println("Moving to (7,6)");
location.x = 7;//gan x = 7
location.y = 6;//gan y = 6
System.out.println("\nEnding location: ");
//them \n de xuong them 1 hang nua.
System.out.println("X equals " + location.x);
System.out.println("y equals " + location.y);
}
}

CỘNG VÀ TÍNH TRUNG BÌNH GIÁ TRỊ CÁC MẢNG:
/*
* Created on Oct 27, 2008 by huu truc
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day9_2008oct28;

/**
* @author Administrator
*
* TODO Day 5 trong 21 ngay code
* Window - Preferences - Java - Code Style - Code Templates
*/
public class halfDollars {

public static void main(String[] arguments) {
int [] denver = {15000006, 18810000, 20752110};
int [] philadelphia = new int[denver.length];
int [] total = new int[denver.length];
int average;
philadelphia[0] = 15020000;
philadelphia[1] = 18708000;
philadelphia[2] = 21348000;
total[0] = denver[0] + philadelphia[0];
total[1] = denver[1] + philadelphia[1];
total[2] = denver[2] + philadelphia[2];
average = (total[0] + total[1] + total[2])/3;
System.out.println("1993 production: " + total[0]);
System.out.println("1994 production: " + total[1]);
System.out.println("1995 production: " + total[2]);
System.out.println("Average production: " + average);
}
}


TÍNH GIÁ TRỊ CỦA CHUỖI:
/*
* Created on Oct 27, 2008 by huu truc
*
* TODO
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day9_2008oct28;

/**
* @author Administrator
*
* TODO Day 5 trong 21 ngay code
* Window - Preferences - Java - Code Style - Code Templates
*/
public class halfLoop {

public static void main(String[] arguments) {
int [] denver = {15000006, 18810000, 20752110};
int [] philadelphia = {15020000, 18708000, 21348000};
int [] total = new int[denver.length];
int sum = 0;
for(int i=0; i< length =" 3" yearin =" 2008;" monthin =" 2;"> 0){
monthIn = Integer.parseInt(arguments[0]);
//phan tu thu 0 cua mang tra ve so thang.parseInt tra ve
//kieu so nguyen
}
if(arguments.length > 1){
yearIn = Integer.parseInt(arguments[1]);
//phan tu thu 1 cua mang tra ve so nam
}
System.out.println(monthIn + "/" + yearIn + " has " +
countDays(monthIn, yearIn) + " days.");
//dem so ngay cua thang nam.
}
static int countDays(int month, int year){
int count =1;
switch (month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
count = 31;
break;
case 4:
case 6:
case 9:
case 11:
count = 30;
break;
case 2:
if(year%4 ==0){
count = 29;
}else{
count = 28;
}
if((year%100 ==0) & (year%400 !=0)){
count = 28;
}
}
return count;// return thuoc ham, khong co dau ngoac ( hay {
}
}


ÉP KIỂU DỮ LIỆU VÀ VÒNG LẶP WHILE:
/*
* Created on Oct 27, 2008 by huu truc
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day9_2008oct28;

/**
* @author Administrator
*
* TODO Day 5 trong 21 ngay code
* Window - Preferences - Java - Code Style - Code Templates
*/
public class CopyArrayWhile {

public static void main(String[] arguments) {
double[] array1 = {7.9, 4.1, 8, 1, 4, 1, 4};
int [] array2 = new int[array1.length];
System.out.print("array1 : [");
for(int i=0; i< count =" 0;" x1 =" 0;" y1 =" 0;" x2 =" 0;" y2 =" 0;" x1 =" x1;" y1 =" y1;" x2 =" x2;" y2 =" y2;" x1 =" topLeft.x;" y1 =" topLeft.y;" x2 =" bottomRight.x;" y2 =" bottomRight.y;" x1 =" topLeft.x;" y1 =" topLeft.y;" x2 =" (x1" y2 =" (y1">");
}
public static void main(String[]arguments){
myRect2 rect;//khai bao mot doi tuong rect co kieu la myRect2
System.out.println("Calling myRect2 with coordinates 25,25 50,50:");
rect = new myRect2(25, 25, 50, 50);
rect.printRect();//goi ham printRect() o cua lop myRect2
System.out.println("***");
System.out.print("Calling myRect2 with 1 point(10,10)");
System.out.println("width(50) and height(50):");
rect = new myRect2(new Point(10,10), 50, 50);
//new Point(10,10) khoi tao doi tuong point.
rect.printRect();
System.out.println("***");
}
}

- IN CLASS: lấy tên package.class để in ra:
/*
* Created on Oct 29, 2008 by huu truc
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day11_2008oct30;

/**
* @author Administrator
*
* TODO Day 6 trong 21 ngay code
* Window - Preferences - Java - Code Style - Code Templates
*/
class printClass {
int x = 0;
int y = 1;
void printMe(){
System.out.println("x is " + x + ", y is " + y);
System.out.println("I am an instance of the class " +
this.getClass().getName()) ;
//this la con tro cua class.
//getClass(): lay class. tra ve class nen khong in duoc.
//getName() : lay ten package. trong truong hop nay la ten class.
}
}
class printSubClass extends printClass{
int z = 3;
public static void main(String[]arguments){
printClass aClass = new printClass();
aClass.printMe();
printSubClass obj = new printSubClass();
obj.printMe();
}
}

-THỰC HIỆN HÀM CÓ GIÁ TRỊ TRẢ VỀ LÀ MỘT KIỂU MẢNG:
/*
* Created on Oct 29, 2008 by huu truc
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day11_2008oct30;

/**
* @author Administrator
*
* TODO Day 6 trong 21 ngay code
* Window - Preferences - Java - Code Style - Code Templates
*/
class rangeClass {
int[] makeRange(int lower, int upper){
//makeRange la ham co gia tri tra ve la mot kieu mang int[].
//nen co dau ngoac tron( va dau nhon mo{.
int arr[] = new int[(upper-lower) + 1];//khai bao mang int arr[];
for(int i=0; i< therange =" new" thearray =" theRange.makeRange(1," i="0;" arg =" new" sum =" 0;">0){//so luong tham so cua argument.
for(int i=0; i< butterscotch =" new" lasttime = "" screen2d =" (Graphics2D)screen;" type =" new" day =" new" time =" day.getTime().toString();" lasttime =" time;" pi =" 3.141592653589793" pi = " + Math.PI); //tao ra cac doi tuong hinh hoc. Point point = new Point(7,11);//khoi tao doi tuong point trong lop Circle circle = new Circle(3.5, 22, 8); Cylinder cylinder = new Cylinder(10, 3.3, 10, 10); //tao mot mang cac doi tuong hinh hoc. shape arrOfShapes[] = new shape[3]; //arrayOfshapes[0] la mot doi tuong Point. arrOfShapes[0] = point; //arrayOfshapes[1] la mot doi tuong Circle. arrOfShapes[1] = circle; //arrayOfshapes[2] la mot doi tuong cylinder. arrOfShapes[2] = cylinder; //lay ten va bieu dien cua moi doi tuong hinh hoc. String output = point.getName() + " precision =" new" i =" 0;" narea = " + precision.format(arrOfShapes[i].area())+ " nvolume =" + precision.format(arrOfShapes[i].volume()); } System.out.println(output); System.exit(0); } }//end class Test - hiển thị khung Frame: /* * Created on Nov 3, 2008 by huu truc * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */ package Day14_2008sep3; /** * @author Administrator * * TODO Lap trinh GUI page 86 * Window - Preferences - Java - Code Style - Code Templates */ import java.awt.*; class FrameDemo { public static void main(String[] args) { //tao doi tuong khung chua Frame Frame fr = new Frame(" fr =" new" buttok =" new" f =" new" style="color: rgb(0, 0, 0);">-Programming gui:
/*
* Created on Nov 4, 2008 by huu truc
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day15_2008sep4;

/**
* @author Administrator
*
* TODO programing gui
* page 96
*/
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import com.sun.java.swing.plaf.windows.resources.windows;
public class GridBagLayoutDemo {

public static void main(String[] args) {
//ten cua so ung dung
Frame f = new Frame("GridBagLayoutDemo");
//thiet lap Layout Manager
//tao doi tuong rang buoc cho cach trinh bay
//GridBagLayout.
GridBagLayout layout = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
f.setLayout(layout);
//Tao ra chin nut nhan voi cac ten Mot, ..., Chin.
String[] ButtName = {"Mot", "Hai", "Ba", "Bon", "Nam",
"Sau", "Bay", "Tam", "Chin"
};
//gan phan tu cho mang
Button[] buttons = new Button[9];
for(int i = 0; i < insets =" new" fill =" GridBagConstraints.BOTH;" gridx =" 1;" gridy =" 1;" gridheight =" 2;" gridwidth =" 1;" gridx =" 2;" gridy =" 1;" gridheight =" 1;" gridwidth =" 2;" gridx =" 2;" gridy =" 2;" gridheight =" 1;" gridwidth =" 1;" gridx =" 1;" gridy =" 3;" gridheight =" 1;" gridwidth =" 2;" gridx =" 3;" gridy =" 2;" gridheight =" 2;" gridwidth =" 1;" gridx =" 4;" gridy =" 1;" gridheight =" 3;" gridwidth =" 1;" fill =" GridBagConstraints.NONE;" gridx =" 1;" gridy =" 4;" gridheight =" 1;" gridwidth =" 1;" weightx =" 1.0;" gridx =" 2;" gridy =" 5;" gridheight =" 1;" gridwidth =" 1;" weightx =" 2.0;" gridx =" 3;" gridy =" 6;" gridheight =" 1;" gridwidth =" 1;" weightx =" 3.0;" i="0;">

Không có nhận xét nào: