这个是我程序的一部分,通过在主JFrame里按connect的JButton后连接到指定server,
但不知道是不是因为阻塞的问题,每次按下去后JButton就弹不上来,其他的按钮也不能点了,
我试着把输入输出分离做成多线程,但很难分开,初学java,希望大家指点。有类似的最好谈谈
最好有实例,谢谢,分不多20分全送了。class Client extends JDialog {
public JTextField tf;
public JTextArea display;
public ObjectOutputStream output;
public ObjectInputStream input;
public String msg="";
public Socket client;
public String linkstatus;
Box box;
//Thread st;
Client(JFrame frame){
super(frame,"CLI Mode",true);
Container cont=getContentPane();
tf=new JTextField();
//st=new Thread(this);
box=Box.createVerticalBox();
tf.setEnabled(false);
tf.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent eve){
sendData(eve.getActionCommand());
tf.setText(null);
}
});
cont.add(tf,BorderLayout.NORTH);
display=new JTextArea();
box.add(Box.createVerticalStrut(8));
box.add(new JScrollPane(display));
cont.add(box,BorderLayout.CENTER);
setSize(640,480);
setLocation(140,130);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
setVisible(false);
}
});
} public void runClient(){
try{
connectToServer();
getStream();
processConnection();
closeConnection();
}catch(EOFException eofe){
display.append('\n'+"服务器中断连接!");
connectresponse.append('\n'+"服务器中断连接!");
connectresponse.setCaretPosition(connectresponse.getText().length());
}catch(IOException ioe){
ioe.printStackTrace();
}
}
private void getStream() { try{
output=new ObjectOutputStream(client.getOutputStream());
output.flush();
input=new ObjectInputStream(client.getInputStream());
display.append("\n获得 I/O 输入输出!\n");
display.append("\n获得 I/O 输入输出!\n");
connectresponse.append("\n获得 I/O 输入输出!\n");
connectresponse.setCaretPosition(connectresponse.getText().length());
} catch (IOException ioe1){ioe1.printStackTrace();}}
private void connectToServer() {try{
display.setText("正在尝试到服务器的连接......\n");
client=new Socket(t_ip.getText(),Integer.parseInt(t_port.getText()));
display.append("Connected to:"+client.getInetAddress());
linkstatus="OK";
} catch (IOException ioe2){ioe2.printStackTrace();}}
private void processConnection() throws IOException{
tf.setEnabled(true);//设置tf的setenabled为true,以便客户能发送消息
do{
try{
msg=(String)input.readObject();
display.append("\n"+msg);
display.setCaretPosition(display.getText().length());
}catch(ClassNotFoundException cnfe){
display.append("\n 收到不明类型");
connectresponse.append("\n收到不明类型");
connectresponse.setCaretPosition(connectresponse.getText().length());
}
}
while(true);
}
private void closeConnection() throws IOException {
display.append("\n 正在关闭连接");
connectresponse.append("\n正在关闭连接");
connectresponse.setCaretPosition(connectresponse.getText().length());
try{
output.close();
input.close();
client.close();
linkstatus="CLOSED";}
catch(SocketException skec) {skec.printStackTrace();}
}
private void sendData(String msg){
try{
output.writeObject(msg);
output.flush();
display.append(msg+'\n');
}catch(IOException ioe){
display.append("\n输出有误!");
connectresponse.append("\n输出有误!");
connectresponse.setCaretPosition(connectresponse.getText().length());
linkstatus="SendError";
}
}
}
但不知道是不是因为阻塞的问题,每次按下去后JButton就弹不上来,其他的按钮也不能点了,
我试着把输入输出分离做成多线程,但很难分开,初学java,希望大家指点。有类似的最好谈谈
最好有实例,谢谢,分不多20分全送了。class Client extends JDialog {
public JTextField tf;
public JTextArea display;
public ObjectOutputStream output;
public ObjectInputStream input;
public String msg="";
public Socket client;
public String linkstatus;
Box box;
//Thread st;
Client(JFrame frame){
super(frame,"CLI Mode",true);
Container cont=getContentPane();
tf=new JTextField();
//st=new Thread(this);
box=Box.createVerticalBox();
tf.setEnabled(false);
tf.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent eve){
sendData(eve.getActionCommand());
tf.setText(null);
}
});
cont.add(tf,BorderLayout.NORTH);
display=new JTextArea();
box.add(Box.createVerticalStrut(8));
box.add(new JScrollPane(display));
cont.add(box,BorderLayout.CENTER);
setSize(640,480);
setLocation(140,130);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
setVisible(false);
}
});
} public void runClient(){
try{
connectToServer();
getStream();
processConnection();
closeConnection();
}catch(EOFException eofe){
display.append('\n'+"服务器中断连接!");
connectresponse.append('\n'+"服务器中断连接!");
connectresponse.setCaretPosition(connectresponse.getText().length());
}catch(IOException ioe){
ioe.printStackTrace();
}
}
private void getStream() { try{
output=new ObjectOutputStream(client.getOutputStream());
output.flush();
input=new ObjectInputStream(client.getInputStream());
display.append("\n获得 I/O 输入输出!\n");
display.append("\n获得 I/O 输入输出!\n");
connectresponse.append("\n获得 I/O 输入输出!\n");
connectresponse.setCaretPosition(connectresponse.getText().length());
} catch (IOException ioe1){ioe1.printStackTrace();}}
private void connectToServer() {try{
display.setText("正在尝试到服务器的连接......\n");
client=new Socket(t_ip.getText(),Integer.parseInt(t_port.getText()));
display.append("Connected to:"+client.getInetAddress());
linkstatus="OK";
} catch (IOException ioe2){ioe2.printStackTrace();}}
private void processConnection() throws IOException{
tf.setEnabled(true);//设置tf的setenabled为true,以便客户能发送消息
do{
try{
msg=(String)input.readObject();
display.append("\n"+msg);
display.setCaretPosition(display.getText().length());
}catch(ClassNotFoundException cnfe){
display.append("\n 收到不明类型");
connectresponse.append("\n收到不明类型");
connectresponse.setCaretPosition(connectresponse.getText().length());
}
}
while(true);
}
private void closeConnection() throws IOException {
display.append("\n 正在关闭连接");
connectresponse.append("\n正在关闭连接");
connectresponse.setCaretPosition(connectresponse.getText().length());
try{
output.close();
input.close();
client.close();
linkstatus="CLOSED";}
catch(SocketException skec) {skec.printStackTrace();}
}
private void sendData(String msg){
try{
output.writeObject(msg);
output.flush();
display.append(msg+'\n');
}catch(IOException ioe){
display.append("\n输出有误!");
connectresponse.append("\n输出有误!");
connectresponse.setCaretPosition(connectresponse.getText().length());
linkstatus="SendError";
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货