BAITAP
BAITAP
import java.awt.*; import java.awt.event.*; class Chkbox extends Frame { Checkbox chkhothuong,chkkinhdoanh; Button btnOk; TextField txtsokwcu,txtsokwmoi,txttiendien; public Chkbox() { setTitle("Tien Dien"); setSize(600,400); add(new Label("So kw cu"));add(txtsokwcu=new TextField(5)); add(new Label("So kw moi:"));add(txtsokwmoi=new TextField(5)); //tao group co 3 radiobutton CheckboxGroup grp=new CheckboxGroup(); add(chkhothuong=new Checkbox("Ho Thuong",grp,true)); add(chkkinhdoanh=new Checkbox("Kinh Doanh",grp,false)); //nut Ok, nut Cancel va nut Exit add(btnOk=new Button("OK")); add(new Label("tien dien"));add(txttiendien=new TextField(5)); //====================================== ================ setLayout(new FlowLayout(FlowLayout.CENTER,10,10)); setVisible(true); //-----------------------------------------------btnOk.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ String s=""; //Xu ly cac Radiobutton double sokwcu=Double.parseDouble(txtsokwcu.getText()); double sokwmoi=Double.parseDouble(txtsokwmoi.getText()); if(chkhothuong.getState()==true) s=""+((sokwmoisokwcu)*200); else //(chkkinhdoanh.getState()==true) s=""+((sokwmoi-sokwcu)*200*1.6); txttiendien.setText(s); }}); //------------------------------------------------
addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { dispose(); System.exit(0); } }); } public static void main(String args[]) { new Chkbox(); } }
MAY TINH
import java.awt.*; import java.awt.event.*; class Frame1 extends Frame { TextField txta,txtb,txtkq; Button btncong,btntru,btnnhan,btnchia; public Frame1() { setSize(400, 400); setTitle("May Tinh"); add(new Label("a:"));add(txta=new TextField(5)); add(new Label("b:"));add(txtb=new TextField(5)); add(btncong=new Button("+")); add(btntru=new Button("-")); add(btnnhan=new Button("*")); add(btnchia=new Button("/")); add(new Label("Ket Qua:"));add(txtkq=new TextField(15)); setLayout(new FlowLayout(FlowLayout.CENTER,10,10)); setVisible(true); btncong.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { double a=Double.parseDouble(txta.getText()); double b=Double.parseDouble(txtb.getText()); txtkq.setText(""+(a+b)); }}); btntru.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { double a=Double.parseDouble(txta.getText()); double b=Double.parseDouble(txtb.getText()); txtkq.setText(""+(a-b)); }});
btnnhan.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { double a=Double.parseDouble(txta.getText()); double b=Double.parseDouble(txtb.getText()); txtkq.setText(""+(a*b)); }}); btnchia.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { double a=Double.parseDouble(txta.getText()); double b=Double.parseDouble(txtb.getText()); txtkq.setText(""+(a/b)); }}); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { dispose(); System.exit(0); } }); } public static void main(String args[]) { Frame1 mainFrame = new Frame1(); } void Giai(){ String st=""; double a=Double.parseDouble(txta.getText()); double b=Double.parseDouble(txtb.getText()); txtkq.setText(st); } } SEVER 3 import java.net.*; import java.io.*; public class Server3 { public static void main(String args[]){ try { ServerSocket server = new ServerSocket(456);
int localPort = server.getLocalPort(); System.out.println("Reverse Server is listening on port "+localPort+"."); Socket client = server.accept(); BufferedReader in=new BufferedReader(new InputStreamReader(client.getInputStream())); DataOutputStream out=new DataOutputStream(client.getOutputStream()); boolean finished = false; BufferedReader kb1=new BufferedReader(new InputStreamReader(System.in)); int sum=0; do { String inLine = in.readLine(); System.out.println("Received_Client: "+inLine); System.out.print("Server: "); System.out.flush(); String message1 = kb1.readLine(); if(inLine.equals("q")) finished=true; out.writeBytes(message1); out.write(13); out.write(10); out.flush(); } while(!finished); in.close(); out.close(); client.close(); server.close(); } catch (IOException e){ System.out.println(e); }
//("IOException occurred.");
} } CLIENT 3 import java.net.*; import java.io.*; import java.awt.event.*; import java.awt.*; public class Client3 extends Frame{ TextArea Dialog1; TextField Txt1; boolean finished=false; Button btnSend; DataOutputStream out; public Client3() { setSize(400, 400); setTitle("Chuong trinh chat"); add(new Label("Dialog:"));add(Dialog1=new TextArea(10,50)); add(new Label("Client:"));add(Txt1=new TextField(50)); add(btnSend=new Button("Send")); setLayout(new FlowLayout(FlowLayout.CENTER,10,10)); setVisible(true); btnSend.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { Send(); }}); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { dispose(); System.exit(0); } }); try{ //1 Socket connection=new Socket("localhost",456); BufferedReader in=new BufferedReader( new InputStreamReader(connection.getInputStream())); out=new DataOutputStream(connection.getOutputStream()); BufferedReader kb=new BufferedReader(new InputStreamReader(System.in)); do{ try{ //2 //System.out.print("\n"); //System.out.print("Client: "); //System.out.flush();
//String message = kb.readLine(); //if (message.equals("q")) finished=true; //out.writeBytes(message); //out.write(13); out.write(10); out.flush(); //System.out.print("Receive_Server "+in.readLine()); Dialog1.setText(Dialog1.getText() +"\nServer:"+in.readLine()) ; } //try 2 catch(Exception e){ System.out.println(e); } }while(!finished); connection.close(); } // try 1 catch(Exception e){ System.out.println(e); }} public static void main(String args[]){ Client3 Thuy=new Client3(); } //main void Send(){ String message=Txt1.getText(); if (message.equals("q")) finished=true; try{ Dialog1.setText(Dialog1.getText()+"\nClient:"+message) ; out.writeBytes(message); out.write(13); out.write(10); out.flush(); out.writeBytes(message); out.write(13); out.write(10); out.flush(); Txt1.setText(""); } catch(Exception e){ System.out.println(e); } } } //class