import java.applet.Applet; import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; public class GrKlient1 extends Applet implements ActionListener, Runnable{ TextField tf1=new TextField(15); //suurus Button nupp1=new Button("Ühenda"); TextArea ta1=new TextArea(5, 20); Panel p1=new Panel(new GridLayout(2, 1)); //2 rida ja 1 veerg tabelis Label silt1=new Label(); PrintWriter pw1; BufferedReader br1; String seisund="algus"; public GrKlient1(){ setLayout(new BorderLayout()); p1.add(tf1); p1.add(silt1); add(p1, BorderLayout.SOUTH); add(nupp1, BorderLayout.NORTH); add(ta1, BorderLayout.CENTER); tf1.addActionListener(this); nupp1.addActionListener(this); } public void actionPerformed(ActionEvent e){ if(e.getSource()==tf1){ pw1.println(tf1.getText()); tf1.setText(""); if(seisund.equals("nimesisestus")){ silt1.setText("Palun parool"); tf1.setEchoChar('*'); seisund="paroolisisestus"; } } if(e.getSource()==nupp1){ try{ Socket sc=new Socket("don2", 3001); pw1=new PrintWriter(sc.getOutputStream(), true); br1=new BufferedReader(new InputStreamReader(sc.getInputStream())); new Thread(this).start(); silt1.setText("Palun nimi: "); seisund="nimesisestus"; }catch(Exception viga){ ta1.setText(viga.getMessage()); } } } public void run(){ try{ while(true){ ta1.append(br1.readLine()+"\n"); } }catch(Exception viga){ viga.printStackTrace(); ta1.append("Oled väljas"); } } public static void main(String[] argumendid){ Frame f=new Frame(); f.add(new GrKlient1()); f.setSize(300, 300); f.setVisible(true); f.addWindowListener(new Raamikuular()); } static class Raamikuular extends WindowAdapter{ public void windowClosing(WindowEvent e){ System.out.println("Programmi ots"); System.exit(0); } } }