forme855.files.wordpress.com€¦ · web viewchapter 7. packages and interfaces. packages....

42
Chapter 7 Packages and Interfaces 1. Packages ជជជជជជជជជជជជជជជជជជជជជជ class ជជជ interfaceជ 1.1. How to create packages Form: package packageName; ជជជជជជជជ package p1; class Simple{ int a; int b; } 1.2. Import package Form: import packageName.className; ជជជជជជជជ package p1; import Review.Sub1; public class Main{ public static void main(String[] args){ Sub1 obj1=new Sub1(); Review.Sub2 obj2=new Review.Sub2(); Review.Sub2 obj3=new Review.Sub2(); Re-compiled by: Mr. HIEENG Sokh Hymns 1

Upload: dinhtuyen

Post on 10-Apr-2018

228 views

Category:

Documents


10 download

TRANSCRIPT

Page 1: forme855.files.wordpress.com€¦ · Web viewChapter 7. Packages and Interfaces. Packages. ជាកញ្ចប់សំរាប់វេចខ្ចប់ class និង interface។

Chapter 7

Packages and Interfaces

1. Packagesជាកញ្ចប់សំរាប់វេ�ចខ្ចប់ class និង interface។

1.1. How to create packages

Form:

package packageName;

ឧទាហរណ៍៖package p1;

class Simple{

int a;

int b;

}

1.2. Import package

Form:

import packageName.className;

ឧទាហរណ៍៖package p1;

import Review.Sub1;

public class Main{

public static void main(String[] args){

Sub1 obj1=new Sub1();

Review.Sub2 obj2=new Review.Sub2();

Review.Sub2 obj3=new Review.Sub2();

Simple obj4=new Simple();

Review.Simple obj5=new Review.Simple();

}

}

Re-compiled by: Mr. HIEENG Sokh Hymns 1

Page 2: forme855.files.wordpress.com€¦ · Web viewChapter 7. Packages and Interfaces. Packages. ជាកញ្ចប់សំរាប់វេចខ្ចប់ class និង interface។

ចំណា៖ំវេ��ងអាច�ក class ព� package វេ��ងបានវេ!�ព�រ�ធិ�៖1. វេ$ប� keyword import ដែ&លមានលក្ខណៈ$សវេ&+ង Header file វេ,ក្នុងភាសា C ឬ C++

វេ&�ម្ប�ទាញ�ក class វេ5ះ និងបញ្ជា8 ក់ព� package ។2. វេ��ងមិនចាបំាច់វេ$ប� keyword import ក៏បានដែ&រ ដែ;វេ��ងចាបំាច់$;�ូបញ្ជា8 ក់ package វេ5ះ$=ប់វេពល

ដែ&លវេ��ងវេ$ប� class វេ5ះ។

JComboBox ជា class ស្ថិ;ក្នុង package javax.swing.JComboBox ដែ&ល class JComboBox =ឺជា Components ដែ&លអាច�្ទុកទិន្នន័�បានវេ$ច�ន Items មានលក្ខណៈជា Drop-down Listឧទាហរណ៍៖

JComboBox combo=new JComboBox();

វេ,ក្នុង class JComboBox មាន Method ជាវេ$ច�ន&ូចជា៖ void setEditable(boolean); ជា method មាន;ួ5ទ�អាចឲ្យ User ដែកដែ$បទិន្នន័�វេ,ក្នុង

ComboBox បាន។ឧទាហរណ៍៖

combo.setEditable(true);

void setMaximumRowCount(int); ជា method មាន;ួ5ទ�កំណ;់ចំនួន Item វេ$ច�នបំ�ុ; ដែ&ល$;�ូបានបង្ហាI ញវេ,ក្នុង Drop-down List ។ឧទាហរណ៍៖

combo.setMaximumRowCount(5);

int getItemCount(); ជា method មួ�មាន;ួ5ទ�រាប់ចំនួន Item មានក្នុង ComboBox ។ឧទាហរណ៍៖

int numItems;

numItems = combo.getItemCount();

// get numbers of Items in JComboBox

void setSelectedIndex(int); ជា method មួ�មាន;ួ5ទ�បវេងJ�;ការ Select Item ណាមួ� តាម Index ដែ&លវេ��ងបានកំណ;់។ឧទាហរណ៍៖

combo.setSelectedIndex(3);

Object getSelectedItem();ជា method មួ�មាន;ួ5ទ�ទាញ�ក Item ដែ&ល User បាន Select ។ឧទាហរណ៍៖

String item;

item = (String) combo.getSelectedItem();

Object getItemAt(int); ជា method មួ�មាន;ួ5ទ�ទាញ�ក Item ដែ&លស្ថិ;វេ,ក្នុង Index ណាមួ�។ឧទាហរណ៍៖

String item;

item = (String) combo.getItemAt(3);Re-compiled by: Mr. HIEENG Sokh Hymns 2

Page 3: forme855.files.wordpress.com€¦ · Web viewChapter 7. Packages and Interfaces. Packages. ជាកញ្ចប់សំរាប់វេចខ្ចប់ class និង interface។

void addItem(object); ជា method មួ�មាន;ួ5ទ� add Item ចូល ComboBox ។ឧទាហរណ៍៖

String item;

item = “Teacher”;

combo.addItem(item);

combo.addItem(“Student”);

private void cmdAddActionPerformed(Java,…,…..){

string item;

item = txtAdd.getText();

combo.addItem(item);

txtAdd.setText(“ ”);

txtAdd.grabFocus();

int last;

last = combo.getItemCount() -1;

combo.setSelectedIndex(last);

}

void insertItemAt(object, int); ជា method មួ�មាន;ួ5ទ�$បវេ$M+; Item ថ្ម�ចូលក្នុង ComboBox។ឧទាហរណ៍៖

String item;

item = “New Item”;

combo.insertItemAtt(item, 3);

void removeItemAt(int);

void removeItem(object);

void removeAllItems();

ឧទាហរណ៍៖combo.removeItemAt(3);

combo.removeItem(“Student”);

combo.removeAllItems();

Re-compiled by: Mr. HIEENG Sokh Hymns 3

txtAdd cmdAdd

combo

Page 4: forme855.files.wordpress.com€¦ · Web viewChapter 7. Packages and Interfaces. Packages. ជាកញ្ចប់សំរាប់វេចខ្ចប់ class និង interface។

Homework:

ចូរសរវេសរ Program វេ&�ម្ប� Add ទិន្នន័�ចូលក្នុង ComboBox ។ ទិន្នន័�ដែ&ល$;�ូបាន Add $;�ូវេរ+បតាមលំ!ប់វេ!�ស្វ័�$ប�;្តិ (A → Z) ។ មិនអនុញ្ជាS ;ឲ្យទិន្នន័�$ចំដែ&ល ឬទវេទ វេ,ក្នុង ComboBox វេទ។

Correction:

1. វេរ+បតាមលំ!ប់វេ!�ស្វ័�$ប�;្តិ (A → Z)private void cmdAddActionPerformed(java.awt.event...)

String item;

Item = txtAdd.getText().trim();

String itemCom;

itemCom = (String) combo.getItemAt(0);

if(item.equals(itemCom)){

// item == itemCom

}else{

// item != itemCom

}

if(item.compareTo(itemCom)<0){

// item<itemCom

}else if(item.CompareTo(itemCom)>0){

// item>itemCom

}else{

// item == itemCom

}

2. មិនអនុញ្ជាS ;ឲ្យទិន្នន័�$ចំដែ&ល ឬទវេទ វេ,ក្នុង ComboBox

3. សរវេសរកំុឲ្យស្ទួនគ្នា្ន វេ!�ខ្លួនឯង (Check in C++ $;ង់ចំណុច code already exist)

25 – June – 2011

Re-compiled by: Mr. HIEENG Sokh Hymns 4

TextField

ComboBox

CammandAdd

Page 5: forme855.files.wordpress.com€¦ · Web viewChapter 7. Packages and Interfaces. Packages. ជាកញ្ចប់សំរាប់វេចខ្ចប់ class និង interface។

1.3. Inheritance with Different Package

Package p1

public class Simple{

int a;

int b;

}

package inheritance;

import p1.Simple;

public class SubSimple extends Simple{

int x;

int y;

}

1.4. Nested Package

package p1.Sub1; // Nested package.

1.5. Access Control

Private NoModifier Protected Public

Same class yes yes yes yes (1st)

Same package SubClass no yes yes yes (2nd)

Same package noSubClass no yes yes yes (3rd)

Different package SubClass no no yes yes (4th)

Different package noSubClass no no no yes (5th)

ឧទាហរណ៍អំព� Same class(1st)៖package accessControl;

public class Access{

private int a;

int b;

protected int c;

public int d;

public Access(){

a = 1;

b = 1;

c = 1;

d = 1;

}

Re-compiled by: Mr. HIEENG Sokh Hymns 5

Page 6: forme855.files.wordpress.com€¦ · Web viewChapter 7. Packages and Interfaces. Packages. ជាកញ្ចប់សំរាប់វេចខ្ចប់ class និង interface។

}

ឧទាហរណ៍អំព� Same package SubClass (2nd)៖ public class SubAccess extends Access{

public SubAccess(){

// a = 1; can’t use private

b = 1;

c = 1;

d = 1;

}

}

ឧទាហរណ៍អំព� Same package noSubClass (3rd)៖ public class NoSubAccess{

publicNoSubAccess(){

Access obj = new Access();

// obj.a = 1; can’t use private

obj.b = 1;

obj.c = 1;

obj.d = 1;

}

}

ឧទាហរណ៍អំព� Different package SubClass (4th)៖ import accessControl.Access;

public class DiffSubAccess extends Access{

public DiffSubAccess(){

// a = 1; can’t use private

// b = 1; can’t use NoModifier

c = 1;

d = 1;

}

}

ឧទាហរណ៍អំព� Different package noSubClass (5th)៖ import accessControl.Access;

public class DiffNoSubAccess{

public DiffNoSubAccess(){

Re-compiled by: Mr. HIEENG Sokh Hymns 6

Page 7: forme855.files.wordpress.com€¦ · Web viewChapter 7. Packages and Interfaces. Packages. ជាកញ្ចប់សំរាប់វេចខ្ចប់ class និង interface។

Access obj = new Access();

// obj.a = 1; can’t use private

// obj.b = 1; can’t use NoModidier

// obj.c = 1; can’t use protected

obj.d = 1;

}

}

2. Interface=ឺជា class ដែ&លមាន instance variable សុទ្ធដែ; Final value និង method សុទ្ធដែ;ជា abstract

(ជា method ដែ&លមិនវេពញវេលញ) ។2.1. How to create interface

Form:

AccessControl interface InterfaceName{

type-Final instanceVariable = final-Value;

return-type methodName(arg);

}

ឧទាហរណ៍៖public interface Inter1{

public int VALUE_A = 20;

public void showA();

}

2.2. Implementing interfaceវេ&�ម្ប�ទាញ�ក member របស់ interface មកវេ$ប�វេ,ក្នុង class វេ��ងចាបំាច់$;�ូវេ$ប� keyword

“implements” ។

package Interface;

public interface Inter1{

public int VALUES_A = 10;

public void showA();

}

package Interface;Re-compiled by: Mr. HIEENG Sokh Hymns 7

Page 8: forme855.files.wordpress.com€¦ · Web viewChapter 7. Packages and Interfaces. Packages. ជាកញ្ចប់សំរាប់វេចខ្ចប់ class និង interface។

public class SubInter implements Inter1{

public void showA(){

System.out.println(VALUES_A);

}

}

package Interface;

public class SubInter implements Inter1{

public void showA(){

System.out.println(VALUES_A);

}

}

package Interface;

public class Main{

public static void main(String[] args){

SubInter obj1 = new SubInter();

System.out.println(obj1.VALUES_A);

obj1.showA();

Inter1 obj2 = new Inter1(){

Public void showA(){

System.out.println(VALUES_A);

}

}

obj2.showA();

}

}

JRadioButton ជា class ដែ&លស្ថិ;វេ,ក្នុង Package Javax.swing.JRadioButton ។ class JRadioButton ជា Components ដែ&លអនុញ្ជាS ;ិឲ្យ Users វេ$M�សវេរ Xស Option ណាមួ� ដែ&លពួកវេ=ចង់បាន។

ឧទាហរណ៍៖Re-compiled by: Mr. HIEENG Sokh Hymns 8

Page 9: forme855.files.wordpress.com€¦ · Web viewChapter 7. Packages and Interfaces. Packages. ជាកញ្ចប់សំរាប់វេចខ្ចប់ class និង interface។

JRadioButton radio = new JRadioButton();ក្នុង class JRadioButton មាន method 2 ដែ&លសំខាន់៖

void setSelected(Boolean); ជា method ដែ&លមាន;ួ5ទ�បវេងJ�; Select ឲ្យ RadioButton ។ឧទាហរណ៍៖

Radio.setSelected(true);

boolean isSelected();ជា method ដែ&លមាន;ួ5ទ�វេ�្ទZងផ្ទា្ទ ;់វេម�លថាRadio Button$;�ូបាន Select ឬអ;់។ឧទាហរណ៍៖

if(radio.isSelected()){

// radio has selected

}else{

// radio has not selected

}

Homework:

Re-compiled by: Mr. HIEENG Sokh Hymns 9

GenderFirst Name:

ស Last

Male Female

Country ComboBox Add

First Name Last Name Gender Country

Page 10: forme855.files.wordpress.com€¦ · Web viewChapter 7. Packages and Interfaces. Packages. ជាកញ្ចប់សំរាប់វេចខ្ចប់ class និង interface។

ចូរសរវេសរ Program វេ&�ម្ប� input ទិន្នន័�&ូចរបូខាងវេល� វេហ��ទិន្នន័�ដែ&ល User បញ្ចូល$;�ូធ្លា្ល ក់ចុលក្នុង Text area វេ,វេពល User click button Add ។

2.3. Multi Inheritance with Interfacepackage Interface;

public class SubInter implements Inter1, Inter2{

public void showA(){

System.out.println(VALUES_A);

}

public void showB(){

System.out.println(VALUES_B);

}

}

package Interface;

public interface MainInter extends Inter1, Inter2{

public inter VALUES_C = 89;

public void showC();

}

ចំណា៖ំ វេ��ងអាច Inheritance បាន ៣យ̀៉ាងវេ!�វេ$ប� 2 Keywords =ឺ៖ Keywords “extends” និង

“implements” ។ វេប� class ទទួលមរ;កព� class វេ��ងចាបំាច់វេ$ប� Keyword “extends” ។ វេប� interface ទទួលមរ;កព� interface វេ��ងចាបំាច់វេ$ប� Keyword “extends” ។ វេប� class ទទួលមរ;កព� interface វេ��ងចាបំាប់វេ$ច� Keyword “implements” ។

សវេង្ខប៖ class extends class

interface extends interface

class implements interface

interface ??? class

Interface មិនអាចទទួល Member ព� Class បានវេទ វេ!�សារដែ;វេ,ក្នុង class មាន Normal instanceVariable និង Normal Method ។

2.4. Interface versus Abstract class

Re-compiled by: Mr. HIEENG Sokh Hymns 10

Page 11: forme855.files.wordpress.com€¦ · Web viewChapter 7. Packages and Interfaces. Packages. ជាកញ្ចប់សំរាប់វេចខ្ចប់ class និង interface។

package Inheritance;

public interface Interface{

public int VALUES=10;

// public int x; can’t create Normal instanceVariable

// public void show();

// public void showValues(){

can’t create Normal Method

}

}

Package Interface;

Public abstract class Abstract{

Final public int VALUES = 90;

Public int value;

Abstract public void show();

Public void showValue(){

Systeml.out.println(value);

}

}

The End !

The three OOP Principles (Object Oriented Programming) ៖ ពាក្យថា OOP មានន័�ថា Object Oriented Programming ។ វេ&�ម្ប�សំគ្នាល់ថា Program មួ�មានលក្ខណៈជា OOP លុះ $តាដែ; Program វេ5ះ$;�ូបំវេពញលក្ខណៈពិវេសស ៣យ̀៉ាង=ឺ៖ Encapsulation, Inheritance និង Polymorphism ។

Encapsulation ៖ ជាការវេ�ចខ្ចប់ទិន្នន័�វេ!�មានសុ�;្ថិភាព មានន័�ថា Instance variable របស់ Class វេ��ងមិនឲ្យវេ= Access វេ!�ផ្ទា្ទ ល់បានវេទ វេ��ងឲ្យវេ= Access តាម Method ។

Inheritance ៖ ជាការវេ�្ទរ Member ព� Class មួ�វេbកាន់ Class មួ�វេ��ងវេទ+;ដែ&លវេធ្វ� ឲ្យមានលក្ខណៈង្ហា�$សលួ មិនខា;វេពលសរវេសរ Code $ចំដែ&លវេd�ង�ញិ។

Polymorphism ៖ ជាការបវេងJ�; Class =ំរ ូ វេហ��វេ��ងអាច�ក Class វេ5ះវេbដែកច្នៃច្ន បាន Class វេ$ច�នវេទ+;ដែ&លមានលក្ខណៈ$សវេ&+ងគ្នា្ន ។

Re-compiled by: Mr. HIEENG Sokh Hymns 11

Page 12: forme855.files.wordpress.com€¦ · Web viewChapter 7. Packages and Interfaces. Packages. ជាកញ្ចប់សំរាប់វេចខ្ចប់ class និង interface។

Chapter 8

Exception Handling

1. The Exception Hierarchyក្នុងភាសា Java វេ��ងមាន code សំរាប់សរវេសរ error ដែ;វេប�វេ,ដែ;មាន error វេ��ងអាចបំ បា;់ error ទាងំ

អស់វេ5ះបាន។Class Throwable មាន;ួ5ទ�$=ប់$=ង$=ប់ error ទាងំអស់ដែ&លវេក�;មានវេd�ង។ error ទាងំ វេ5ះ

ដែចកវេចញជាព�រ៖ Exception

ArithmeticException

NumberFormatException

ArrayIndexOutOfBoundsException

ArrayStroeException

SQLException

ClassNotFoundException

Other class

Error (JVM = Java Virtual Machine)

Re-compiled by: Mr. HIEENG Sokh Hymns 12

Page 13: forme855.files.wordpress.com€¦ · Web viewChapter 7. Packages and Interfaces. Packages. ជាកញ្ចប់សំរាប់វេចខ្ចប់ class និង interface។

ចំណា៖ំ Throwable : ជា SuperClass របស់ class Exception និង Error ។ Exception ជា class មួ�មាន;ួ5ទ�ការពារ error កំរ;ិ$សាលដែ&លភា=វេ$ច�នបណា្ត ល មកព� code ឬ

user input ។ Error វេនះ$បវេfទវេនះវេ��ងអាចការពារបានទាងំ$សងុវេ!� $គ្នាន់ដែ;វេ$ប� try, catch វេ&�ម្ប� throw error វេចាល។

Error ជា class ដែ&ល$=ប់$=ងវេល� error $បវេfទធ្ងន់ដែ&លបណា្ត លមកព� Hardware ឬ Software machine ។ Error $បវេfទវេនះវេ��ងមិនអាចការពារបានវេទ។ឧទាហរណ៍៖

Out of memory Not found hard disk

2. Using try, catch keywordForm:

try{

// block of code to monitor for errors

}catch(Exception-type 1 obj){

// block exception type 1

// this block executed when has error

// of Exception type 1

}catch(Exception-type 2 obj){

// block Exception type 2

// this block executed when has errors

// of Exception type 2

}finally{

// this block always executed

}

ឧទាហរណ៍៖private void ...

try{

int a[]={10,20,30,40,50};

int n;

n=Integer.valueOf(txtNumber.getText().trim());

int index;

index=Integer.valueOf(txtIndex.getText().trim());

int dive;

div=a[index]/n;

Re-compiled by: Mr. HIEENG Sokh Hymns 13

Page 14: forme855.files.wordpress.com€¦ · Web viewChapter 7. Packages and Interfaces. Packages. ជាកញ្ចប់សំរាប់វេចខ្ចប់ class និង interface។

txt.setText(“Your result is ”+div.);

}catch(NumberFormatException e){

JOptionPan.showMessageDialog(this, “Please, ....”);

}catch(ArrayIndexOutOfBoundsException e){

JOptionPane.showMessageDialog(this, “Please, ....”);

}catch(ArithmeticException e){

JOptionPane.showMessageDialog(this, “Please, divide by zero.”);

}catch(Exception e){

JOptionPane.showMessageDialog(this,e.getMassage());

}

The End !

09 – July – 2011

Chapter 9

Input and Output

1. java.io.* ជា package ដែ&ល$បមូល�្តុំវេbវេ!� class ដែ&លអាច input និង output ។2. File ជាclass ដែ&លស្ថិ;វេ,ក្នុង package java.io.File មាន;ួ5ទ�$=ប់$=ងព;៌មានទាងំអស់ របស់ file ឬ

directory ។

Form:

File objF = new File(String pathName);

pathName; ជា address និងវេj្ម ះរបស់ file ឬ directory ។ objF; ជា object ដែ&លវេក�;វេចញព� class File មាន;ួ5ទ��កព;៌មានរបស់ file ឬ directory

ឧទាហរណ៍៖File objF = new File(“C:\\myPro\\My File\\text.txt”);OrFile objF = new File(“C:/myPro/My File/text.txt”);

File objD = new File(“C:\\myPro\\My File”);OrFile objD = new File(“C:/myPro/My File”);

Re-compiled by: Mr. HIEENG Sokh Hymns 14

Page 15: forme855.files.wordpress.com€¦ · Web viewChapter 7. Packages and Interfaces. Packages. ជាកញ្ចប់សំរាប់វេចខ្ចប់ class និង interface។

វេ&�ម្ប�ទាញ�កព;៌មានរបស់ file ឬ directory វេ��ងចាបំាច់$;�ូវេ$ប� method &ូចខាងវេ$កាម៖ String getName(); ជា method មាន;ួ5ទ�ទទួល�កវេj្ម ះរបស់ file ឬ directory ។

ឧទាហរណ៍៖String nameF, nameD;

nameF=objF.getName(); // nameF= “text.txt”

nameD=objD.getName(); // nameD= “My File”

String getParent();ជា method មាន;ួ5ទ�ទទួល�ក$;ឹមដែ; address របស់ file ឬ directory ។ឧទាហរណ៍៖

String parentF, parentD;

parentF=objF.getParent (); // parentF= “C:/myPro/My File”

parentD=objD.getParent (); // parentD= “C:/myPro”

09 – July – 2011

String getPath();ជា method មាន;ួ5ទ�ទទួល�កទាងំ Address និងវេj្ម ះ របស់ File ឬ Directory ឧទាហរណ៍៖

String pathF, pathD;

pathF=objF.getPath (); // pathF= “C:/myPro/My File/text.txt”

pathD=objD.getPath (); // pathD = “C:/myPro/My File”

String getAbsolutePath();ជា method មាន;ួ5ទ�ទទួល�ក Path បានជាក់ច្បាស់ជាង Method getPath ។ឧទាហរណ៍៖

File objF=new File(“My File/text.txt”);

String path,abPath;

Path=objF.getPath(); // path=“My File/text.txt”

abPath()=objF.getAbsolutePath(); // abPath=“C:/myPro/My File/text.txt”

String getCanocicalPath(); ជា method មាន;ួ5ទ�ទទួល�ក Path បានជាក់ច្បាស់ជាង getAbsolutePath ។ឧទាហរណ៍៖

File objF=new File(“../text.txt”);

String abPath, canPath;

abPath=objF.getAbsolutePath(); // “c:/MyPro/../text.txt”

canPath()=objF.getCanonicalPath(); // “C:/text.txt”

16 – July – 2011 boolean mkdir(); ជា method មាន;ួ5ទ�បវេងJ�; Directory វេប� return true Directory វេ5ះ$;�ូ

បានបវេងJ�;វេហ��។ឧទាហរណ៍៖

File objD=new File(“C:/myPro/New Directory”);Re-compiled by: Mr. HIEENG Sokh Hymns 15

Page 16: forme855.files.wordpress.com€¦ · Web viewChapter 7. Packages and Interfaces. Packages. ជាកញ្ចប់សំរាប់វេចខ្ចប់ class និង interface។

objD.mkdir();

boolean mkdirs(); ជា method មាន;ួ5ទ�បវេងJ�;ទាងំ Root Directory និង Sub-directoryឧទាហរណ៍៖

File objD=new File(“C:/myPro/Sub/Sub1/Sub2”);

objD.mkdirs();

boolean createNewFile();ជា method មាន;ួ5ទ�បវេងJ�; File ថ្ម� វេប� Return true File វេ5ះ $;�ូបានបវេងJ�;។ឧទាហរណ៍៖

File objF=new File(“C:/myPro/My File/text.txt”);

objF.createNewFile();

boolean exists();ជា method មាន;ួ5ទ�វេ�្ទZងផ្ទា្ទ ;់វេម�ល ថាវេ;� Directory ឬ File មានវេ,ក្នុង $បព័ន្ធឬអ;់? វេប�មានវា Return true ។ឧទាហរណ៍៖

File objF=new File(“C:/myPro/My File/text.txt”);

if(objF.exists())

System.out.println(“This file is existed.”);

else

System.out.println(“This is not existed.”);

boolean delete(); ជា method មាន;ួ5ទ�delete file វេចញព� System។ វេប� return true File វេ5ះ$;�ូបាន delete ។ឧទាហរណ៍៖

File objF=new File(“C:/myPro/My File/text.txt”);

if(objF.delete())

System.out.println(“Deleted.”);

else

System.out.println(“Can’t deleted.”);

boolean isFile();ជា method មាន;ួ5ទ�វេ�្ទZងផ្ទា្ទ ;់វេម�ល ថាវេ;� object វេ5ះជា obj របស់ File ឬក៏មិនដែមន? វេប�ជា object file វា Return true ។ឧទាហរណ៍៖

File objF=new File(“C:/myPro/My File/text.txt”);

if(objF.isFile())

System.out.println(“This is file.”);

else{

Re-compiled by: Mr. HIEENG Sokh Hymns 16

Page 17: forme855.files.wordpress.com€¦ · Web viewChapter 7. Packages and Interfaces. Packages. ជាកញ្ចប់សំរាប់វេចខ្ចប់ class និង interface។

System.out.println(“This is not file.”);

}

boolean isDirectory(); ជា method មាន;ួ5ទ�វេ�្ទZងផ្ទា្ទ ;់វេម�ល ថាវេ;� object វេ5ះជា obj របស់ File ឬក៏មិនដែមន? វេប�ជា object directory វា Return true ។ឧទាហរណ៍៖

File objD=new File(“C:/myPro/My File”);

if(objD.isDirectory())

System.out.println(“This is directory.”);

else{

System.out.println(“This is not directory.”);

}

boolean renameTo(File F);ជា method មាន;ួ5ទ�Rename File ឬផ្ទា្ល ស់ប្តូរ File វេbកាន់ ទ�តាងំថ្ម�។ឧទាហរណ៍៖

File objF=new File(“C:/myPro/My File/text.txt”);

File objNew=new File(“D:/newFile.txt”);

objF.renameTo(objNew); // Rename and move to D:\newFile.txt

String[] list();ជា method មាន;ួ5ទ�បង្ហាI ញ Name របស់ Directory និង File ដែ&លស្ថិ; វេ,ក្នុងទ�តាងំណាមួ�។ឧទាហរណ៍៖

File objD=new File(“C:/myPro”);

String path[];

path=objD.list();

for(int i=0; i<path.length; i++)

System.out.println(path[i]);

file[] listFiles(); ជា method មាន;ួ5ទ�ទាញ�ក Object Directory និង Object File ដែ&លស្ថិ; វេ,ក្នុងទ�តាងំណាមួ�។ឧទាហរណ៍៖

File objD=new File(“C:/myPro”);

File f[];

f=objD.listFiles();

for(int i=0; i<f.length; i++)

f[i].delete();

long length(); ជា method មាន;ួ5ទ�ទទួល�កទំហំរបស់ File ដែ&លខា្ន ;=ិ;ជា byte ។

Re-compiled by: Mr. HIEENG Sokh Hymns 17

Page 18: forme855.files.wordpress.com€¦ · Web viewChapter 7. Packages and Interfaces. Packages. ជាកញ្ចប់សំរាប់វេចខ្ចប់ class និង interface។

long getTotalSpace(); ជា method មាន;ួ5ទ�ទទួល�កទំហំសរបុរបស់ Drive ណា មួ�ដែ&លខា្ន ;=ិ;ជា byte ។

long getUsableSpace(); ជា method មាន;ួ5ទ�ទទួល�ក Space ព� Drive ណាមួ� ដែ&លខា្ន ;=ិ;ជា byte ។

23 – July – 2011

3. FileInputStream ជា class ដែ&លស្ថិ;វេ,ក្នុង Package java.io.FileInputStream មាន;ួ5ទ� Read ទិន្នន័�ជា File ។*.BufferedInputStream ជា class ដែ&លស្ថិ;វេ,ក្នុង Package java.io.BufferedInputStream មាន ;ួនទ�បវេងJ�;ទ�តាងំបំរងុទុកសំរាប់ Read ទិន្នន័�ជា File ។*.Scanner ជា class ដែ&លស្ថិ;វេ,ក្នុង Package java.util.Scanner មាន;ួ5ទ� Read ទិន្នន័�ជា File បាន$=ប់ DataTypes ជាពិវេសស DataTypes $បវេfទជា String ។

ឧទាហរណ៍៖ How to Read File.wmvpublic class WriteRead{

public static void main(String arg[]){

try{

File f=new File(“java.txt”);

FileInputStream fi=new FileInputStream(f);

BufferedInputStream bi=new BufferedInputStream(fi);

Scanner sc=new Scanner(bi);

While(sc.hasNext()){

System.out.println(sc.nextLine());

}

}catch(Exception e){}

}

}

4. FileOutputStream ជា class ដែ&លស្ថិ;វេ,ក្នុង Package java.io.FileOutputStream មាន;ួ5ទ� Write ទិន្នន័�ចូល File ។*.BufferedOutputStream ជា class ដែ&លស្ថិ;វេ,ក្នុង Package java.io.BufferedOutputStream មាន;ួ5ទ�បវេងJ�;ទ�តាងំបំរងុទុកសំរាប់ Write ទិន្នន័�ចូល File ។*.PrintStream ជា class ដែ&លស្ថិ;វេ,ក្នុង Package java.io.PrintStream មាន;ួ5ទ�អាច Write ជា String ចូលក្នុង File ។ឧទាហរណ៍៖ How to Write File.wmv

public class WriteRead{

public static void main(String arg[]){

try{

Re-compiled by: Mr. HIEENG Sokh Hymns 18

Page 19: forme855.files.wordpress.com€¦ · Web viewChapter 7. Packages and Interfaces. Packages. ជាកញ្ចប់សំរាប់វេចខ្ចប់ class និង interface។

File f=new File(“Data.txt”);

FileOutputStream fo=new FileOutputStream(f,true);

BufferedOutputStream bo=new BufferedOutputStream(fo);

PrintStream ps=new PrintStream(bo);

ps.println();

ps.println(“Hello, Java Programming”);

ps.println(“What subject do you study?”);

ps.close();

bo.close();

fo.close();

}catch(Exception e){}

}

}

*.FileWriter ជា class ដែ&លស្ថិ;វេ,ក្នុង Package java.io.FileWriter មាន;ួ5ទ� Write ទិន្នន័� ជា String ចូលក្នុង File វេហ��វាមិនចាបំាច់$;�ូការ FileOutputStream និង BufferedOutput Stream វេទ។ឧទាហរណ៍៖ How to Write File with FileWriter.wmv

public class WriteRead{

public static void main(String arg[]){

try{

File f=new File(“Data.txt”);

FileWriter fw=new FileWriter(f,true);

fw.write(“How are you?\r\n”);

fw.write(“I’m well.\r\n”);

fw.close();

}catch(Exception e){}

}

}

How to Write and Read 1 Record.wmvLabel: Name TextFieldLabel: Age TextFieldAddress TextField

Command SaveCommand Open

public class WriteRead{

public static void main(String arg[]){

Re-compiled by: Mr. HIEENG Sokh Hymns 19

Page 20: forme855.files.wordpress.com€¦ · Web viewChapter 7. Packages and Interfaces. Packages. ជាកញ្ចប់សំរាប់វេចខ្ចប់ class និង interface។

try{

File f=new File(“MyData.txt”);

FileInputStream fi=new FileInputStream(f);

BufferedInputStream bi=new BufferedInputStream(fi);

Scanner sc=new Scanner(bi);

String st;

st=sc.nextLine();

String d[];

d=st.split(“;”,3);

txtName.setText(d[0]);

txtAge.setText(d[1]);

txtAddress.setText(d[2]);

}catch(Exception e){}

}

}

30 – July – 2011 5. Write and Read Object File

5.1. Write and Read Object File វេ&�ម្ប� Write Object ចូល File វេ��ង$;�ូការ Interface មួ�វេn ថា Serializable ដែ&លស្ថិ;វេ,ក្នុង package java.io.Serializable ដែ&លមាន;ួ5ទ��្ទុក Address របស់ Object និង$;�ូការ class មួ�វេទ+;វេnថា ObjectOutputStream ដែ&លស្ថិ; វេ,ក្នុង package java.io.ObjectOutputStream ដែ&លមាន;ួ5ទ� Write Object ចូល File ។ Object ដែ&ល$;�ូ Write ចូល File $;�ូវេក�;វេចញព� class ដែ&ល implement ជាមួ�នឹង Interface Serializable ។

package TestFile.Sub;

import java.io.Serializable;

public class Data implements Serializable{

public String name;

public int age;

public String address;

public Data(String name; int age; String address){

this.name=name;

this.age=age;

this.address=address;

}

Re-compiled by: Mr. HIEENG Sokh Hymns 20

Page 21: forme855.files.wordpress.com€¦ · Web viewChapter 7. Packages and Interfaces. Packages. ជាកញ្ចប់សំរាប់វេចខ្ចប់ class និង interface។

}

packge TestFile.Sub;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileOutputStream;

import java.io.ObjectOutputStream;

public class WriteObject{

public static void main(String arg[]){

try{

File f=new File(“Mydatabase.sys”);

FileOutputStream fo=new FileOutputStream(f);

BufferedOutputStream bo=new BufferedOutputStream(fo);

ObjecOutputStream objO=new ObjectOutputStream(bo);

Data obj;

obj=new Data(“Mam Phanavuth”,30, “PP”);

objO.writeObject(obj);

objO.close();

bo.close();

fo.close();

}catch(Exception e){

System.out.println(e);

}

}

}

5.2. Read Object From File វេ&�ម្ប� Read Object ព� File វេ��ងចាបំាច់$;�ូការ Interface មួ� វេj្ម ះថា Serializable ដែ&លស្ថិ;វេ,ក្នុង package java.io.Serializable ដែ&លមាន;ួ5ទ��្ទុក Address របស់ object វេហ��វា$;វូាការ class មួ�វេទ+;វេj្ម ះថា ObjectInputStream មាន ;ួ5ទ� Read Object ព� File ។ Object ដែ&លទទួលទិន្នន័�ព� File ចាបំាច់$;�ូវេក�;វេចញព� class រមួគ្នា្ន ជាមួ� Object ដែ&លបាន Write ចូល File ។

packge TestFile.Sub;

import java.io.BufferedInputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.ObjectInputStream;

public class ReadObject{

Re-compiled by: Mr. HIEENG Sokh Hymns 21

Page 22: forme855.files.wordpress.com€¦ · Web viewChapter 7. Packages and Interfaces. Packages. ជាកញ្ចប់សំរាប់វេចខ្ចប់ class និង interface។

public static void main(String arg[]){

try{

File f=new File(“Mydatabase.sys”);

FileInputStream fi=new FileInputStream(f);

BufferedInputStream bi=new BufferedInputStream(fi);

ObjecInputStream objI=new ObjectInputStream(bi);

Data obj;

obj=(Data)objI.readObject();

System.out.println(“Name = ”+obj.name);

System.out.println(“Age = ”+obj.age);

System.out.println(“Address = ”+obj.address);

}catch(Exception e){

System.out.println(e);

}

}

}

SH5_Create_bat_and_Run.wmv

public class WriteRead{

public static void main(String arg[]){

try{

Runtime obj=Runtime.getRuntime();

// obj.exec(“pathProgram pathFile”);

// obj.exec(“\“C:\\Program Files\\Windows Media Player\\

wmplayer.exe\”\“D:\\Song\\Town Vol 09\\AutoRun.bat\” ”);

obj.exec(“C:\\Windows\\explorer.exe\“D:\\Java 2011\\SH5\\

AutoRun.bat\” ”)

}catch(Exception e){}

}

}

The End !

Re-compiled by: Mr. HIEENG Sokh Hymns 22

Page 23: forme855.files.wordpress.com€¦ · Web viewChapter 7. Packages and Interfaces. Packages. ជាកញ្ចប់សំរាប់វេចខ្ចប់ class និង interface។

13 – August – 2011

Chapter 10

Swing

1. What is swing?Swing =ឺជាបណ្តុំ Components ជាវេ$ច�ន ដែ&លវេ$ប�សំរាប់កសាង User Interface ។ឧទាហរណ៍៖

JFrame, JButton, JTextField, JLabel, JList, JComboBox…2. How to create JFrame

import javax.swing.WindowConstants;

public class Swing1{

public class Swing1(){

Re-compiled by: Mr. HIEENG Sokh Hymns 23

Page 24: forme855.files.wordpress.com€¦ · Web viewChapter 7. Packages and Interfaces. Packages. ជាកញ្ចប់សំរាប់វេចខ្ចប់ class និង interface។

frame=new JFrame();

frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

frame.setLayout(NULL);

frame.setSize(800, 600);

cmdOK=new JButton(“OK”);

cmdOK.setBounds(10, 20, 100, 25);

frame.getContentPane().add(cmdOK);

cmdOK.AddActionListener(new ActionListener(){

public void actionPerformed(ActionEvent evt){

cmdOKActionPerformed(evt);

}

});

Frame.setVisible(true);

}

private void cmdOKActionPerformed(ActionEvent evt){

JOptionPane.showMessageDialog(frame, “HI”);

}

public static void main(String arg[]){

new Swing1();

}

private JFrame frame;

private JFrame cmdOK;

}

public class Swing2 extends JFrame{

public class Swing2(){

setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

setSize(800, 600);

setLayout(NULL);

cmdOK=new JButton(“OK”);

cmdOK.setSize(100, 25);

cmdOK.setLocation(10, 20);

cmdOK.AddActionListener(new ActionListener(){

public void actionPerformed(ActionEvent evt){

cmdOKActionPerformed(evt);

Re-compiled by: Mr. HIEENG Sokh Hymns 24

Page 25: forme855.files.wordpress.com€¦ · Web viewChapter 7. Packages and Interfaces. Packages. ជាកញ្ចប់សំរាប់វេចខ្ចប់ class និង interface។

}

});

getContentPane().add(cmdOK);

}

private void cmdOKActionPerformed(ActionEvent evt){

JOptionPane.showMessageDialog(this, “HI”);

}

public static void main(String arg[]){

new Swing2().setVisible(true);

}

private JButton cmdOK;

}

20 – August – 2011

3. How to customize JFrame

// Please, see detail “SH5 how to customize JFrame.wmv”

public class SubFrame extends JFrame{

public SubJFrame(){

addWindowListener(new WindowAdapter (){

formwindowClosing(e);

}

public void windowOpened(WindowEvent e){

formwindowOpened(evt);

}

});

}

private void formwindowOpened(WindowEvent evt){

Dimension ds=java.awt.Toolkit.getDefaultToolkit().getScreenSize();

SetLocation((ds.width-this.getWidth())/2,(ds.height-this.getHeight())/2);

}

Private void formwindowClosing(WindowEvent evt){

int click;

click=JOptionPane.showConfirmDialog(this, “Do you want to Exit?” “Confirm”,

JOptionPane.YES_NO_OPTION);

if(click==JOptionPane.YES_OPTION){

Re-compiled by: Mr. HIEENG Sokh Hymns 25

Page 26: forme855.files.wordpress.com€¦ · Web viewChapter 7. Packages and Interfaces. Packages. ជាកញ្ចប់សំរាប់វេចខ្ចប់ class និង interface។

closeMyFrame();

System.exit(0);

}

}

Public void closeMyFrame(){

// Please, override in your own JFrame

}

4. How to customize DefaultTableModel

// Please, see the detail “SH5 how to customize DefaultTableModel.wmv”

public class SubDefaultTableModel extends DefaultTableModel{

public void removeAllRows(){

while (getRowCount()>0){

removeRow(0);

}

}

public void removeSelectedValues(JTable table){

int index[];

index=table.getSelectedRows();

int j=0;

for(int i=0; i<index.lenght; i++){

removeRow(index[i]-j);

j++;

}

}

private boolean b=false;

public Boolean isCellEditable(int r, int c){

return b;

}

public void setCellEditable(boolean b){

this.b=b;

}

}

5. JTableRe-compiled by: Mr. HIEENG Sokh Hymns 26

Page 27: forme855.files.wordpress.com€¦ · Web viewChapter 7. Packages and Interfaces. Packages. ជាកញ្ចប់សំរាប់វេចខ្ចប់ class និង interface។

Create ListSelectionChange of JTable

// Please, see detail “SH5 how to create Enevt Selection JTable.wmv”

table.getSelectionModel().addListSelectionListener (new ListSelectionListener(){

public void valueChange(ListSelectionEvent e){

tableListSelectionChanged(e);

}

});

private void tableListSelectionChanged(ListSelectionEvent evt){

// Execute when selection has been changed.

}

03 – September – 2011

Create Event columnSelectionChanged of JTable

// Please, see detail “SH5 create Row and ColumnSelectionChange.wmv”

Table.getColumnModel().addColumnModelListener(new TableColumnModelLIstener(){

public void columnAdded(TableColumnModelEvent e){

// Throw new unsupportedOperationException (“Not support yet.”);

}

public void columnRemoved(TableColumnModelEvent e){

//Throw new unsupportedOperationException (“Not support yet.”);

}

public void columnMoved(TableColumnModelEvent e){

//Throw new unsupportedOperationException (“Not support yet.”);

}

public void columnMarginChanged(ChangeEvent e){

//Throw new unsupportedOperationException (“Not support yet.”);

}

public void columnSelectionChanged(ListSelectionEvent e){

tableColumnSelectionChanged(e);

//Throw new unsupportedOperationException (“Not support yet.”);

}

});

private void tableColumnSelectionChanged(ListSelectionEvent evt){

// Execute when column has been changed.

}Re-compiled by: Mr. HIEENG Sokh Hymns 27

Page 28: forme855.files.wordpress.com€¦ · Web viewChapter 7. Packages and Interfaces. Packages. ជាកញ្ចប់សំរាប់វេចខ្ចប់ class និង interface។

Use JTextField, JComboBox, JCheckBox as the cell of JTabletable.getColumnModel().getColumn(int col).setCellEditor(newDefaultCellEditor (Component)); ជា Method មួ�មាន;ួ5ទ� Add Components &ូចជា JTextField, JComboBox, JCheckBox វេធ្វ�ជា Cell របស់ JTable។

Resize column of JTable Table.getColumnModel().getColumn(int col).setPreferedWidth(int width); ជា Method មាន ;ួ5ទ�កំណ;់ទំហំរបស់ Column ដែ&លស្ថិ;វេ,ក្នុង index ណាមួ�ច្នៃន Table ។

10 – September – 2011

How to Customize JTable

Set all TableHeader of JTables

try{

UIManager.getLookAndFeelDefaults().put(“TableHeader.background”,new

color(136,136,136));

UIManager.getLookAndFeelDefaults().put(“TableHeader.foreground”,new

color(0,0,0));

UIManager.getLookAndFeelDefaults().put(“TableHeader.font”,new

font(“Arial”,font.BOLD,12));

}catch(Exception e){}

Using JTableHeader

table.getTableHeader().setBackground(color.red);

table.getTableHeader().setFont(new Font(“Arial”,Font.BOLD,14));

table.getTableHeader().setForeground(color.YELLOW);

How to resize height of TableHeader

table.getTableHeader().setPreferedSize(new Dimension(sp.getWidth(),18));

// sp is the JScrollPane

Date and Calendar (abstract)ជា class ដែ&លស្ថិ;វេ,ក្នុង Package Java.util.Date មាន;ួ5ទ� �្ទុកកាលបរវិេចoទទាងំអ;�;កាល បច្ចុប្បន្ន និងអ5=;។ Calendar ជា abstract class ដែ&លស្ថិ;វេ,ក្នុង Package java.util.calendar មាន;ួ5ទ�ទទួល�កព;៌មានទាងំអស់របស់ កាលបរវិេចoទ។

How to use Date

EX:

Date date=new Date();

String st= “ ”+date;

Re-compiled by: Mr. HIEENG Sokh Hymns 28

Page 29: forme855.files.wordpress.com€¦ · Web viewChapter 7. Packages and Interfaces. Packages. ជាកញ្ចប់សំរាប់វេចខ្ចប់ class និង interface។

System.out.println(st);

// Sat Sept 10.18.22:17 ICT 2011

EX:

SimpleDateFormat sdf=new SimpleDateFormat(“EEE,dd/MMMM/yyy”);

String st=sdf.format(date);

System.out.println(st);

// Saturday, 10/September/2011

EX:

long time

time=System.currentTimeMillis();

string st=sdf.format(time);

system.out.println(st);

// Saturday, 10/September/2011

How to calculate Date

EX:

long millis1=system.currentTimeMillis();

try{

thread.sleep(5000);

}catch(Exception e){}

Long mills2=system.currentTimeMillis();

long millis;

millis=millis2-millis1;

system.out.println(millis);

EX:

Date dataOld=new Date();

long milli;Re-compiled by: Mr. HIEENG Sokh Hymns 29

Page 30: forme855.files.wordpress.com€¦ · Web viewChapter 7. Packages and Interfaces. Packages. ជាកញ្ចប់សំរាប់វេចខ្ចប់ class និង interface។

milli=dateOld.getTime();

long add;

add=2*24*60*60*1000;

milli+=add;

Date dateNew=new Date();

dateNew.setTime(milli);

System.out.println(dataNew);

Using Calendar to control on Date

EX:

calendar cd=calendar.getInstance();

cd.setTime(New Data());

// cd.setTimeInMillis(System.currentTimeMillis());

int day,month,year;

day=cd.get(cd.DAY_OF_MONTH);

month=cd.get(cd.MONTH)+1;

year=cd.get(cd.YEAR);

system.out.println(day+“/”+month “/”+year);

EX:

calendar cd=calendar.getInstance();

int day=5;

int month=8; // September

int year=2011;

cd.set(year,month,day);

Date date=new Date();

Date=cd.getTime();

SimpleDateFormat sdf=new SimpleDateFormat(“EEEE,dd/MMMM/yyyy”);

System.out.println(sdf.format(date));

Re-compiled by: Mr. HIEENG Sokh Hymns 30

Page 31: forme855.files.wordpress.com€¦ · Web viewChapter 7. Packages and Interfaces. Packages. ជាកញ្ចប់សំរាប់វេចខ្ចប់ class និង interface។

EX:

Calendar cd=Calendar.getInstance();

int year=2011;

int month=7; // index of month start from 0 to 11.

Int date=10; // date can start from 1 to 20, 29, 30 or 31.

cd.set(year,month,day);

int dayofweek=cd.get(cd.DAY_OF_WEEK);

// Sunday=1, Monday=2,…,Saturday=7

Int maxDayOfMonth;

maxDayOfMonth=cd.getActualMaximum(cd.DAY_OF_MONTH);

system.out.println(“Maximum of Day in a month”+maxDayOfMonth);

The End !

Re-compiled by: Mr. HIEENG Sokh Hymns 31