I am trying to write a server application in java and creating a vector v1 to store all the sockets that hit the server. Next i made an arraylist where i stored all my Client sockets as objects and then created a JCheckBox array where I loop it to add checkbox to the JPanel controlpanel which was initiated in the class contructor but something seems to be going wrong and i can't see any checkbox.
import java.util.*;
import java.net.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.awt.*;
class server extends JFrame implements ActionListener
{
JTextArea t1;
PrintWriter pw;
JButton b1,b2;
static Vector v1=new Vector();
static ArrayList checks=new ArrayList();
static JPanel controlpanel;
server()
{
setLayout(new FlowLayout());
t1=new JTextArea();
add(t1);
b1=new JButton("Send to All");
b2=new JButton("Send");
//b1.addActionListener(this);
//b2.addActionListener(this);
add(b2,BorderLayout.SOUTH);
add(b1,BorderLayout.NORTH);
b1.addActionListener(this);
b2.addActionListener(this);
controlpanel=new JPanel();
controlpanel.setVisible(true);
add(controlpanel);
}
public void actionPerformed(ActionEvent ae)
{
String m=t1.getText();
if(ae.getSource()==b1)
{
try
{
Iterator t2=v1.iterator();
while(t2.hasNext())
{
Socket s=(Socket)t2.next();
pw=new PrintWriter(s.getOutputStream(),true);
pw.println(m);
pw.flush();
System.out.println("send"+m);
}
}catch(Exception e){}
}
if(ae.getSource()==b2)
{
try
{
Iterator itr=checks.iterator();
while(itr.hasNext())
{
Socket schecked=(Socket)itr.next();
pw=new PrintWriter(schecked.getOutputStream(),true);
pw.println(m);
pw.flush();
System.out.println("sent "+m);
}
}
catch(Exception e){}
}
t1.setText("");
}
public static void main(String z[])
{
try {
new server().setVisible(true);
ServerSocket s=new ServerSocket(2000);
while(true)
{
System.out.println("waiting...");
Socket c=s.accept();
// System.out.print(c);
v1.add(c);
for(int i=0;i<v1.size();i++)
{
checks.add(v1.get(i));
// checks[i].setText()=v1[i];
}
JCheckBox checkbox[]=new JCheckBox[checks.size()];
for(int i=0;i<checks.size();i++)
{
// Object o=checks.get(i);
checkbox[i]= new JCheckBox();
// checkbox[i].setText()=checks.get(i).name;
controlpanel.add(checkbox[i]);
checkbox[i].setSelected(true);
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Aucun commentaire:
Enregistrer un commentaire