You are on page 1of 2

//Hien thi voi console

public class Welcome


{
public static void main ( String [] args )
{
System.out.println( "Hello World !");
}
}
// Hien thi voi hop thoai
import javax.swing.JOptionPane;
public class WelcomeInMessageDialogBox
{
public static void main ( String [] args )
{
JOptionPane.showMessageDialog(null, "Hello World !");
}
}
// Co hai cach su dung showMessageDialog( )
Cach 1: nhu tren
JOptionPane.showMessageDialog( null, x ); ( x la cau text can hien thi )
Cach 2:
JOptionPane.showMessageDialog( null, x, y, INFORMATION_MESSAGE);
trong do : x la cau text can hien thi, y la tieu de cua hop thoai hien ra
INFORMATION_MESSAGE la dau cham than hien ben trai
//----------------- Nhap su dung Scanner ---------------import java.util.Scanner;
public class ComputeAreaWithConsoleInput
{
public static void main ( String [] args )
{
final double PI = 3.14159;
Scanner input = new Scanner (System.in);
System.out.print("Enter a number for radius: ");
double radius = input.nextDouble();
double area = radius * radius * PI;
System.out.println("The area for the circle of radius " + radius + "is"
+ area);
}
}
//---------- Hien thi thoi gian hien tai
public class ShowCurrentTime
{
public static void main ( String [] args)
{
long totalMilliseconds = System.currentTimeMillis();
long totalSeconds = totalMilliseconds / 1000;
long currentSecond = (int)(totalSeconds % 60);
long totalMinutes = totalSeconds / 60;
long currentMinute = totalMinutes % 60;
long totalHours = totalMinutes / 60;
long currentHour = totalHours % 24;
System.out.println("Current time is " + currentHour + ":" + currentMinute + ":"
+ currentSecond + " GMT");
}
}
//---------Hien thi Unicode ---------trang 42

import javax.swing.JOptionPane;
public class DisplayUnicode
{
public static void main( String [] args)
{
JOptionPane.showMessageDialog(null, "\u6B22\u8FCE \u03b1 \u03b2 \u0
3b3",
"\u6B22\u8FCE Welcome", JOptionPane.INFORMATION_MESSAGE);
}
}
//----------- Hien thi hop thoai co dong de nhap vao ------- trang 55
su dung : JOptionPane.showInputDialog
Cach 1:
String s = JOptionPane.showInputDialog( x );
voi x la 1 chuoi la 1 loi nhan nhac nho
Cach 2:
String s = JOptionPane.showInputDialog( null, x , y, JOptionPane.QUESTION_MESSAG
E);
trong do : x la 1 chuoi la 1 loi nhan nhac nho user nhap chuoi
y la tieu de cua hop thoai hien ra
QUESTION_MESSAGE la ky hieu dau ? di kem
//---------- Chuong trinh vi du ket hop showMessageDialog ( ) va showInputDialog
( )
Truoc het can biet ve : Converting Strings to Number ( trang 56 )
Ly do: dau vao duoc tra lai tu hop input dialog la 1 chuoi (string) .Neu ban nha
p
vao 1 gia tri so ( vd : 123 ), no se tra lai 123.Nen ban phai chuyen doi no sang
dang so de
thu duoc dau vao la 1 so => de chuyen 1 chuoi sang 1 gia tri int
ta su dung phuong thuc parseInt trong lop Integer, nh sau:
int intValue = Interger.parseInt(intString);
nu la double thi cung tng t :
int intValue Double.parseDouble(doubleString);
Cac lp Interger va Double u c cung cp trong goi java.lang ( => chung c t ng nhp
vi du : Tr lai chng trinh tinh din tich hinh tron
import javax.swing.JOptionPane;
public class CircleAreaWithDialog
{
public static void main ( String [] args )
{
final double PI = 3.14159;
//Hin thi hp thoai nhc nh nhp gia tri cua ban kinh
String radiusString = JOptionPane.showInputDialog("Enter a number fo
r radius: ");
//Chuyn ban kinh dang String va nhp sang s tinh toan
double radius = Double.parseDouble(radiusString);
double area = radius * radius * PI;
//Chui thng bao hin thi kt qua ( din tich )
String areaString = "Area of circle with radius " + radius + " is "
+ area ;
//a chui thng bao kt qua vao hp hin thi
JOptionPane.showMessageDialog(null, areaString);
}
}