package baocao;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Bai32 extends Applet implements ActionListener,ItemListener {
Label lb=new Label("The item is display here");
CheckboxGroup cg=new CheckboxGroup();
Checkbox nam=new Checkbox("Male", cg, true);
Checkbox nu=new Checkbox("Female", cg, false);
List list1 =new List(3,false);
Choice ch1=new Choice();
Button exit=new Button("Exit");
Panel pn=new Panel();
GridBagLayout gb=new GridBagLayout();
GridBagConstraints gbs=new GridBagConstraints();
54 trang |
Chia sẻ: tienthan23 | Lượt xem: 2395 | Lượt tải: 0
Bạn đang xem trước 20 trang tài liệu Bài thực hành ngôn ngữ lập trình java, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
System.out.println("Nhap gio ket thuc ");
b = input.nextInt();
}
public int tinhtien()
{
int sotien=0;
if(a> 0 && b<=18) sotien = (b-a)*45000;
if(a>0 && b>18) sotien = (18-a)*45000 + (b-18)*60000;
if(a>18) sotien = (b-a)*60000;
return sotien;
}
public static void main(String[] str)
{
Bai4 k =new Bai4();
k.nhap();
System.out.println("So tien can fai tra la : "+k.tinhtien());
}
}
Nhập vào tháng năm bất kì in ra số ngày ứng với tháng năm đó:
package baocao;
import java.util.Scanner;
public class Bai5 {
int nam,thang;
public Bai5() {
}
public boolean namnhuan(int nam)
{
if((nam%4==0 && nam%100!=0)||(nam%400==0)) return true;
else return false;
}
public void nhap()
{
Scanner input= new Scanner (System.in);
System.out.println("Nhap nam thang ");
System.out.print("Nam ");
nam=input.nextInt();
System.out.print("Thang ");
thang=input.nextInt();
}
public void inra()
{
switch(thang)
{
case 4:
case 6:
case 9:
case 11: System.out.println("Thang "+thang+" nam "+nam+" co 30 ngay");break;
case 2 : if(namnhuan(nam))
{System.out.println("Thang "+thang+" nam"+nam+" co 29 ngay");
break; }
else {System.out.println("Thang "+thang+" nam "+nam+" co 28 ngay");
break;}
default: System.out.println("Thang "+thang+" nam "+nam+" co 31 ngay");break;
}
}
public static void main (String[] args) {
Bai5 temp= new Bai5();
temp.nhap();
temp.inra();
}
}
Cấu trúc lặp
Viết chương trình tính: 1+1/2+1/3+...+1/n
package baocao;
import java.util.Scanner;
public class Bai6 {
int n;
public void nhapn()
{
Scanner input=new Scanner(System.in);
System.out.println("Nhap n");
n=input.nextInt();
}
public void tinh()
{
float s=0;
for(int i=1;i<=n;i++) s+=(float) 1/i;
System.out.println("S= "+s);
}
public static void main (String[] args) {
Bai6 b6=new Bai6();
b6.nhapn();
b6.tinh() ;
}
}
Viết chương trình tính : S=15-1+1/2-1/3!+....+(-1)n 1/n!
package baocao;
import java.util.Scanner;
public class Bai7
{
int n;
public void nhap()
{
Scanner input =new Scanner(System.in);
System.out.print("Nhap n = ");
n = input.nextInt();
}
public int giaithua(int m)
{
if(m==0) return 1;
else return m*giaithua(m-1);
}
public void tinh()
{
float s=15.0f;
for(int i=1;i<=n;i++)
{
float p = 1.0f/giaithua(i); p*=Math.pow(-1,i);
s+=p;
}
System.out.print("S = " + s);
}
public static void main(String[] str)
{
Bai7 b = new Bai7();
b.nhap();
b.tinh();
}
}
Viết chương trình tính : S=1+1/3!+1/5!+..+1/(2n-1)!
package baocao;
import java.util.Scanner;
public class Bai8
{
int n;
public void nhap()
{
Scanner input =new Scanner(System.in);
System.out.println("Nhap n : ");
n = input.nextInt();
}
public int giaithua(int m)
{
if(m==0) return 1;
else return m*giaithua(m-1);
}
public void tinh()
{
float s=0.0f;
for(int i=1;i<2*n;i+=2) s+=1.0f/giaithua(i);
System.out.print("S = " + s);
}
public static void main(String[] str)
{
Bai8 b = new Bai8();
b.nhap();
b.tinh();
}
}
Tính n!! = 1*3*5*..*n(n lẽ)
= 2*4*6*.*n(n chẵn)
package baocao;
import java.util.Scanner;
public class Bai9 {
int n;
public void nhap()
{
Scanner input=new Scanner(System.in);
System.out.print("Nhap n= ");
n=input.nextInt();
}
public void tinh()
{
float gt=1;
//int i;
for(int i=n;i>=1;i-=2) gt*=i;
//for(i=((n%2)==0)?2:1;i<=n;i+=2) gt*=i;
System.out.println(n+"!!= "+gt);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Bai9 dt=new Bai9();
dt.nhap();
dt.tinh();
}
}
Tính tổng và tích các chữ số của một số nguyên dương m cho trước:
package baocao;
import java.util.Scanner;
public class Bai10 {
int m;
public void nhap()
{
Scanner input=new Scanner(System.in);
System.out.print("Nhap so nguyen duong m= ");
m=input.nextInt();
}
public void tinhtong()
{
int s=0,p=1,tam=m;
while(tam!=0)
{
s+=tam%10;
p*=tam%10;
tam=tam/10;
}
System.out.println("Tong cac chu so cua "+m+" la "+s);
System.out.println("Tich cac chu so cua "+m+" la "+p);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Bai10 dt=new Bai10();
dt.nhap();
dt.tinhtong();
}
}
Nhập một số và kiểm tra có phải nguyên tố không?
package baocao;
import java.util.Scanner;
public class Bai11 {
static int n;
public int get() {
return n;
}
public void nhap() {
Scanner input = new Scanner(System.in);
System.out.print("Nhap so : ");
n = input.nextInt();
}
public boolean check() {
int i=2;
if (n == 0 || n == 1) return false;
while (i <= n/2) {
if (n%i == 0) return false;
i ++;
}
return true;
}
public static void main(String args[]) {
Bai11 a = new Bai11();
a.nhap();
if (a.check()) System.out.println(n + " la so nguyen to");
else System.out.println(a.get() + " ko phai la so nguyen to");
}
}
Kiểm tra số P có phải là số chính phương không?
package baocao;
import java.util.Scanner;
public class Bai12 {
static int n;
private Scanner input;
void nhap(){
input=new Scanner(System.in);
System.out.printf("Nhap n= ");
n=input.nextInt();
}
public boolean chinhphuong(){
for(int i=0;i<=n;i++) if(i*i==n) return true;
return false;
}
public static void main(String args[])
{
Bai12 dt =new Bai12();
dt.nhap();
if(dt.chinhphuong()) System.out.println(n+ " la so chinh phuong");
else System.out.println(n+ " khong la so chinh phuong");
}
}
Kiểm tra số M có phải là số đối xứng không?
package baocao;
import java.util.Scanner;
public class Bai13 {
static int n;
private Scanner input;
public void nhap(){
input=new Scanner(System.in);
System.out.print("Nhap n=");
n=input.nextInt();
}
public void doixung(int n){
int dao=0,tam=n;
while(tam!=0)
{
dao*=10;
dao+=tam%10;
tam/=10;
}
if(dao==n) System.out.println(n+ " la so doi xung");
else System.out.println(n+" Khong phai la so doi xung");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Bai13 dt=new Bai13();
dt.nhap();
dt.doixung(n);
}
}
In ra các số nguyên tố nhỏ hơn hoặc bằng số nguyên dương n cho trước:
package baocao;
import java.util.Scanner;
public class Bai14 {
int n;
private Scanner input;
public void nhap(){
input=new Scanner(System.in);
System.out.print("Nhap n=");
n=input.nextInt();
}
public boolean nto(int n)
{
if(n==0||n==1) return false;
else for(int i=2;i<n;i++) if(n%i==0) return false;
return true;
}
public void in(){
System.out.println("Cac so nguyen to <= " +n+ " la : ");
for(int i=2;i<=n;i++) if(nto(i)) System.out.print(" "+i);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Bai14 dt=new Bai14();
dt.nhap();
dt.in();
}
}
In ra các số hoàn hảo nhỏ hơn 1000.
( Ví dụ : 6=1+2+3, 28=1+2+4+7+14)
package baocao;
public class Bai15 {
public boolean hh(int n)
{
int sum=0;
for (int i=1;i<n;i++) if(n%i==0) sum+=i;
if (sum==n) return true;
return false;
}
public void inra()
{
System.out.println ("Cac so hoan hao <1000 la: ");
for(int i=1;i<1000;i++) if(hh(i)) System.out.println(i);
}
public static void main(String[] args) {
Bai15 dt=new Bai15();
dt.inra();
}
}
In ra n chữ số Fibonaci đầu tiên:
package baocao;
public class Bai16 {
int n;
public Bai16(int m)
{
n=m;
}
public int fibo(int i)
{
if(i==1||i==2) return 1;
else return (fibo(i-1)+fibo(i-2));
}
public void inra()
{
System.out.println(n+" Chu so Fibonaci dau tien la: ");
for(int i=1;i<=n;i++)
System.out.print(fibo(i)+" ");
}
public static void main (String[] args)
{
Bai16 dt=new Bai16(10);
dt.inra();
}
}
Kiểm tra số K có thuộc dãy Fibonaci hay không?
package baocao;
import java.util.Scanner;
public class Bai17 {
int n;
private Scanner input;
public void nhap()
{
input=new Scanner(System.in);
System.out.print("Nhap n= ");
n=input.nextInt();
}
public void testnto()
{
int x=0,y=1,z=0;
while(z<n)
{
z=x+y;
x=y;
y=z;
}
if(z==n) System.out.println(n+ " la so FIBONACI");
else System.out.println(n+" Khong phai la so FBONACI");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Bai17 dt=new Bai17();
dt.nhap();
dt.testnto();
}
}
Tìm ước chung lớn nhất và bội chung nhỏ nhất của 2 số a và b:
package baocao;
import java.util.Scanner;
public class Bai18 {
int a,b;
private Scanner input;
public void nhap()
{
input=new Scanner(System.in);
System.out.println("Nhap 2 so: ");
System.out.print("a= ");
a=input.nextInt();
System.out.print("b= ");
b=input.nextInt();
}
public void ucbc()
{
int x=a,y=b,uc;
if(x==y) uc=x;
else{
while(x!=y)
{if(x>y) x=x-y;
else y=y-x;
}
uc=x;
}
System.out.println("UCLN cua "+a+" va "+b+" la: "+uc);
System.out.println("BCNN cua "+a+" va "+b+" la: "+(a*b)/uc);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Bai18 dt=new Bai18();
dt.nhap();
dt.ucbc();
}
}
LẬP TRÌNH AWT-SWING
Giải phương trình bậc nhất:
package baocao;
import java.awt.*;
import java.awt.event.*;
public class Bai19 extends Frame implements ActionListener
{
Label lb1,lb2,lb3,lb;
TextField txta,txtb,txtkq;
Button kq,reset,thoat;
Panel pn,pn1,pn2,pn3;
public void GUI() {
lb=new Label("Giai phuong trinh bac nhat");
lb1=new Label("Nhap a");
lb2=new Label("Nhap b");
lb3=new Label("Ket qua");
txta=new TextField();
txtb=new TextField();
txtkq=new TextField();
kq=new Button("Tinh");
reset=new Button("Reset");
thoat=new Button("Thoat");
kq.addActionListener(this);
reset.addActionListener(this);
thoat.addActionListener(this);
pn=new Panel(new GridLayout(3,1));
pn1=new Panel(new GridLayout(1,1));
pn2=new Panel(new GridLayout(3,2));
pn3=new Panel(new GridLayout(1,3));
pn1.add(lb);
pn2.add(lb1);
pn2.add(txta);
pn2.add(lb2);
pn2.add(txtb);
pn2.add(lb3);
pn2.add(txtkq);
pn3.add(kq);
pn3.add(reset);
pn3.add(thoat);
pn.add(pn1);
pn.add(pn2);
pn.add(pn3);
add(pn);
setSize(400,300);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==kq)
{
int a=Integer.parseInt(txta.getText());
int b=Integer.parseInt(txtb.getText());
if(a!=0) txtkq.setText(Float.toString((float)-b/a) );
else if(b==0) txtkq.setText("Pt vo so nghiem");
else txtkq.setText("Pt vo nghiem");
}
if(e.getSource()==reset)
{
txta.setText("");
txtb.setText("");
txtkq.setText("");
}
if(e.getSource()==thoat) System.exit(0);
}
public Bai19(String st)
{
super(st);
GUI();
}
public static void main (String[] args) {
new Bai19("Giai pt bac 1");
}
}
Minh họa các phép toán:
package baocao;
import java.awt.*;
import java.awt.event.*;
public class Bai20 extends Frame implements ActionListener
{
Label lb,lb1,lb2,lb3;
TextField txta,txtb,txtkq;
Button cong,tru,nhan,chia,exit,reset;
Panel pn,pn1,pn2,pn3,pn4;
public void GUI()
{
lb=new Label("Minh hoa cac phep toan");
lb1=new Label("Nhap a");
lb2=new Label("Nhap b");
lb3=new Label("Ket qua");
txta=new TextField("");
txtb=new TextField("");
txtkq=new TextField("");
cong=new Button("Cong");
tru=new Button("Tru");
nhan=new Button("Nhan");
chia=new Button("Chia");
exit=new Button("Exit");
reset=new Button("Reset");
cong.addActionListener(this);
tru.addActionListener(this);
nhan.addActionListener(this);
chia.addActionListener(this);
exit.addActionListener(this);
reset.addActionListener(this);
pn=new Panel(new GridLayout(4,1));
pn1=new Panel(new GridLayout(1,1));
pn2=new Panel(new GridLayout(3,2));
pn3=new Panel(new FlowLayout(FlowLayout.CENTER));
pn4=new Panel(new FlowLayout(FlowLayout.CENTER));
pn1.add(lb);
pn2.add(lb1);
pn2.add(txta);
pn2.add(lb2);
pn2.add(txtb);
pn2.add(lb3);
pn2.add(txtkq);
pn3.add(cong);
pn3.add(tru);
pn3.add(nhan);
pn3.add(chia);
pn4.add(exit);
pn4.add(reset);
pn.add(pn1);
pn.add(pn2);
pn.add(pn3);
pn.add(pn4);
add(pn);
//setSize(400,300);
this.setBounds(200,200,400,300);
//pack();
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==reset)
{
txta.setText("");
txtb.setText("");
txtkq.setText("");
}
else if(e.getSource()==exit) System.exit(0);
else
{
double a=Double.parseDouble(txta.getText());
double b=Double.parseDouble(txtb.getText());
if (e.getSource()==cong) txtkq.setText(Double.toString(a+b));
if (e.getSource()==tru) txtkq.setText(Double.toString(a-b));
if (e.getSource()==nhan) txtkq.setText(Double.toString(a*b));
if (e.getSource()==chia) txtkq.setText(Double.toString(a/b));
}
}
public Bai20(String st)
{
super(st);
GUI();
}
public static void main (String[] args) {
new Bai20("Minh hoa cac phep toan");
}
}
In ra các số nguyên tố nhỏ hơn hoặc bằng số n cho trước:
package baocao;
import java.awt.*;
import java.awt.event.*;
public class Bai21 extends Frame implements ActionListener
{
//String chuoi1=new String();
//String chuoi2=new String(" ");
Label lb,lb1,lb2;
TextField txtnhap,txtkq;
Button tim,reset,exit;
Panel pn,pn1,pn2,pn3;
public void GUI1(){
lb=new Label("Cac so nguyen to <=n ");
lb1=new Label("Nhap n:");
lb2=new Label("KQ:");
txtnhap=new TextField();
txtkq=new TextField();
txtkq.setEditable(false);
tim=new Button("Tim");
exit=new Button("Exit");
reset=new Button("Reset");
tim.addActionListener(this);
exit.addActionListener(this);
reset.addActionListener(this);
pn=new Panel(new GridLayout(3,1));
pn1=new Panel(new GridLayout(1,1));
pn2=new Panel(new GridLayout(2,2));
pn3=new Panel(new FlowLayout(FlowLayout.CENTER));
pn1.add(lb);
pn2.add(lb1);
pn2.add(txtnhap);
pn2.add(lb2);
pn2.add(txtkq);
pn3.add(tim);
pn3.add(reset);
pn3.add(exit);
pn.add(pn1);
pn.add(pn2);
pn.add(pn3);
add(pn);
this.setBounds(200,200,400,300);
this.setVisible(true);
}
public boolean Test(int t)
{
if(t==0||t==1) return false;
else for(int i=2;i<=(t/2);i++) if((t%i)==0) return false ;
return true;
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==reset)
{
txtnhap.setText("");
txtkq.setText("");
}
if(e.getSource()==exit) System.exit(0);
if(e.getSource()==tim)
{
int n=Integer.parseInt(txtnhap.getText());
String chuoi1="";
if(n>=2)for(int i=2;i<=n;i++) if(Test(i)) chuoi1+=" "+i;
/* {chuoi1=chuoi1.concat(Integer.toString(i));
chuoi1=chuoi1.concat(chuoi2);
}*/
txtkq.setText(chuoi1);
//chuoi1="";
}
}
public Bai21(String st){
super(st);
GUI1();
}
public static void main (String[] args) {
new Bai21("Tim so nguyen to");
}
}
Kiểm tra một số có thuộc dãy Fibonaci hay không?
package baocao;
import java.awt.*;
import java.awt.event.*;
public class Bai22 extends Frame implements ActionListener{
Label lb,lb1,lb2;
TextField txtnhap,txtkq;
Button ok,reset,exit;
Panel pn=new Panel();
GridBagLayout gb=new GridBagLayout();
GridBagConstraints gbs=new GridBagConstraints();
public Bai22(String st)
{
super(st);
lb=new Label("Kiem tra a co thuoc day Fibonaci");
lb1=new Label("Nhap a:");
lb2=new Label("KQ: ");
txtnhap=new TextField(20);
txtkq=new TextField(20);
ok=new Button("OK");
reset=new Button("Reset");
exit=new Button("Exit");
ok.addActionListener(this);
reset.addActionListener(this);
exit.addActionListener(this);
//pn=new Panel(new GridBagLayout());
gbs.insets=new Insets(2, 2, 2, 2);
gbs.fill=GridBagConstraints.BOTH;
gbs.gridx=1;
gbs.gridy=1;
gbs.gridwidth=6;
gbs.gridheight=1;
gb.setConstraints(lb, gbs);
gbs.gridx=1;
gbs.gridy=2;
gbs.gridwidth=2;
gbs.gridheight=1;
gb.setConstraints(lb1, gbs);
gbs.gridx=3;
gbs.gridy=2;
gbs.gridwidth=4;
gbs.gridheight=1;
//gbs.ipadx=4;
gb.setConstraints(txtnhap, gbs);
gbs.gridx=1;
gbs.gridy=3;
gbs.gridwidth=2;
gbs.gridheight=1;
gb.setConstraints(lb2, gbs);
gbs.gridx=3;
gbs.gridy=3;
gbs.gridwidth=4;
gbs.gridheight=1;
gb.setConstraints(txtkq, gbs);
//gbs.fill=GridBagConstraints.BOTH;
gbs.gridx=1;
gbs.gridy=4;
gbs.gridwidth=2;
gbs.gridheight=1;
//gbs.weightx=1;
gb.setConstraints(ok, gbs);
gbs.gridx=3;
gbs.gridy=4;
gbs.gridwidth=2;
gbs.gridheight=1;
//gbs.weightx=1;
gb.setConstraints(reset, gbs);
gbs.gridx=5;
gbs.gridy=4;
gbs.gridwidth=2;
gbs.gridheight=1;
//gbs.weightx=1;
gb.setConstraints(exit, gbs);
pn.add(lb);
pn.add(lb1);
pn.add(txtnhap);
pn.add(lb2);
pn.add(txtkq);
pn.add(ok);
pn.add(reset);
pn.add(exit);
pn.setLayout(gb);
this.add(pn);
this.setSize(400, 200);
setVisible(true);
}
public boolean fibo(int n)
{
int x=0,y=1,z=0;
if(n!=0)
{while(z<n)
{
x=y;
y=z;
z=x+y;
}
if(z==n) return true;
}
return false;
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==exit) System.exit(0);
else if(e.getSource()==reset) {
txtnhap.setText("");
txtkq.setText("");
}
else {
int a=Integer.parseInt(txtnhap.getText());
if(fibo(a)) txtkq.setText(a+" la so FIBONACI");
else txtkq.setText(a+" khong la so FIBONACI");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Bai22("Kiem tra fibo");
}
}
Mô tả máy tính điện tử cá nhân
package baocao;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Bai23 extends JFrame implements ActionListener{
GridBagLayout gb=new GridBagLayout();
GridBagConstraints gbc=new GridBagConstraints();
final int MAX_INPUT_LENGTH=20;
final int INPUT_MODE=0;
final int RESULT_MODE=1;
final int ERROR_MODE=2;
int displayMode=INPUT_MODE;
boolean ClearOnNextDigit,percent;
double lastnumber=0;
String lastoperator="0";
private JTextField txtkq=new JTextField("0",20);
private JButton jbnButtons[];
JPanel jpn=new JPanel();
public Bai23(String s)
{
super(s);
jbnButtons=new JButton[23];
gbc.insets=new Insets(3, 3, 3, 3);
gbc.fill=GridBagConstraints.BOTH;
for(int i=0;i<=9;i++) jbnButtons[i]= new JButton(String.valueOf(i));
jbnButtons[10]=new JButton("+/-");
jbnButtons[11]=new JButton(".");
jbnButtons[12]=new JButton("=");
jbnButtons[13]=new JButton("/");
jbnButtons[14]=new JButton("*");
jbnButtons[15]=new JButton("-");
jbnButtons[16]=new JButton("+");
jbnButtons[17]=new JButton("sqrt");
jbnButtons[18]=new JButton("1/x");
jbnButtons[19]=new JButton("%");
jbnButtons[20]=new JButton("Backspace");
jbnButtons[21]=new JButton("CE");
jbnButtons[22]=new JButton("C");
gbc.gridx=0;
gbc.gridy=0;
gbc.gridheight=2;
gbc.gridwidth=5;
gb.setConstraints(txtkq, gbc);
gbc.gridx=0;
gbc.gridy=2;
gbc.gridheight=1;
gbc.gridwidth=2;
gb.setConstraints(jbnButtons[20], gbc);
gbc.gridx=2;
gbc.gridy=2;
gbc.gridheight=1;
gbc.gridwidth=2;
gb.setConstraints(jbnButtons[21], gbc);
gbc.gridx=4;
gbc.gridy=2;
gbc.gridheight=1;
gbc.gridwidth=1;
gb.setConstraints(jbnButtons[22], gbc);
gbc.gridx=0;
gbc.gridy=3;
gbc.gridheight=1;
gbc.gridwidth=1;
gb.setConstraints(jbnButtons[7], gbc);
gbc.gridx=1;
gbc.gridy=3;
gbc.gridheight=1;
gbc.gridwidth=1;
gb.setConstraints(jbnButtons[8], gbc);
gbc.gridx=2;
gbc.gridy=3;
gbc.gridheight=1;
gbc.gridwidth=1;
gb.setConstraints(jbnButtons[9], gbc);
gbc.gridx=3;
gbc.gridy=3;
gbc.gridheight=1;
gbc.gridwidth=1;
gb.setConstraints(jbnButtons[13], gbc);
gbc.gridx=4;
gbc.gridy=3;
gbc.gridheight=1;
gbc.gridwidth=1;
gb.setConstraints(jbnButtons[17], gbc);
gbc.gridx=0;
gbc.gridy=4;
gbc.gridheight=1;
gbc.gridwidth=1;
gb.setConstraints(jbnButtons[4], gbc);
gbc.gridx=1;
gbc.gridy=4;
gbc.gridheight=1;
gbc.gridwidth=1;
gb.setConstraints(jbnButtons[5], gbc);
gbc.gridx=2;
gbc.gridy=4;
gbc.gridheight=1;
gbc.gridwidth=1;
gb.setConstraints(jbnButtons[6], gbc);
gbc.gridx=3;
gbc.gridy=4;
gbc.gridheight=1;
gbc.gridwidth=1;
gb.setConstraints(jbnButtons[14], gbc);
gbc.gridx=4;
gbc.gridy=4;
gbc.gridheight=1;
gbc.gridwidth=2;
gb.setConstraints(jbnButtons[18], gbc);
gbc.gridx=0;
gbc.gridy=5;
gbc.gridheight=1;
gbc.gridwidth=1;
gb.setConstraints(jbnButtons[1], gbc);
gbc.gridx=1;
gbc.gridy=5;
gbc.gridheight=1;
gbc.gridwidth=1;
gb.setConstraints(jbnButtons[2], gbc);
gbc.gridx=2;
gbc.gridy=5;
gbc.gridheight=1;
gbc.gridwidth=1;
gb.setConstraints(jbnButtons[3], gbc);
gbc.gridx=3;
gbc.gridy=5;
gbc.gridheight=1;
gbc.gridwidth=1;
gb.setConstraints(jbnButtons[15], gbc);
gbc.gridx=4;
gbc.gridy=5;
gbc.gridheight=1;
gbc.gridwidth=2;
gb.setConstraints(jbnButtons[19], gbc);
gbc.gridx=0;
gbc.gridy=6;
gbc.gridheight=1;
gbc.gridwidth=1;
gb.setConstraints(jbnButtons[0], gbc);
gbc.gridx=1;
gbc.gridy=6;
gbc.gridheight=1;
gbc.gridwidth=1;
gb.setConstraints(jbnButtons[10], gbc);
gbc.gridx=2;
gbc.gridy=6;
gbc.gridheight=1;
gbc.gridwidth=1;
gb.setConstraints(jbnButtons[11], gbc);
gbc.gridx=3;
gbc.gridy=6;
gbc.gridheight=1;
gbc.gridwidth=1;
gb.setConstraints(jbnButtons[16], gbc);
gbc.gridx=4;
gbc.gridy=6;
gbc.gridheight=1;
gbc.gridwidth=2;
gb.setConstraints(jbnButtons[12], gbc);
for(int i=0;i<=22;i++)
{jpn.add(jbnButtons[i]);
jbnButtons[i].addActionListener(this);
}
jpn.add(txtkq);
jpn.setLayout(gb);
this.add(jpn);
pack();
this.setVisible(true);
//clearAll();
}
public void actionPerformed(ActionEvent e)
{
double result=0;
for(int i=0;i<jbnButtons.length;i++)
{
if(e.getSource()==jbnButtons[i])
{
switch(i)
{
case 0: addDigitToDisplay(i);break;
case 1: addDigitToDisplay(i);break;
case 2: addDigitToDisplay(i);break;
case 3: addDigitToDisplay(i);break;
case 4: addDigitToDisplay(i);break;
case 5: addDigitToDisplay(i);break;
case 6: addDigitToDisplay(i);break;
case 7: addDigitToDisplay(i);break;
case 8: addDigitToDisplay(i);break;
case 9: addDigitToDisplay(i);break;
case 10: processSignChange();break;
case 11: addDecimalPoint();break;
case 12: processEquals();break;
case 13: processOperator("/");break;
case 14: processOperator("*");break;
case 15: processOperator("-");break;
case 16: processOperator("+");break;
case 17: if(displayMode!=ERROR_MODE)
{
try{
if(getDisplayString().indexOf("-")==0)
displayError("Khong hop le");
result=Math.sqrt(getNumberInDisplay());
displayResult(result);
}catch(Exception ex){
displayError("Khong hop le");
displayMode=ERROR_MODE;
}
}break;
case 18: if(displayMode!=ERROR_MODE){
try{
if(getNumberInDisplay()==0)
displayError("Khong hop le");
result=1/getNumberInDisplay();
displayResult(result);
}catch(Exception ex){
displayError("Loi chia 0");
displayMode=ERROR_MODE;
}
}break;
case 19: if(displayMode!=ERROR_MODE){
try{
result=getNumberInDisplay()/100;
displayResult(result);
}catch(Exception ex){
displayError("Khong hop le");
displayMode=ERROR_MODE;
}
}break;
case 20: if(displayMode!=ERROR_MODE){
setDisplayString(getDisplayString().substring(0, getDisplayString().length()-1));
if(getDisplayString().length()<1) setDisplayString("0");
}break;
case 21: clearExisting();break;
case 22: clearAll();break;
}
}
}
}
void setDisplayString(String s){
txtkq.setText(s);
}
String getDisplayString(){
return txtkq.getText();
}
void addDigitToDisplay(int digit){
if(ClearOnNextDigit && !lastoperator.equals("0")) setDisplayString("");
String inputString=getDisplayString();
if(inputString.indexOf("0")==0 && inputString.indexOf(".")<0) inputString=inputString.substring(1);
if((!inputString.equals("0")||digit>0)&&inputString.length()<MAX_INPUT_LENGTH)
setDisplayString(inputString+digit);
displayMode=INPUT_MODE;
ClearOnNextDigit=false;
}
void addDecimalPoint(){
displayMode=INPUT_MODE;
//if(ClearOnNextDigit) setDisplayString("");
String inputString =getDisplayString();
if(inputString.indexOf(".")<0) setDisplayString(new String(inputString+"."));
}
void processSignChange(){
if(displayMode==INPUT_MODE)
{
String input=getDisplayString();
if(input.length()>0 && !input.equals("0"))
{
if(input.indexOf("-")==0) setDisplayString(input.substring(1));
else setDisplayString("-"+input);
}
}else if(displayMode==RESULT_MODE)
{
double numberInDisplay=getNumberInDisplay();
if(numberInDisplay!=0) displayResult(-numberInDisplay);
}
}
void clearAll(){
setDisplayString("0");
lastoperator="0";
lastnumber=0;
displayMode=INPUT_MODE;
ClearOnNextDigit=true;
}
void clearExisting(){
setDisplayString("0");
ClearOnNextDigit=true;
displayMode=INPUT_MODE;
}
double getNumberInDisplay(){
String input=txtkq.getText();
return Double.parseDouble(input);
}
void processOperator(String op){
if(displayMode!=ERROR_MODE)
{
double numberInDisplay=getNumberInDisplay();
if(!lastoperator.equals("0"))
{
try{
double result=processLastOperator();
displayResult(result);
lastnumber=result;
}catch(Exception e){}
}else lastnumber=numberInDisplay;
ClearOnNextDigit=true;
lastoperator=op;
}
}
void processEquals(){
double result=0;
if(displayMode!=ERROR_MODE){
try{
result=processLastOperator();
displayResult(result);
}catch(Exception e){
displayError("Loi chia 0");
}lastoperator="0";
}
}
double processLastOperator() throws DivideByZeroException{
double result = 0;
double numberInDisplay = getNumberInDisplay();
if (lastoperator.equals("/"))
{
if (numberInDisplay == 0)
throw (new DivideByZeroException());
result = lastnumber / numberInDisplay;
}
if (lastoperator.equals("*"))
result = lastnumber * numberInDisplay;
if (lastoperator.equals("-"))
result = lastnumber - numberInDisplay;
if (lastoperator.equals("+"))
result = lastnumber + numberInDisplay;
if (lastoperator.equals("0"))
result=numberInDisplay;
return result;
}
void displayResult(double result){
setDisplayString(Double.toString(result));
lastnumber = result;
displayMode = RESULT_MODE;
ClearOnNextDigit=true;
}
void displayError(String ems){
setDisplayString(ems);
lastnumber = 0;
displayMode = ERROR_MODE;
ClearOnNextDigit = true;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Bai23("Calculator");
//Bai23 dt=new Bai23("Calculator");
}
}
@SuppressWarnings("serial")
class DivideByZeroException extends Exception{
public DivideByZeroException()
{
super();
}
public DivideByZeroException(String s)
{
super(s);
}
}
Đổi màu nền:
package baocao;
import java.awt.*;
import java.awt.event.*;
public class Bai24 extends Frame implements ActionListener {
Button gre,blu,red,exit;
public Bai24(String st)
{
super(st);
gre=new Button("Green");
blu=new Button("Blue");
red=new Button("Red");
exit=new Button("Exit");
add(gre);
add(blu);
add(red);
add(exit);
gre.addActionListener(this);
blu.addActionListener(this);
red.addActionListener(this);
exit.addActionListener(this);
this.setLayout(new FlowLayout(FlowLayout.CENTER));
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==exit) System.exit(0);
if(e.getSource()==gre) this.setBackground(Color.GREEN);
if(e.getSource()==blu) this.setBackground(Color.BLUE);
if(e.getSource()==red) this.setBackground(Color.RED);
}
public static void main(String arg[])
{
Bai24 tam =new Bai24("Doi Mau");
tam.setSize(300,200);
tam.setVisible(true);
}
}
Minh họa kiểu sự kiện ItemEvent
package baocao;
import java.awt.*;
import java.awt.event.*;
public class Bai25 extends Frame implements ActionListener,ItemListener {
Label lb=new Label("The item is display here");
CheckboxGroup cg=new CheckboxGroup();
Checkbox nam=new Checkbox("Male", cg, true);
Checkbox nu=new Checkbox("Female", cg, false);
List list1 =new List(3,false);
Choice ch1=new Choice();
Button exit=new Button("Exit");
Panel pn=new Panel();
GridBagLayout gb=new GridBagLayout();
GridBagConstraints gbs=new GridBagConstraints();
public Bai25(String st)
{
super(st);
list1.add("MSDOS");
list1.add("WINDOW");
list1.add("LINUX");
list1.add("UNIX");
ch1.add("Dog");
ch1.add("Cat");
ch1.add("chicken");
exit.addActionListener(this);
nam.addItemListener(this);
nu.addItemListener(this);
list1.addItemListener(this);
ch1.addItemListener(this);
gbs.insets=new Insets(8, 2, 8, 2);
gbs.fill=GridBagConstraints.BOTH;
gbs.gridx=1;
gbs.gridy=1;
gbs.gridwidth=2;
gbs.gridheight=1;
gb.setConstraints(nam, gbs);
gbs.gridx=4;
gbs.gridy=1;
gbs.gridwidth=2;
gbs.gridheight=1;
gb.setConstraints(nu, gbs);
gbs.fill=GridBagConstraints.NONE;
gbs.gridx=2;
gbs.gridy=2;
gbs.gridwidth=3;
gbs.gridheight=2;
gb.setConstraints(list1, gbs);
gbs.gridx=3;
gbs.gridy=4;
gbs.gridwidth=1;
gbs.gridheight=1;
gb.setConstraints(ch1, gbs);
gbs.gridx=1;
gbs.gridy=5;
gbs.gridwidth=4;
gbs.gridheight=1;
gb.setConstraints(lb, gbs);
gbs.gridx=5;
gbs.gridy=5;
gbs.gridwidth=1;
gbs.gridheight=1;
gb.setConstraints(exit, gbs);
pn.setLayout(gb);
pn.add(nam);
pn.add(nu);
pn.add(list1);
pn.add(ch1);
pn.add(lb);
pn.add(exit);
add(pn);
setSize(300, 300);
setVisible(true);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
} );
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==exit) System.exit(0);
}
public void itemStateChanged(ItemEvent ea)
{
if(ea.getStateChange()==ItemEvent.SELECTED)
{
if(ea.getSource()==list1) lb.setText(list1.getSelectedItem());
else lb.setText((String)ea.getItem());
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Bai25("Tong hop cac item");
}
}
LẬP TRÌNH APPLETS
Giải phương trình bậc nhất:
package baocao;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Bai26 extends Applet implements ActionListener{
Panel p,p1,p2,p3;
Label lTitle,lInputA,lInputB,lKq;
TextField tInputA,tInputB,tKq;
Button Tinh,Reset,Thoat;
public void init(){
p = new Panel();
p1 = new Panel();
p2 = new Panel();
p3 = new Panel();
lTitle = new Label("Giai phuong trinh bac nhat");
lInputA = new Label("Nhap a");
lInputB = new Label("Nhap b");
lKq = new Label("Ket qua");
tKq = new TextField();
tInputA = new TextField();
tInputB = new TextField();
Tinh = new Button("Tinh");
Reset = new Button("Reset");
Thoat = new Button("Thoat");
p.setLayout(new GridLayout(3,1));
p1.setLayout(new GridLayout(1,1));
p2.setLayout(new GridLayout(3,2));
p3.setLayout(new FlowLayout(FlowLayout.CENTER));
p1.add(lTitle);
p2.add(lInputA); p2.add(tInputA);
p2.add(lInputB); p2.add(tInputB);
p2.add(lKq); p2.add(tKq);
p3.add(Tinh);
p3.add(Reset);
p3.add(Thoat);
Thoat.addActionListener(this);
Tinh.addActionListener(this);
Reset.addActionListener(this);
p.add(p1);
p.add(p2);
p.add(p3);
this.add(p);
//this.setBounds(0,0,400,300);
this.setVisible(true);
}
public void actionPerformed(ActionEvent ae){
if(ae.getSource()==Thoat) System.exit(1);
if(ae.getSource()==Tinh){
double a,b;
a=Double.parseDouble(tInputA.getText());
b=Double.parseDouble(tInputB.getText());
if(a==0)
if(b==0) tKq.setText("pt co vo so nghiem");
else tKq.setText("Pt vo nghiem");
else tKq.setText(Double.toString(-b/a));
}
if(ae.getSource()==Reset){
tInputA.setText("");
tInputB.setText("");
tKq.setText("");
}
}
}
Minh họa các phép toán:
package baocao;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class Bai27 extends Applet implements ActionListener
{
Label lb,lb1,lb2,lb3;
TextField txta,txtb,txtkq;
Button cong,tru,nhan,chia,exit,reset;
Panel pn,pn1,pn2,pn3,pn4;
public void init()
{
lb=new Label("Minh hoa cac phep toan");
lb1=new Label("Nhap a");
lb2=new Label("Nhap b");
lb3=new Label("Ket qua");
txta=new TextField("");
txtb=new TextField("");
txtkq=new TextField("");
cong=new Button("Cong");
tru=new Button("Tru");
nhan=new Button("Nhan");
chia=new Button("Chia");
exit=new Button("Exit");
reset=new Button("Reset");
cong.addActionListener(this);
tru.addActionListener(this);
nhan.addActionListener(this);
chia.addActionListener(this);
exit.addActionListener(this);
reset.addActionListener(this);
pn=new Panel(new GridLayout(4,1));
pn1=new Panel(new GridLayout(1,1));
pn2=new Panel(new GridLayout(3,2));
pn3=new Panel(new FlowLayout(FlowLayout.CENTER));
pn4=new Panel(new FlowLayout(FlowLayout.CENTER));
pn1.add(lb);
pn2.add(lb1);
pn2.add(txta);
pn2.add(lb2);
pn2.add(txtb);
pn2.add(lb3);
pn2.add(txtkq);
pn3.add(cong);
pn3.add(tru);
pn3.add(nhan);
pn3.add(chia);
pn4.add(exit);
pn4.add(reset);
pn.add(pn1);
pn.add(pn2);
pn.add(pn3);
pn.add(pn4);
add(pn);
setSize(300,300);
//this.setBounds(200,200,400,300);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==reset)
{
txta.setText("");
txtb.setText("");
txtkq.setText("");
}
else if(e.getSource()==exit) System.exit(0);
else
{
double a=Double.parseDouble(txta.getText());
double b=Double.parseDouble(txtb.getText());
if (e.getSource()==cong) txtkq.setText(Double.toString(a+b));
if (e.getSource()==tru) txtkq.setText(Double.toString(a-b));
if (e.getSource()==nhan) txtkq.setText(Double.toString(a*b));
if (e.getSource()==chia) txtkq.setText(Double.toString(a/b));
}
}
}
In các số nguyên tố nhỏ hơn hoặc bằng số n cho trước:
package baocao;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class Bai28 extends Applet implements ActionListener
{
Label lb,lb1,lb2;
TextField txtnhap,txtkq;
Button tim,reset,exit;
Panel pn,pn1,pn2,pn3;
public void init(){
lb=new Label("Cac so nguyen to <=n ");
lb1=new Label("Nhap n:");
lb2=new Label("KQ:");
txtnhap=new TextField();
txtkq=new TextField();
txtkq.setEditable(false);
tim=new Button("Tim");
exit=new Button("Exit");
reset=new Button("Reset");
tim.addActionListener(this);
exit.addActionListener(this);
reset.addActionListener(this);
pn=new Panel(new GridLayout(3,1));
pn1=new Panel(new GridLayout(1,1));
pn2=new Panel(new GridLayout(2,2));
pn3=new Panel(new FlowLayout(FlowLayout.CENTER));
pn1.add(lb);
pn2.add(lb1);
pn2.add(txtnhap);
pn2.add(lb2);
pn2.add(txtkq);
pn3.add(tim);
pn3.add(reset);
pn3.add(exit);
pn.add(pn1);
pn.add(pn2);
pn.add(pn3);
add(pn);
this.setSize(300,200);
this.setVisible(true);
}
public boolean Test(int t)
{
if(t==0||t==1) return false;
else for(int i=2;i<=(t/2);i++) if((t%i)==0) return false ;
return true;
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==reset)
{
txtnhap.setText("");
txtkq.setText("");
}
if(e.getSource()==exit) System.exit(0);
if(e.getSource()==tim)
{
int n=Integer.parseInt(txtnhap.getText());
String chuoi1="";
if(n>=2)for(int i=2;i<=n;i++) if(Test(i)) chuoi1+=" "+i;
txtkq.setText(chuoi1);
}
}
}
Kiểm tra một số có thuộc dãy Fibonaci không?
package baocao;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class Bai29 extends Applet implements ActionListener {
Label lb,lb1,lb2;
TextField txtnhap,txtkq;
Button ok,reset,exit;
Panel pn=new Panel();
GridBagLayout gb=new GridBagLayout();
GridBagConstraints gbs=new GridBagConstraints();
public void init()
{
lb=new Label("Kiem tra a co thuoc day Fibonaci");
lb1=new Label("Nhap a:");
lb2=new Label("KQ: ");
txtnhap=new TextField(20);
txtkq=new TextField(20);
ok=new Button("OK");
reset=new Button("Reset");
exit=new Button("Exit");
ok.addActionListener(this);
reset.addActionListener(this);
exit.addActionListener(this);
gbs.insets=new Insets(2, 2, 2, 2);
gbs.fill=GridBagConstraints.BOTH;
gbs.gridx=1;
gbs.gridy=1;
gbs.gridwidth=6;
gbs.gridheight=1;
gb.setConstraints(lb, gbs);
gbs.gridx=1;
gbs.gridy=2;
gbs.gridwidth=2;
gbs.gridheight=1;
gb.setConstraints(lb1, gbs);
gbs.gridx=3;
gbs.gridy=2;
gbs.gridwidth=4;
gbs.gridheight=1;
//gbs.ipadx=4;
gb.setConstraints(txtnhap, gbs);
gbs.gridx=1;
gbs.gridy=3;
gbs.gridwidth=2;
gbs.gridheight=1;
gb.setConstraints(lb2, gbs);
gbs.gridx=3;
gbs.gridy=3;
gbs.gridwidth=4;
gbs.gridheight=1;
gb.setConstraints(txtkq, gbs);
gbs.gridx=1;
gbs.gridy=4;
gbs.gridwidth=2;
gbs.gridheight=1;
//gbs.weightx=1;
gb.setConstraints(ok, gbs);
gbs.gridx=3;
gbs.gridy=4;
gbs.gridwidth=2;
gbs.gridheight=1;
//gbs.weightx=1;
gb.setConstraints(reset, gbs);
gbs.gridx=5;
gbs.gridy=4;
gbs.gridwidth=2;
gbs.gridheight=1;
//gbs.weightx=1;
gb.setConstraints(exit, gbs);
pn.add(lb);
pn.add(lb1);
pn.add(txtnhap);
pn.add(lb2);
pn.add(txtkq);
pn.add(ok);
pn.add(reset);
pn.add(exit);
pn.setLayout(gb);
this.add(pn);
this.setSize(400, 200);
setVisible(true);
}
public boolean fibo(int n)
{
int x=0,y=1,z=0;
if(n!=0)
{while(z<n)
{
x=y;
y=z;
z=x+y;
}
if(z==n) return true;
}
return false;
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==exit) System.exit(0);
else if(e.getSource()==reset) {
txtnhap.setText("");
txtkq.setText("");
}
else {
int a=Integer.parseInt(txtnhap.getText());
if(fibo(a)) txtkq.setText(a+" la so FIBONACI");
else txtkq.setText(a+" khong la so FIBONACI");
}
}
}
Mô tả máy tính điện tử cá nhân:
package baocao;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Bai30 extends Applet implements ActionListener{
GridBagLayout gb=new GridBagLayout();
GridBagConstraints gbc=new GridBagConstraints();
final int MAX_INPUT_LENGTH=20;
final int INPUT_MODE=0;
final int RESULT_MODE=1;
final int ERROR_MODE=2;
int displayMode=INPUT_MODE;
boolean ClearOnNextDigit,percent;
double lastnumber=0;
String lastoperator="0";
private JTextField txtkq=new JTextField("0",20);
private JButton jbnButtons[];
JPanel jpn=new JPanel();
public void init()
{
jbnButtons=new JButton[23];
gbc.insets=new Insets(3, 3, 3, 3);
gbc.fill=GridBagConstraints.BOTH;
for(int i=0;i<=9;i++) jbnButtons[i]= new JButton(String.valueOf(i));
jbnButtons[10]=new JButton("+/-");
jbnButtons[11]=new JButton(".");
jbnButtons[12]=new JButton("=");
jbnButtons[13]=new JButton("/");
jbnButtons[14]=new JButton("*");
jbnButtons[15]=new JButton("-");
jbnButtons[16]=new JButton("+");
jbnButtons[17]=new JButton("sqrt");
jbnButtons[18]=new JButton("1/x");
jbnButtons[19]=new JButton("%");
jbnButtons[20]=new JButton("Backspace");
jbnButtons[21]=new JButton("CE");
jbnButtons[22]=new JButton("C");
gbc.gridx=0;
gbc.gridy=0;
gbc.gridheight=2;
gbc.gridwidth=5;
gb.setConstraints(txtkq, gbc);
gbc.gridx=0;
gbc.gridy=2;
gbc.gridheight=1;
gbc.gridwidth=2;
gb.setConstraints(jbnButtons[20], gbc);
gbc.gridx=2;
gbc.gridy=2;
gbc.gridheight=1;
gbc.gridwidth=2;
gb.setConstraints(jbnButtons[21], gbc);
gbc.gridx=4;
gbc.gridy=2;
gbc.gridheight=1;
gbc.gridwidth=1;
gb.setConstraints(jbnButtons[22], gbc);
gbc.gridx=0;
gbc.gridy=3;
gbc.gridheight=1;
gbc.gridwidth=1;
gb.setConstraints(jbnButtons[7], gbc);
gbc.gridx=1;
gbc.gridy=3;
gbc.gridheight=1;
gbc.gridwidth=1;
gb.setConstraints(jbnButtons[8], gbc);
gbc.gridx=2;
gbc.gridy=3;
gbc.gridheight=1;
gbc.gridwidth=1;
gb.setConstraints(jbnButtons[9], gbc);
gbc.gridx=3;
gbc.gridy=3;
gbc.gridheight=1;
gbc.gridwidth=1;
gb.setConstraints(jbnButtons[13], gbc);
gbc.gridx=4;
gbc.gridy=3;
gbc.gridheight=1;
gbc.gridwidth=1;
gb.setConstraints(jbnButtons[17], gbc);
gbc.gridx=0;
gbc.gridy=4;
gbc.gridheight=1;
gbc.gridwidth=1;
gb.setConstraints(jbnButtons[4], gbc);
gbc.gridx=1;
gbc.gridy=4;
gbc.gridheight=1;
gbc.gridwidth=1;
gb.setConstraints(jbnButtons[5], gbc);
gbc.gridx=2;
gbc.gridy=4;
gbc.gridheight=1;
gbc.gridwidth=1;
gb.setConstraints(jbnButtons[6], gbc);
gbc.gridx=3;
gbc.gridy=4;
gbc.gridheight=1;
gbc.gridwidth=1;
gb.setConstraints(jbnButtons[14], gbc);
gbc.gridx=4;
gbc.gridy=4;
gbc.gridheight=1;
gbc.gridwidth=2;
gb.setConstraints(jbnButtons[18], gbc);
gbc.gridx=0;
gbc.gridy=5;
gbc.gridheight=1;
gbc.gridwidth=1;
gb.setConstraints(jbnButtons[1], gbc);
gbc.gridx=1;
gbc.gridy=5;
gbc.gridheight=1;
gbc.gridwidth=1;
gb.setConstraints(jbnButtons[2], gbc);
gbc.gridx=2;
gbc.gridy=5;
gbc.gridheight=1;
gbc.gridwidth=1;
gb.setConstraints(jbnButtons[3], gbc);
gbc.gridx=3;
gbc.gridy=5;
gbc.gridheight=1;
gbc.gridwidth=1;
gb.setConstraints(jbnButtons[15], gbc);
gbc.gridx=4;
gbc.gridy=5;
gbc.gridheight=1;
gbc.gridwidth=2;
gb.setConstraints(jbnButtons[19], gbc);
gbc.gridx=0;
gbc.gridy=6;
gbc.gridheight=1;
gbc.gridwidth=1;
gb.setConstraints(jbnButtons[0], gbc);
gbc.gridx=1;
gbc.gridy=6;
gbc.gridheight=1;
gbc.gridwidth=1;
gb.setConstraints(jbnButtons[10], gbc);
gbc.gridx=2;
gbc.gridy=6;
gbc.gridheight=1;
gbc.gridwidth=1;
gb.setConstraints(jbnButtons[11], gbc);
gbc.gridx=3;
gbc.gridy=6;
gbc.gridheight=1;
gbc.gridwidth=1;
gb.setConstraints(jbnButtons[16], gbc);
gbc.gridx=4;
gbc.gridy=6;
gbc.gridheight=1;
gbc.gridwidth=2;
gb.setConstraints(jbnButtons[12], gbc);
for(int i=0;i<=22;i++)
{jpn.add(jbnButtons[i]);
jbnButtons[i].addActionListener(this);
}
jpn.add(txtkq);
jpn.setLayout(gb);
this.add(jpn);
this.setSize(300, 200);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
double result=0;
for(int i=0;i<jbnButtons.length;i++)
{
if(e.getSource()==jbnButtons[i])
{
switch(i)
{
case 0: addDigitToDisplay(i);break;
case 1: addDigitToDisplay(i);break;
case 2: addDigitToDisplay(i);break;
case 3: addDigitToDisplay(i);break;
case 4: addDigitToDisplay(i);break;
case 5: addDigitToDisplay(i);break;
case 6: addDigitToDisplay(i);break;
case 7: addDigitToDisplay(i);break;
case 8: addDigitToDisplay(i);break;
case 9: addDigitToDisplay(i);break;
case 10: processSignChange();break;
case 11: addDecimalPoint();break;
case 12: processEquals();break;
case 13: processOperator("/");break;
case 14: processOperator("*");break;
case 15: processOperator("-");break;
case 16: processOperator("+");break;
case 17: if(displayMode!=ERROR_MODE)
{
try{
if(getDisplayString().indexOf("-")==0)
displayError("Khong hop le");
result=Math.sqrt(getNumberInDisplay());
displayResult(result);
}catch(Exception ex){
displayError("Khong hop le");
displayMode=ERROR_MODE;
}
}break;
case 18: if(displayMode!=ERROR_MODE){
try{
if(getNumberInDisplay()==0)
displayError("Khong hop le");
result=1/getNumberInDisplay();
displayResult(result);
}catch(Exception ex){
displayError("Loi chia 0");
displayMode=ERROR_MODE;
}
}break;
case 19: if(displayMode!=ERROR_MODE){
try{
result=getNumberInDisplay()/100;
displayResult(result);
}catch(Exception ex){
displayError("Khong hop le");
displayMode=ERROR_MODE;
}
}break;
case 20: if(displayMode!=ERROR_MODE){
setDisplayString(getDisplayString().substring(0, getDisplayString().length()-1));
if(getDisplayString().length()<1) setDisplayString("0");
}break;
case 21: clearExisting();break;
case 22: clearAll();break;
}
}
}
}
void setDisplayString(String s){
txtkq.setText(s);
}
String getDisplayString(){
return txtkq.getText();
}
void addDigitToDisplay(int digit){
if(ClearOnNextDigit && !lastoperator.equals("0")) setDisplayString("");
String inputString=getDisplayString();
if(inputString.indexOf("0")==0 && inputString.indexOf(".")<0) inputString=inputString.substring(1);
if((!inputString.equals("0")||digit>0)&&inputString.length()<MAX_INPUT_LENGTH)
setDisplayString(inputString+digit);
displayMode=INPUT_MODE;
ClearOnNextDigit=false;
}
void addDecimalPoint(){
displayMode=INPUT_MODE;
//if(ClearOnNextDigit) setDisplayString("");
String inputString =getDisplayString();
if(inputString.indexOf(".")<0) setDisplayString(new String(inputString+"."));
}
void processSignChange(){
if(displayMode==INPUT_MODE)
{
String input=getDisplayString();
if(input.length()>0 && !input.equals("0"))
{
if(input.indexOf("-")==0) setDisplayString(input.substring(1));
else setDisplayString("-"+input);
}
}else if(displayMode==RESULT_MODE)
{
double numberInDisplay=getNumberInDisplay();
if(numberInDisplay!=0) displayResult(-numberInDisplay);
}
}
void clearAll(){
setDisplayString("0");
lastoperator="0";
lastnumber=0;
displayMode=INPUT_MODE;
ClearOnNextDigit=true;
}
void clearExisting(){
setDisplayString("0");
ClearOnNextDigit=true;
displayMode=INPUT_MODE;
}
double getNumberInDisplay(){
String input=txtkq.getText();
return Double.parseDouble(input);
}
void processOperator(String op){
if(displayMode!=ERROR_MODE)
{
double numberInDisplay=getNumberInDisplay();
if(!lastoperator.equals("0"))
{
try{
double result=processLastOperator();
displayResult(result);
lastnumber=result;
}catch(Exception e){}
}else lastnumber=numberInDisplay;
ClearOnNextDigit=true;
lastoperator=op;
}
}
void processEquals(){
double result=0;
if(displayMode!=ERROR_MODE){
try{
result=processLastOperator();
displayResult(result);
}catch(Exception e){
displayError("Loi chia 0");
}lastoperator="0";
}
}
double processLastOperator() throws Exception{
double result = 0;
double numberInDisplay = getNumberInDisplay();
if (lastoperator.equals("/"))
{
if (numberInDisplay == 0)
throw (new Exception());
result = lastnumber / numberInDisplay;
}
if (lastoperator.equals("*"))
result = lastnumber * numberInDisplay;
if (lastoperator.equals("-"))
result = lastnumber - numberInDisplay;
if (lastoperator.equals("+"))
result = lastnumber + numberInDisplay;
if (lastoperator.equals("0"))
result=numberInDisplay;
return result;
}
void displayResult(double result){
setDisplayString(Double.toString(result));
lastnumber = result;
displayMode = RESULT_MODE;
ClearOnNextDigit=true;
}
void displayError(String ems){
setDisplayString(ems);
lastnumber = 0;
displayMode = ERROR_MODE;
ClearOnNextDigit = true;
}
}
Đổi màu nền
package baocao;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Bai31 extends Applet implements ActionListener {
Button gre,blu,red,exit;
public void init()
{
gre=new Button("Green");
blu=new Button("Blue");
red=new Button("Red");
exit=new Button("Exit");
add(gre);
add(blu);
add(red);
add(exit);
gre.addActionListener(this);
blu.addActionListener(this);
red.addActionListener(this);
exit.addActionListener(this);
this.setLayout(new FlowLayout(FlowLayout.CENTER));
this.setSize(300,200);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==exit) System.exit(0);
if(e.getSource()==gre) this.setBackground(Color.GREEN);
if(e.getSource()==blu) this.setBackground(Color.BLUE);
if(e.getSource()==red) this.setBackground(Color.RED);
}
}
Minh họa kiểu sự kiện ItemEvent
package baocao;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Bai32 extends Applet implements ActionListener,ItemListener {
Label lb=new Label("The item is display here");
CheckboxGroup cg=new CheckboxGroup();
Checkbox nam=new Checkbox("Male", cg, true);
Checkbox nu=new Checkbox("Female", cg, false);
List list1 =new List(3,false);
Choice ch1=new Choice();
Button exit=new Button("Exit");
Panel pn=new Panel();
GridBagLayout gb=new GridBagLayout();
GridBagConstraints gbs=new GridBagConstraints();
public void init()
{
list1.add("MSDOS");
list1.add("WINDOW");
list1.add("LINUX");
list1.add("UNIX");
ch1.add("Dog");
ch1.add("Cat");
ch1.add("chicken");
exit.addActionListener(this);
nam.addItemListener(this);
nu.addItemListener(this);
list1.addItemListener(this);
ch1.addItemListener(this);
gbs.insets=new Insets(8, 2, 8, 2);
gbs.fill=GridBagConstraints.BOTH;
gbs.gridx=1;
gbs.gridy=1;
gbs.gridwidth=2;
gbs.gridheight=1;
gb.setConstraints(nam, gbs);
gbs.gridx=4;
gbs.gridy=1;
gbs.gridwidth=2;
gbs.gridheight=1;
gb.setConstraints(nu, gbs);
gbs.fill=GridBagConstraints.NONE;
gbs.gridx=2;
gbs.gridy=2;
gbs.gridwidth=3;
gbs.gridheight=2;
gb.setConstraints(list1, gbs);
gbs.gridx=3;
gbs.gridy=4;
gbs.gridwidth=1;
gbs.gridheight=1;
gb.setConstraints(ch1, gbs);
gbs.gridx=1;
gbs.gridy=5;
gbs.gridwidth=4;
gbs.gridheight=1;
gb.setConstraints(lb, gbs);
gbs.gridx=5;
gbs.gridy=5;
gbs.gridwidth=1;
gbs.gridheight=1;
gb.setConstraints(exit, gbs);
pn.setLayout(gb);
pn.add(nam);
pn.add(nu);
pn.add(list1);
pn.add(ch1);
pn.add(lb);
pn.add(exit);
add(pn);
setSize(300, 200);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==exit) System.exit(0);
}
public void itemStateChanged(ItemEvent ea)
{
if(ea.getStateChange()==ItemEvent.SELECTED)
{
if(ea.getSource()==list1) lb.setText(list1.getSelectedItem());
else lb.setText((String)ea.getItem());
}
}
}
Các file đính kèm theo tài liệu này:
- 112665748_bai_thuc_hanh_ngon_ngu_lap_trinh_java_3109.docx