Click to See Complete Forum and Search --> : Primitive IM Program Help


Nifrpe
04-20-2010, 08:38 AM
Hi everyone, I was wondering if you could help me with a logic error. Look at this program for a minute:

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Color;

public class betaClient extends JFrame {

public static JButton sendButton = new JButton("SEND");
public static JButton endButton = new JButton("End Chat");
public static JTextField messageInput = new JTextField();
public static JLabel[] logLine = new JLabel[8];
public static boolean[] jWTLL = new boolean[8];
public static int i = 0;

public static void main(String[] args){
new betaClient();
}

public betaClient() {

for(i = 0; i < 8; i++){
logLine[i] = new JLabel("ABC" + '\n');
jWTLL[i] = true;
}

JPanel inputArea = new JPanel();
inputArea.setLayout(new BorderLayout());
inputArea.add(sendButton, BorderLayout.WEST);
inputArea.add(messageInput, BorderLayout.CENTER);
inputArea.add(endButton, BorderLayout.EAST);

JPanel logArea = new JPanel();
logArea.setLayout(new GridLayout(8, 1, 4, 4));
logArea.add(logLine[0]);
logArea.add(logLine[1]);
logArea.add(logLine[2]);
logArea.add(logLine[3]);
logArea.add(logLine[4]);
logArea.add(logLine[5]);
logArea.add(logLine[6]);
logArea.add(logLine[7]);

setLayout(new BorderLayout());
add(inputArea, BorderLayout.SOUTH);
add(logArea, BorderLayout.CENTER);
setTitle("Nick's IM Client 2.0");
setSize(500, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

sendButton.addActionListener(new sendAction());
messageInput.addActionListener(new sendAction());
endButton.addActionListener(new endClientAction());

setVisible(true);

}

private class sendAction implements ActionListener{
public void actionPerformed(ActionEvent e) {

for(i = 1; i != 0; i++) {
int q = i - 1;
if(jWTLL[0] == true && jWTLL[1] == true && jWTLL[2] == true && jWTLL[3] == true && jWTLL[4] == true && jWTLL[5] == true && jWTLL[6] == true && jWTLL[7]){
for(int h = 0; h < 8; h++){
jWTLL[h] = false;
}
}
else{
if(jWTLL[q] == false){
sendFunction blah = new sendFunction(logLine[q]);
jWTLL[q] = true;
String cool = blah.run();
betaClient.i = 0;
}
}
}

}
}

private class endClientAction implements ActionListener{
public void actionPerformed(ActionEvent e) {

}
}

public class sendFunction{

public JLabel line;

public sendFunction(JLabel sentLine){
this.line = sentLine;
}

public String run(){
String message = messageInput.getText();
line = new JLabel(message + '\n');
line.setForeground(Color.RED);
return "cool";
}

}

}


The problem is, when you go to send a message, the program begins the loop but doesn't stop and I don't know why! Please Help!:confused:

criterion9
04-20-2010, 10:08 AM
for(i = 1; i != 0; i++) {

This loop doesn't terminate. I suspect this line might need adjustment?

betaClient.i = 0;

Nifrpe
04-20-2010, 03:21 PM
thanks, but do you have any suggestions on how to change it? I've tried a couple things, however, it hasn't helped much. I'm only a novice on Java, so is there some super cool class that can help?

Nifrpe
04-20-2010, 03:31 PM
nevermind, it works!