|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Senior Member
Iscritto dal: Jun 2002
Città: Milano
Messaggi: 959
|
Socket,java e JTextArea...
sto imparando a usare i socket con java e vorrei poter stampare in una JTextArea i comandi che mi invia il server però ogni volta che il server mi invia un comando mi si riapre una JTextArea mentre io vorrei che mi rimanesse aperta sempre la stessa e che i messaggi si accodassero uno dietro l'altro...ecco il codice del mio programma
Codice:
import java.io.*; import java.net.*; import java.awt.*; import javax.swing.*; import java.util.*; public class irc { public static void main(String[] args) { String pong="",pon="",comandi="",prova=""; char po[]; po = new char[9]; Socket smtpSocket = null; PrintStream os = null; DataInputStream is = null; JTextArea outputarea = new JTextArea(30,30); try { smtpSocket = new Socket("irc.tin.it", 6667); os = new PrintStream(smtpSocket.getOutputStream()); is = new DataInputStream(smtpSocket.getInputStream()); } catch (UnknownHostException e) { System.err.println("Don't know about host: hostname"); } catch (IOException e) { System.err.println("Couldn't get I/O for the connection to: hostname"); } if (smtpSocket != null && os != null && is != null) { try { os.println("user user hostname ip :user"); os.println("nick guest1"); pong = is.readLine(); pong.getChars(5,13,po,0); for (int i=0;i<9;i++) pon += po[i]; System.out.println("pong="+pon); System.out.println(pong); os.println("pong "+pon); String responseLine; while ((responseLine = is.readLine()) != null) { System.out.println("Server: " + responseLine); prova += responseLine; outputarea.append(responseLine); JOptionPane.showMessageDialog(null,outputarea,"messaggio", JOptionPane.INFORMATION_MESSAGE); if (responseLine.indexOf("Ok") != -1) { break; } } os.close(); is.close(); smtpSocket.close(); } catch (UnknownHostException e) { System.err.println("Trying to connect to unknown host: " + e); } catch (IOException e) { System.err.println("IOException: " + e); } } } } ![]() Ultima modifica di SteR9 : 23-08-2003 alle 19:47. |
![]() |
![]() |
![]() |
#2 |
Bannato
Iscritto dal: Nov 2001
Città: Verona
Messaggi: 1086
|
E' sufficiente eliminare l'uso di JOptionPane e sostituirlo con una finestra, sempre visibile, a cui connetti la JTextArea:
Ad esempio: Codice:
public static void main(String[] a) { JFrame frame=new JFrame("Titolo"); frame.getContentPane().add(outputarea); //la riga seguente è una funzione standard da sostituire //con una chiusura dell'applicazione più consona frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(500,500); frame.show(); ecc. ecc. Codice:
JOptionPane.showMessageDialog(null,outputarea,"messaggio", JOptionPane.INFORMATION_MESSAGE); |
![]() |
![]() |
![]() |
#3 | |
Senior Member
Iscritto dal: Jun 2002
Città: Milano
Messaggi: 959
|
Quote:
non ho ancora studiato il jframe quindi basta che aggiungo ? Codice:
public static void main(String[] a) { JFrame frame=new JFrame("Titolo"); frame.getContentPane().add(outputarea); //la riga seguente è una funzione standard da sostituire //con una chiusura dell'applicazione più consona frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(500,500); frame.show(); |
|
![]() |
![]() |
![]() |
#4 |
Senior Member
Iscritto dal: Jun 2002
Città: Milano
Messaggi: 959
|
ok sono riuscito...e se ora volessi inviare un comando al server??
dovrei usare un action listener? |
![]() |
![]() |
![]() |
#5 |
Bannato
Iscritto dal: Nov 2001
Città: Verona
Messaggi: 1086
|
E' più lungo a dirsi che a farsi:
ti serve un'area di testo o un campo, a seconda che tu voglia inviare una o più linee di testo insieme. Poi imposti il layout del JFrame in modo che il componente di testo venga visualizzato con un minimo di coerenza: Codice:
import java.awt.*; import java.awt.event.*; import javax.swing.*; ... class... ... static JTextField testoOut=new JTextField(); ... public static void main(String[] a) { JFrame frame=new JFrame("Titolo"); JTextArea outputArea=new JTextArea(); frame.getContentPane().setLayout(new BorderLayout()); frame.getContentPane().add(testoOut, BorderLayout.SOUTH); frame.getContentPane().add(outputArea, BorderLayout.CENTER); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400,400); frame.show(); ...ecc... Aggiungi a "testoOut" un gestore di eventi che reagisca alla pressione del tasto "enter" (il metodo più intuitivo per l'invio di testo da componenti a linea singola): Codice:
testoOut.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent ke) { if(ke.getKeyCode()==KeyEvent.VK_ENTER) { inviaTestoAlServer(); } } }); Codice:
static void inviaTestoAlServer() { os.println(testoOut.getText()); } ![]() |
![]() |
![]() |
![]() |
#6 |
Senior Member
Iscritto dal: Jun 2002
Città: Milano
Messaggi: 959
|
mi da questo errore nella funzione keyPressed
identifier> expected testoOut.addKeyListener(new KeyAdapter() { Ultima modifica di SteR9 : 24-08-2003 alle 07:05. |
![]() |
![]() |
![]() |
#7 |
Bannato
Iscritto dal: Nov 2001
Città: Verona
Messaggi: 1086
|
controlla la successione di parentesi e la presenza di un punto e virgola al termine della settima linea.
Il codice seguente è corretto ![]() Codice:
testoOut.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent ke) { if(ke.getKeyCode()==KeyEvent.VK_ENTER) { // } } }); |
![]() |
![]() |
![]() |
#8 |
Senior Member
Iscritto dal: Jun 2002
Città: Milano
Messaggi: 959
|
posto il codice che facciamo prima...
Codice:
import java.io.*; import java.net.*; import java.awt.*; import javax.swing.*; import java.util.*; import java.awt.event.*; public class irc extends Frame implements KeyListener{ testoOut.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent ke) { if(ke.getKeyCode()==KeyEvent.VK_ENTER) { inviaTestoAlServer(); } } }); static void inviaTestoAlServer() { os.println(testoOut.getText()); } public static void main(String[] args){ JTextField testoOut=new JTextField(); JTextArea outputarea = new JTextArea(); JFrame frame=new JFrame("Titolo"); frame.getContentPane().add(outputarea); frame.getContentPane().add(testoOut, BorderLayout.SOUTH); //la riga seguente è una funzione standard da sostituire //con una chiusura dell'applicazione più consona frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(500,500); frame.show(); String pong="",pon="",comandi="",prova=""; char po[]; po = new char[9]; Socket smtpSocket = null; PrintStream os = null; DataInputStream is = null; try { smtpSocket = new Socket("irc.dal.net", 6667); os = new PrintStream(smtpSocket.getOutputStream()); is = new DataInputStream(smtpSocket.getInputStream()); } catch (UnknownHostException e) { System.err.println("Don't know about host: hostname"); } catch (IOException e) { System.err.println("Couldn't get I/O for the connection to: hostname"); } if (smtpSocket != null && os != null && is != null) { try { os.println("user user hostname ip :user"); os.println("nick guest2993"); pong = is.readLine(); pong.getChars(5,13,po,0); for (int i=0;i<9;i++) pon += po[i]; System.out.println("pong="+pon); System.out.println(pong); os.println("pong "+pon); String responseLine; while ((responseLine = is.readLine()) != null) { System.out.println("Server: " + responseLine); prova += responseLine; outputarea.append(responseLine + "\n"); if (responseLine.indexOf("Ok") != -1) { break; } } os.close(); is.close(); smtpSocket.close(); } catch (UnknownHostException e) { System.err.println("Trying to connect to unknown host: " + e); } catch (IOException e) { System.err.println("IOException: " + e); } } } } |
![]() |
![]() |
![]() |
#9 |
Bannato
Iscritto dal: Nov 2001
Città: Verona
Messaggi: 1086
|
Bel macello
![]() All'esterno di un metodo in Java puoi inizializzare variabili ma non usare gli oggetti inizializzati. testoOut.addKeyListener(...) va a finire nel main. Non è necessario che "irc" estenda Frame né che implementi KeyListener. O meglio, sarebbe il modo più "Java-style" di costruire l'applicazione, ma devi rimaneggiare ampiamente il codice per portarlo su quella strada. PrintStream os = null; dovrebbe diventare static PrintStream os= null; e finire in un'area di visibilità di classe (quella in cui al momento si trova testoOut.addKeyListener(...) ) Così dovrebbe "andare" |
![]() |
![]() |
![]() |
#10 |
Senior Member
Iscritto dal: Jun 2002
Città: Milano
Messaggi: 959
|
mmm non ho ben capito cosa devo fare....
in pratica lui non riconosce testoOut?? |
![]() |
![]() |
![]() |
#11 |
Bannato
Iscritto dal: Nov 2001
Città: Verona
Messaggi: 1086
|
Non solo, anche se avessi dichiarato la variabile testoout "prima" comunque non avresti potuto attaccargli un keyListener.
Se mi dai 5 minuti ti sposto le linee di codice che andrebbero spostate e le evidenzio in grassetto, così tagliamo la testa al toro ![]() |
![]() |
![]() |
![]() |
#12 |
Bannato
Iscritto dal: Nov 2001
Città: Verona
Messaggi: 1086
|
Così dovrebbe compilare correttamente. Dovrebbe anche funzionare.
Codice:
import java.io.*; import java.net.*; import java.awt.*; import javax.swing.*; import java.util.*; import java.awt.event.*; public class temp /*extends Frame implements KeyListener*/{ /* testoOut.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent ke) { if(ke.getKeyCode()==KeyEvent.VK_ENTER) { inviaTestoAlServer(); } } });*/ static JTextField testoOut = new JTextField(); static PrintStream os = null; static void inviaTestoAlServer() { if(os!=null) { os.println(testoOut.getText()); } } public static void main(String[] args){ //JTextField testoOut=new JTextField(); //****************************************** testoOut.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent ke) { if(ke.getKeyCode()==KeyEvent.VK_ENTER) { inviaTestoAlServer(); } } }); //****************************************** JTextArea outputarea = new JTextArea(); JFrame frame=new JFrame("Titolo"); frame.getContentPane().add(outputarea); frame.getContentPane().add(testoOut, BorderLayout.SOUTH); //la riga seguente è una funzione standard da sostituire //con una chiusura dell'applicazione più consona frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(500,500); frame.show(); String pong="",pon="",comandi="",prova=""; char po[]; po = new char[9]; Socket smtpSocket = null; /*PrintStream os = null;*/ DataInputStream is = null; try { smtpSocket = new Socket("irc.dal.net", 6667); os = new PrintStream(smtpSocket.getOutputStream()); is = new DataInputStream(smtpSocket.getInputStream()); } catch (UnknownHostException e) { System.err.println("Don't know about host: hostname"); } catch (IOException e) { System.err.println("Couldn't get I/O for the connection to: hostname"); } if (smtpSocket != null && os != null && is != null) { try { os.println("user user hostname ip :user"); os.println("nick guest2993"); pong = is.readLine(); pong.getChars(5,13,po,0); for (int i=0;i<9;i++) pon += po[i]; System.out.println("pong="+pon); System.out.println(pong); os.println("pong "+pon); String responseLine; while ((responseLine = is.readLine()) != null) { System.out.println("Server: " + responseLine); prova += responseLine; outputarea.append(responseLine + "\n"); if (responseLine.indexOf("Ok") != -1) { break; } } os.close(); is.close(); smtpSocket.close(); } catch (UnknownHostException e) { System.err.println("Trying to connect to unknown host: " + e); } catch (IOException e) { System.err.println("IOException: " + e); } } } } |
![]() |
![]() |
![]() |
#13 |
Bannato
Iscritto dal: Nov 2001
Città: Verona
Messaggi: 1086
|
Ho dimenticato una linea.
E' meglio che la linea di testo presente nel JTextField venga resettata dopo l'invio...a dire il vero è l'unico modo per inviare più comandi ![]() Codice:
static void inviaTestoAlServer() { if(os!=null) { os.println(testoOut.getText()); testoOut.setText(""); } } |
![]() |
![]() |
![]() |
#14 |
Senior Member
Iscritto dal: Jun 2002
Città: Milano
Messaggi: 959
|
funziona alla grande,ti ringrazio!
![]() |
![]() |
![]() |
![]() |
#15 |
Senior Member
Iscritto dal: Jun 2002
Città: Milano
Messaggi: 959
|
e se volessi aggiungere uno scroller??che funzione devo usare??
perchè quando il testo diventa lungo sparisce... |
![]() |
![]() |
![]() |
#16 |
Bannato
Iscritto dal: Nov 2001
Città: Verona
Messaggi: 1086
|
La semplicità delle librerie Java ci viene in aiuto.
Occorre usare un "JScrollPane" cambia questa riga frame.getContentPane().add(outputarea); con queste due JScrollPane scroller=new JScrollPane(outputarea); frame.getContentPane().add(scroller); e il gioco è fatto. |
![]() |
![]() |
![]() |
#17 | |
Senior Member
Iscritto dal: Jun 2002
Città: Milano
Messaggi: 959
|
Quote:
|
|
![]() |
![]() |
![]() |
#18 | |
Bannato
Iscritto dal: Nov 2001
Città: Verona
Messaggi: 1086
|
Quote:
![]() Devi impostare la posizione del cursore nell'area di testo usando come indice la lunghezza del testo contenuto nell'area. dopo la linea outputarea.append(responseLine + "\n"); aggiungi questa outputarea.setCaretPosition(outputarea.getText().length()); |
|
![]() |
![]() |
![]() |
#19 |
Senior Member
Iscritto dal: Jun 2002
Città: Milano
Messaggi: 959
|
visto che sto tentando di creare un client irc ora vorrei poter scrivere in un altro JFrame ciò che viene scritto nel canale,mi è abbastanza chiaro come fare,ma non ho ben chiaro come "estrarre" solo ciò che viene scritto nel canale....
non esiste un tutorial sulla creazione di un client??con anche tutti i comandi che può ricevere un server irc... |
![]() |
![]() |
![]() |
#20 |
Bannato
Iscritto dal: Nov 2001
Città: Verona
Messaggi: 1086
|
Per un tutorial sinceramente non saprei dove indirizzarti. Vediamo se qualcun'altro interviene.
|
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 14:17.