程序运行没问题,但是退出时,控制台并没有释放资源,必须要ctrl+c;
Talk:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;public class Talk extends JFrame implements ActionListener,Runnable
{
JButton button;
JTextArea text1,text2;
DatagramPacket sendData=null,receiveData=null;
DatagramSocket sendSocket=null,receiveSocket=null;
byte b1[],b2[];
Thread receiveThread=null;
JScrollPane jscrollpane1,jscrollpane2;
JPanel panel1,panel2,panel3;
Container con=null;
JSplitPane jsplitpane,jsplitpane1;
JLabel label;
JTextField textfield; Talk()
{//***界面的制作
setVisible(true);
setBounds(200,200,400,400);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setResizable(false);
button=new JButton("发送");
button.addActionListener(this);
text1=new JTextArea();
text1.setEditable(false);
text2=new JTextArea();
text2.setText("");
jscrollpane1=new JScrollPane(text1);
jscrollpane2=new JScrollPane(text2);
label=new JLabel("请输入IP地址:");
textfield=new JTextField(20);
textfield.setText("");
panel1=new JPanel();
panel1.setLayout(new BorderLayout());
panel1.setVisible(true);
panel1.setBounds(0,0,200,200);
panel1.add(jscrollpane1,BorderLayout.CENTER);
panel1.validate();
panel2=new JPanel();
panel2.setLayout(new BorderLayout());
panel2.setVisible(true);
panel2.setSize(100,100);
panel2.add(jscrollpane2,BorderLayout.CENTER);
panel2.validate();
panel3=new JPanel();
panel3.setVisible(true);
panel3.setLayout(null);
panel3.add(button);
panel3.add(label);
panel3.add(textfield);
label.setBounds(10,0,80,30);
textfield.setBounds(100,0,100,25);
button.setBounds(310,0,60,30);
jsplitpane=new JSplitPane(JSplitPane.VERTICAL_SPLIT,panel1,panel2);
jsplitpane.setDividerLocation(250);
jsplitpane1=new JSplitPane(JSplitPane.VERTICAL_SPLIT,jsplitpane,panel3);
jsplitpane1.setDividerLocation(330);
con=getContentPane();
con.add(jsplitpane1,BorderLayout.CENTER);
validate();
//***界面
receiveThread=new Thread(this);
receiveThread.start();
}//Talk()
public void run()
{
try{
b1=new byte[10];
receiveData=new DatagramPacket(b1,b1.length);
receiveSocket=new DatagramSocket(6819);
}catch(IOException a){}
while(true)
{
try{
receiveSocket.receive(receiveData);
}catch(IOException aa){continue;}
String s=new String(b1).trim();
text1.append("对方说:"+"\n");
text1.append(new String(b1)+"\n");
}//while()
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button)
{
if(!textfield.getText().equals(""))
{
if(!text2.getText().equals(""))
{
try{
String s=textfield.getText();
String s1=text2.getText();
b2=s1.getBytes();
InetAddress address=InetAddress.getByName(s);
sendData=new DatagramPacket(b2,b2.length,address,6819);
sendSocket=new DatagramSocket();
sendSocket.send(sendData);
text2.setText("");
text1.append("我说:"+"\n");
text1.append(s1+"\n");
}catch(IOException aa){}
}//if(text2.getText()!=null)
else {JOptionPane.showMessageDialog(this,"不能发空消息","error",JOptionPane.INFORMATION_MESSAGE);}
}//if(textfield.getText()!=null)
else {JOptionPane.showMessageDialog(this,"IP地址未输入","error",JOptionPane.WARNING_MESSAGE);}
}
} public static void main(String args[])
{new Talk();}
}//class Talk
Talk:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;public class Talk extends JFrame implements ActionListener,Runnable
{
JButton button;
JTextArea text1,text2;
DatagramPacket sendData=null,receiveData=null;
DatagramSocket sendSocket=null,receiveSocket=null;
byte b1[],b2[];
Thread receiveThread=null;
JScrollPane jscrollpane1,jscrollpane2;
JPanel panel1,panel2,panel3;
Container con=null;
JSplitPane jsplitpane,jsplitpane1;
JLabel label;
JTextField textfield; Talk()
{//***界面的制作
setVisible(true);
setBounds(200,200,400,400);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setResizable(false);
button=new JButton("发送");
button.addActionListener(this);
text1=new JTextArea();
text1.setEditable(false);
text2=new JTextArea();
text2.setText("");
jscrollpane1=new JScrollPane(text1);
jscrollpane2=new JScrollPane(text2);
label=new JLabel("请输入IP地址:");
textfield=new JTextField(20);
textfield.setText("");
panel1=new JPanel();
panel1.setLayout(new BorderLayout());
panel1.setVisible(true);
panel1.setBounds(0,0,200,200);
panel1.add(jscrollpane1,BorderLayout.CENTER);
panel1.validate();
panel2=new JPanel();
panel2.setLayout(new BorderLayout());
panel2.setVisible(true);
panel2.setSize(100,100);
panel2.add(jscrollpane2,BorderLayout.CENTER);
panel2.validate();
panel3=new JPanel();
panel3.setVisible(true);
panel3.setLayout(null);
panel3.add(button);
panel3.add(label);
panel3.add(textfield);
label.setBounds(10,0,80,30);
textfield.setBounds(100,0,100,25);
button.setBounds(310,0,60,30);
jsplitpane=new JSplitPane(JSplitPane.VERTICAL_SPLIT,panel1,panel2);
jsplitpane.setDividerLocation(250);
jsplitpane1=new JSplitPane(JSplitPane.VERTICAL_SPLIT,jsplitpane,panel3);
jsplitpane1.setDividerLocation(330);
con=getContentPane();
con.add(jsplitpane1,BorderLayout.CENTER);
validate();
//***界面
receiveThread=new Thread(this);
receiveThread.start();
}//Talk()
public void run()
{
try{
b1=new byte[10];
receiveData=new DatagramPacket(b1,b1.length);
receiveSocket=new DatagramSocket(6819);
}catch(IOException a){}
while(true)
{
try{
receiveSocket.receive(receiveData);
}catch(IOException aa){continue;}
String s=new String(b1).trim();
text1.append("对方说:"+"\n");
text1.append(new String(b1)+"\n");
}//while()
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button)
{
if(!textfield.getText().equals(""))
{
if(!text2.getText().equals(""))
{
try{
String s=textfield.getText();
String s1=text2.getText();
b2=s1.getBytes();
InetAddress address=InetAddress.getByName(s);
sendData=new DatagramPacket(b2,b2.length,address,6819);
sendSocket=new DatagramSocket();
sendSocket.send(sendData);
text2.setText("");
text1.append("我说:"+"\n");
text1.append(s1+"\n");
}catch(IOException aa){}
}//if(text2.getText()!=null)
else {JOptionPane.showMessageDialog(this,"不能发空消息","error",JOptionPane.INFORMATION_MESSAGE);}
}//if(textfield.getText()!=null)
else {JOptionPane.showMessageDialog(this,"IP地址未输入","error",JOptionPane.WARNING_MESSAGE);}
}
} public static void main(String args[])
{new Talk();}
}//class Talk
界面代码我已经标明了.我在想是不是我的接受线程由于是反复执行没有退出条件
所以在程序退出时.线程并没有退出有这种可能吗
哪位高手帮忙给看看