X
xInvictus
Gast
Guten Tag,
ich bin gerade dabei, ein kleines Chat/Shoutbox Applet zu schreiben, welche alle 5 sekunden mit einer PHP-Datei auf dem Server kommuniziert, welche das Protokoll als HTML formatiert zurücklierfert, dieser neue Inhalt der JTextArea soll nun den bisherigen ersetzen, das ist die Logik.
Praktisch estellt sich das folgende Problem: Meine Main-Methode ist STATIC, in der die Routine läuft:
Mein Eclipse gibt mir hier den Fehler aus "Cannot make a static reference to the non-static field textArea"...
Hier noch einmal der Code im Ganzen, für besseres Verständnis von Klassen/Methoden und Variabel zusammenhängen:
Vielen Dank im Vorraus!
ich bin gerade dabei, ein kleines Chat/Shoutbox Applet zu schreiben, welche alle 5 sekunden mit einer PHP-Datei auf dem Server kommuniziert, welche das Protokoll als HTML formatiert zurücklierfert, dieser neue Inhalt der JTextArea soll nun den bisherigen ersetzen, das ist die Logik.
Praktisch estellt sich das folgende Problem: Meine Main-Methode ist STATIC, in der die Routine läuft:
Java:
public static void main(String[] args) {
//Schedule a job for the event dispatch thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
for(;;) {
try {
URL url = new URL("chat.php");
PhpPostConnect con = new PhpPostConnect(url);
try {
con.send("sets=show");
textArea.setText(con.read());
} catch (IOException e) {
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
Thread.sleep(5000);
} catch (InterruptedException e){
// ERROR
}
}
}
Hier noch einmal der Code im Ganzen, für besseres Verständnis von Klassen/Methoden und Variabel zusammenhängen:
Java:
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.*;
public class Test extends JPanel implements ActionListener {
protected JTextField textField;
protected JTextArea textArea;
private final static String newline = "\n";
public Test() {
super(new GridBagLayout());
textField = new JTextField(80);
textField.addActionListener(this);
textArea = new JTextArea(40, 80);
textArea.setEditable(false);
textArea.setText("Loading...");
JScrollPane scrollPane = new JScrollPane(textArea);
GridBagConstraints c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.HORIZONTAL;
add(textField, c);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
add(scrollPane, c);
}
public void actionPerformed(ActionEvent evt) {
String text = textField.getText();
textArea.append(text + newline);
textField.selectAll();
textArea.setCaretPosition(textArea.getDocument().getLength());
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("DontEvenTrip Chat Application");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new Test());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
for(;;) {
try {
URL url = new URL("chat.php");
PhpPostConnect con = new PhpPostConnect(url);
try {
con.send("sets=show");
textArea.setText(con.read());
} catch (IOException e) {
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
Thread.sleep(5000);
} catch (InterruptedException e){
// ERROR
}
}
}
}
Vielen Dank im Vorraus!