PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : "Kleines" Java Problem



frog
20.08.2010, 23:53
Hi ihr ich hab da mal eine Kleines Probelm und zwar kriege ich ein Problem nicht Gelöst(bin noch ein ziemlicher Anfänger^^).


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/*
* ChatServer.java
*
* Created on 20.08.2010, 20:54:05
*/

/**
*
* @author RadioActiveFrosch
*/
package mulserver;
import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.event.*;






public class Main extends javax.swing.JFrame {

/** Creates new form ChatServer */
public Main() {
initComponents();
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jLabel3 = new javax.swing.JLabel();
jProgressBar1 = new javax.swing.JProgressBar();
jLabel4 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jTextField2 = new javax.swing.JTextField();
jButton2 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);
setTitle("Chat");

jLabel1.setText("First Enter IP");

jTextField1.setText("5.174.214.159");

jLabel2.setText("Press The Button");

jButton1.setText("Press Me");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jLabel3.setText("Wating for Progress Bar ");

jLabel4.setText("Chatting ;)");

jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);

jButton2.setText("Enter");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2)
.addComponent(jButton1)
.addComponent(jLabel3)
.addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1)
.addComponent(jLabel4))
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILI NG, layout.createSequentialGroup()
.addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 298, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addComponent(jButton2))
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 363, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.TRAILING, false)
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.LEADIN G, layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
.addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
.addComponent(jLabel4)))
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.TRAILING)
.addComponent(jButton2)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap())
);

pack();
}// </editor-fold>

/*
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {



}
*/
public String ta;

void setValue( String val )
{
this.ta =jTextArea1.getText();
}

String getValue()
{
return ta;
}
public void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
{
String ip;
Socket server = null;
String teststring;
ip=jTextField1.getText();

try
{
server = new Socket( ip , 3141 );
Scanner in = new Scanner( server.getInputStream() );
PrintWriter out = new PrintWriter( server.getOutputStream(), true );
out.println( "Bin ich angekommen?" );

teststring= in.nextLine();
if(teststring == null ? "Ja bin Ich!" == null : teststring.equals("Ja bin Ich!")){
jProgressBar1.setValue(100);
jTextArea1.append("Connected to the Server!\n");
}else{
jTextArea1.append("ERROR! CANT' CONNECT TO THE SERVER");
}
}
catch ( UnknownHostException e ) {
e.printStackTrace();
}
catch ( IOException e ) {
e.printStackTrace();
}
finally
{
if ( server != null )
try { server.close(); } catch ( IOException e ) { }


}
}}

public void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
jTextArea1.setEditable(false);
String newline = "\n";
String ta=jTextArea1.getText();
String ip;
Socket server = null;
ip=jTextField1.getText();
String text;

if(ta == null ? "ERROR! CANT' CONNECT TO THE SERVER" == null : ta.equals("ERROR! CANT' CONNECT TO THE SERVER")){
}else{
text = jTextField2.getText();
if( !text.equals("")){
jTextArea1.append(text + newline);


}else{}
}



try
{
text = jTextField2.getText();
server = new Socket( ip , 3141 );
PrintWriter out = new PrintWriter( server.getOutputStream(), true );
out.println(text);
jTextField2.setText("");

}
catch ( UnknownHostException e ) {
e.printStackTrace();
}
catch ( IOException e ) {
e.printStackTrace();
}
finally
{
if ( server != null )
try { server.close(); } catch ( IOException e ) { }


}


}
/*
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {




}
*/
private static void handleConnection( Socket client ) throws IOException
{


Scanner in = new Scanner( client.getInputStream() );
PrintWriter out = new PrintWriter( client.getOutputStream(), true );

String instring = in.nextLine();
if( instring == null ? "Bin ich angekommen?" == null : instring.equals("Bin ich angekommen?"))
out.println("Ja bin Ich!");




}




private void sendtext( Socket client ) throws IOException
{
String ip;
Socket server = null;
String teststring;
ip=javax.swing.JFrame.jTextField1.getText();

try
{
server = new Socket( ip , 3141 );
Scanner in = new Scanner( server.getInputStream() );
String instring = in.nextLine();
jTextArea1.append(instring+"\n");

}
catch ( UnknownHostException e ) {
e.printStackTrace();
}
catch ( IOException e ) {
e.printStackTrace();
}
finally
{
if ( server != null )
try { server.close(); } catch ( IOException e ) { }
}}




public static void main(String args[]) throws IOException
{
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Main().setVisible(true);
} });




Timer timer = new Timer("MeinDienst");





/*Server Part*/
ServerSocket server = new ServerSocket( 3141 );

while ( true )
{
Socket client = null;

try
{
client = server.accept();
handleConnection ( client );

}
catch ( IOException e ) {
e.printStackTrace();
}
finally {
if ( client != null )
try { client.close(); } catch ( IOException e ) { }
}
}









}

// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JProgressBar jProgressBar1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
// End of variables declaration

}


Es soll ein kleines Chat Programm werden...naja... der austausch von Daten geht ja...aber ich möchte das wennich den Text absende der in das jTextArea1 geht...da kommt das Problem. Ich wollte es mit sendtext() und nen timer machen aber der timer ist nur static aber um in das Text areal zu schreieben darf es nicht static-only sein...
pls help me^^

€: oder kurz gefasst wie kriege ich es hin das er alle 0.5 - 1 sec überprüft ob ein neuer Text reingekommenist und ihn in das jTextArea1 steckt?(reinstecke mit jTextArea1.append(ta+textempfangen))