แสดงบทความที่มีป้ายกำกับ java แสดงบทความทั้งหมด
แสดงบทความที่มีป้ายกำกับ java แสดงบทความทั้งหมด

วันพฤหัสบดีที่ 7 ตุลาคม พ.ศ. 2553

วิธีกำหนด JFrame ให้อยู่กลางจอ

ถ้าเราจะจัดให้ JFrame ของเราอยู่กลางหน้าได้โดยเรียกใช้ Function getToolkit().getScreenSize() เราจะได้ขนาดของหน้าจอกลับมาในรูปของ Dimention Object (ไอ้เจ้าฟังก์ชั่น getToolkit() ของ JFrame นี้มัน inherit มาจาก Window) ลองมาดูตัวอย่างกันคับ

Dimension d = getToolkit().getScreenSize();

int screenWidth = d.width;

int screenHeight = d.height;

หลังจากนั้นเราก็มาหาตำแหน่งกึ่งกลางของจอโดยเอาค่าความกว้างกับความยาวมาหารด้วย2

int centerX = screenWidth/2;

int centerY = screenHeight/2;

แล้วก็มาหาตำแหน่งที่เราจะวาง JFrame เพื่อให้ frame นั้นอยู่กลางหน้าจอ เนื่องจากว่าเวลาเรากำหนดตำแหน่งของ frame ตำแหน่งที่เรากำหนดจะเป็นตำแหน่งของมุมซ้ายด้านบนของ frame มันเลยไม่อยู่ตรงกลางให้เรา (55+) ดังนั้นถ้าเราจะกำหนดตำแหน่งกึ่งกลางจอให้กับ JFrame ตรงๆนั้น frame ก็จะไม่ได้อยู่ตรงกลางเราจึงต้องคำนวนหาตำแหน่งของมันซะก่อน โดยลบค่าของ centerX และ centerY ด้วยขนาดครึ่งนึงของ frame ตามลำดับดังนี้

int xPos = centerX – frame.getWidth() / 2;

int yPos = centerY – frame.getHeight() / 2;

*สมมติให้ frame เป็น reference ของ JFrame

และแล้วเราก็จะได้ตำแหน่งที่จะต้องกำหนดให้กับ JFrame แว้ววว… ก็มาsetค่าตำแหน่งให้กับมันเลยคับ

frame.setLocation(xPos,yPos);

ก็เป็นอันว่า frame ของเราก็จะอยู่ตรงกลางเป็นที่เรียบร้อยละค๊าบบ.. มาดูตัวอย่างกันดีกว่า อิอิ

import javax.swing.*;

import java.awt.*;

public class TestJFrame extends JFrame {

  public static void main(String[] agrs) {

    JFrame f = new JFrame(“Centering JFrame”);

    f.setSize(200,200);

    Dimension d = f.getToolkit().getScreenSize();

    int screenWidth = d.width;

    int screenHeight = d.height;

    int centerX = screenWidth / 2;

    int centerY = screenHeight / 2;

    int xPos = centerX – f.getWidth() / 2;

    int yPos = centerY – f.getHeight() / 2;

    f.setLocation(xPos,yPos);

    f.setVisible(true);

  }

}

วันเสาร์ที่ 17 กรกฎาคม พ.ศ. 2553

Overriding Method

Methods defined in a superclass can be overridden by methods defined in a subclass. Here is the definition:

Definition Overiding: Overriding refers to the introduction of an instance method in a subclass that has the same name,signature,and return type of a method in the superclass. Implementation of the method in the subclass replaces the implementation of the method in the superclass.

Overriding is different from overloading.Overriding is concerned with methods of different classes that have an inheritance relationship.In overriding,methods share the same name,signature,and return type.In contrast,overloaded methods are part of the same class,but have different signatures.

We can illustrate the distinction between overloading and overriding in the following manner. In

class A{
        public void m1(){
            //statement
        }
        public void m1(int i){
            //statement
        }
    }

class A contain two overloaded methods.An instance of A can access both methods,depending on the arguments of the method invocation,so

A a = new A();
a.m1();            //invoke m1()
a.m1(1);          //invoke m1(int)

Now,let us assume that we have the two classes

class B{
    public void m2(){
        //statement
    }
}
class C{
    public void m2(){
        //statement
    }
}

Implementation of method m2() in class B is overridden by another implementation of method m2() in class C. For a given object,one but not both of the implementations of method m2() is available, depending on the class of the object.

B b = new B();
C c = new C();
b.m2(); //invoke the m2() in class B
c.m2(); //invoke the m2() in class C

Overriding a method with another method of different signature or return type is not allowed.For example,the following code segment will cause a compilation error:

class B{
    public void m3(int i){
    }
}
class C{
    public void m3(char c){
    }
}

วันศุกร์ที่ 16 กรกฎาคม พ.ศ. 2553

Overloading Method

Definition Overloading: Overloading refers to the ability to allow different methods of constructors of a class to share the same name. The name is said to be overloaded with multiple implementations.

The legality of overloading depends on the signatures of the methods or constructors being overloaded. The signature of a methods or constructor consists of the name of  the method and a list of the types of its parameters.Note that the return type,parameter names and final designations of parameters are not part of the signature. Parameter order,however,is significant. The following are some examples of methods and their signatures.

Method Signature
String toString() toString()
void move(int dx,int dy) move(int,int)
void move(final int dx,final int dy) move(int,int)
void paint(Graphics g) paint(Graphics)

The Rule of Overloading: Two methods of constructors in the same class can be overloaded,i.e.,sharing the same name,if they have either different numbers of parameters or the same number of parameters but of different types.In other words,no two methods or constructors in the same class may have identical signatures.

The following class declaration illustrates overloaded constructors and overloaded methods:

public class Point {
    protected double x,y;
    public Point(){
        x = 0.0;y = 0.0;
    }
    public Point(double x,double y){
        this.x = x;this.y = y;
    }
    /** calculate the distance between this point and the other point */
    public double distance(Point other){
        double dx = this.x - other.x;
        double dy = this.y - other.y;
        return Math.sqrt(dx * dx + dy *dy);
    }
    /** calculate the distance between this point and(x,y) */
    public double distance(double x,double y){
        double dx = this.x - x;
        double dy = this.y - y;
        return Math.sqrt(dx * dx + dy * dy);
    }
    /** calculate the distance between this point and(x,y) */
    public double distance(int x,int y){
        double dx = this.x - (double) x;
        double dy = this.y - (double) y;
        return Math.sqrt(dx * dx + dy * dy);
    }
    /** calculate the distance between this point and the origin */
    public double distance() {
        return Math.sqrt(x * x + y * y);
    }
    // other method
}

When an overloaded method is called, the number and the types of the arguments are used to determine the signature of the method that will be invoked.Overloading is resolved at compile time,as in the following code segment:

Point p1 = new Point();                               //invoke Point()
Point p2 = new Point(20.0,30.0);                 //invoke Point(double,double)
p2.distance(p1);                                         //invoke distance(Point)
p2.distance(50.0,60.0);                               //invoke distance(double,double)
p2.distance(50,60);                                    //invoke distance(int,int)
p2.distance();                                            //invoke distance()

วันอาทิตย์ที่ 11 กรกฎาคม พ.ศ. 2553

การสร้าง Package ใน Java

1.ตั้งชื่อ package
สมมุติให้ชื่อว่า mypackage.homework
2. package ที่เราสร้างต้องอยู่ในโฟลเดอร์หนึ่งๆ
ในที่นี้ให้อนู่ใน E:\user
3.เราต้องให้ java รู้ว่านอกจาก stardard java class แล้วยังมี package อื่นอีก
เราต้องไปกำหนดค่า base ใน classpath
วิธีการกำหนดค่าใน classpath
Start --> Settings --> Control Panel --> Systems --> Advance
--> Enviroment Variable --> หาตัวที่เราเก็บ java ไว้โดยมากจะชื่อว่า
path หรือไม่ ก็ class path เลือกแล้วกด Edit --> แล้วใส่ที่อยู่ของ package
ในที่นี้ คือ E:\user แล้วใน semi colon ต่อท้าย --> Ok
4.เราจะไปสร้างโฟลเดอร์ต่อจากที่เราเก็บที่อยู่ คือไปสร้างต่อที่ E:\user
ในที่นี้ให้ชื่อว่า mypackage
5.แล้วเราไปสร้างโฟลเดอร์ต่อจาก mypackage ให้ชื่อว่า homework
6.เสร็จแล้วเราของเอา class ที่เราเขียนมาใส่ในโปลเดอร์ homework
7.วิธีการสร้าง package เราจะใช้ keyword คำว่า package ในการขึ้นต้น
เพื่อทำให้ java ทราบว่าเรากำลังจะสร้าง package ใหม่
package mypackage.homework; //ชื่อ package กับ keyword ว่า package
public class BankAccount { //ชื่อ class ที่จะสร้างไว้เป็น package
//instance variable
//constructor
//method
}
แล้ว compile ให้ได้ไฟล์ .class เอาไว้ที่ E:\user\mypackage\homework\ ด้วยนะครับ
8.วิธีการใช้เราต้อง import เข้ามา
import mypackage.homework.BankAccount ;
public class BankAccountTest {
BankAccount A_NagativesAcc = new BankAccount(500.00);
}
credit : citec.us