package vork; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.net.*; import java.io.*; public class Klient1 extends JPanel implements ActionListener, Runnable{ JTextField tf1=new JTextField(); JTextArea ta1=new JTextArea(); JScrollPane sp; PrintWriter valja; BufferedReader sisse; Socket sc; public Klient1(){ setLayout(new BorderLayout()); add(tf1, BorderLayout.SOUTH); add(sp=new JScrollPane(ta1), BorderLayout.CENTER); ta1.setEditable(false); try{ sc=new Socket("localhost", 3001); valja=new PrintWriter(sc.getOutputStream(), true); sisse=new BufferedReader(new InputStreamReader( sc.getInputStream())); }catch (Exception ex){ex.printStackTrace();} tf1.addActionListener(this); new Thread(this).start(); } public void run(){ try{ String rida=sisse.readLine(); while(rida!=null){ ta1.append(rida+"\n"); rida=sisse.readLine(); sp.getVerticalScrollBar().setValue(sp.getVerticalScrollBar().getMaximum()); } } catch(Exception e){ ta1.append(e.getMessage()); } } public void actionPerformed(ActionEvent e){ valja.println(tf1.getText()); tf1.setText(""); } public static void main(String[] arg){ JFrame f=new JFrame(); Klient1 k; f.getContentPane().add(k=new Klient1()); f.setSize(500, 400); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.requestFocus(); k.tf1.requestFocus(); } }