// Klient import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; import java.util.*; import javax.swing.*; class Klient implements ActionListener{ //Globaalsed muutujad boolean veel=true; JTextArea ala=new JTextArea(); JScrollPane kerimine=new JScrollPane(ala); JTextField TF_nimi=new JTextField(10); JTextField TF_aadress=new JTextField("localhost",10); JButton B_yhenda=new JButton("Ühenda!"); BufferedReader sisend; PrintWriter väljund; Socket sc; public void actionPerformed(ActionEvent e){ if((e.getActionCommand()=="ühenda")&&(TF_nimi.getText().length()!=0)){ B_yhenda.setEnabled(false); TF_aadress.setEditable(false); try{ sc=new Socket(TF_aadress.getText(), 22222); }catch(Exception ex){System.out.println("Ilmnes viga: "+ex);} if(sc.isConnected()){ try{ väljund=new PrintWriter(sc.getOutputStream(), true); }catch(Exception exc){System.out.println("Jama: "+exc);} väljund.println(TF_nimi.getText()); System.out.println("Nimi saadeti ära."); new Lõim(); } }else{ ala.append("Kirjuta oma nimi tekstialasse!\n"); } } public Container createContentPane(){ JPanel content=new JPanel(new BorderLayout()); ala.setEditable(false); ala.setLineWrap(true); JPanel alumine=new JPanel(); JLabel L_nimi=new JLabel("Nimi "); JLabel L_aadress=new JLabel("Aadress "); B_yhenda.addActionListener(this); B_yhenda.setActionCommand("ühenda"); kerimine.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); kerimine.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); alumine.add(L_nimi); alumine.add(TF_nimi); alumine.add(B_yhenda); alumine.add(L_aadress); alumine.add(TF_aadress); content.add(kerimine); content.add(alumine, BorderLayout.SOUTH); return content; } public static void main(String args[]){ JFrame.setDefaultLookAndFeelDecorated(true); JFrame aken=new JFrame("Klient"); aken.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Klient k=new Klient(); aken.setContentPane(k.createContentPane()); aken.setSize(480,320); aken.setLocation(100,100); aken.show(); } class Lõim extends Thread{ public Lõim(){ new Thread(this).start(); } public void run(){ System.out.println("Lõim läks tööle."); String rida=""; Font f=new Font("Courier",Font.BOLD, 14); Font tavaline=new Font("Courier",Font.PLAIN,14); try{ sisend=new BufferedReader(new InputStreamReader(sc.getInputStream())); }catch(Exception e){System.out.println("Viga sissetulevas andmevoos "+e);} System.out.println("Sisend peaks korras olema"); while(veel){ try{ System.out.println("While-tsükkel"); // if(!sc.isClosed()){rida=sisend.readLine()+"\n";} rida=sisend.readLine()+"\n"; System.out.println("Muutuja rida väärtus - "+rida); StringTokenizer st=new StringTokenizer(rida,";"); while(st.hasMoreTokens()){ System.out.println("String Tokenizeri while-tsükkel"); String kasNimi=st.nextToken(); if(kasNimi.startsWith("#")){ ala.append("\n"); ala.setFont(f); ala.append(kasNimi.substring(1,kasNimi.length())+": "); }else{ ala.setFont(tavaline); ala.append(kasNimi+" "); } } }catch(Exception e){System.out.println("Viga: "+e);} } } } }