本帖最后由 杜俊彪 于 2012-6-1 17:31 编辑
是当我按回车键 响应时, 文本域中的闪烁光标自动 从第一行跳到第二行; ; 这么怎么解决!!
import java.awt.* ;
import java.awt.event.* ;
import java.net.* ;
import javax.swing.* ;
public class Adu extends JFrame {
private static final long serialVersionUID = 1L;
private JTextArea tArea01 ;
private JTextArea tArea02 ;
private JButton sendButton ;
private JButton closeButton ;
private JScrollPane sp1,sp2 ;
private DatagramSocket s1 = null ;
private DatagramSocket s2 = null ;
public static int port = 11111 ;
public Adu(){
Gui() ;
String string ;
try {
string = InetAddress.getByName(InetAddress.getLocalHost().getHostAddress()).toString() ;
System.out.println(string);
} catch (UnknownHostException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
//发送按扭 响应事件
sendButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
s1 = new DatagramSocket() ;
} catch (SocketException e1) {
e1.printStackTrace();
}
byte[] buf = new byte[1024] ;
buf = tArea02.getText().getBytes() ;
try {
DatagramPacket dPacket = new DatagramPacket(buf, buf.length,
/*InetAddress.getLocalHost()*/InetAddress.getByName(InetAddress.getLocalHost().
getHostAddress()), port) ;
s1.send(dPacket) ;
tArea02.setText("") ;
tArea02.requestFocus() ;
} catch (Exception e1) {
e1.printStackTrace();
}
}
}) ;
//关闭按扭响应事件
closeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.exit(0) ;
s1.close() ;
s2.close() ;
}
}) ;
//回车按扭响应事件, 就是这里出了问题;当按回车键后,他自动换行了,求高手运行试下;
tArea02.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_ENTER){
try {
s1 = new DatagramSocket() ;
} catch (SocketException e1) {
e1.printStackTrace();
}
byte[] buf ;
buf = tArea02.getText().getBytes() ;
try {
DatagramPacket dPacket = new DatagramPacket(buf, buf.length,
/*InetAddress.getLocalHost()*/InetAddress.getByName(InetAddress.getLocalHost().
getHostAddress()), port) ;
s1.send(dPacket) ;
tArea02.setText("") ;
tArea02.requestFocus() ;
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
}) ;
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent w)
{
if(JOptionPane.showConfirmDialog(null,"是否要退出?","退出",JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION)
{
dispose();
System.exit(0);
s1.close();
s2.close() ;
}
if(JOptionPane.showConfirmDialog(null,"是否要退出?","退出",JOptionPane.YES_NO_OPTION)==JOptionPane.NO_OPTION)
{
Adu.this.setVisible(true) ;
}
}
});
receiveData() ;
}
private void receiveData() {
try {
s2 = new DatagramSocket(port) ;
} catch (SocketException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
new Thread(new Runnable()
{
public void run()
{
byte buf[] = new byte[1024];
//表示接受数据报包
while(true)
{
try
{
DatagramPacket dPacket = new DatagramPacket(buf,1024);
s2.receive(dPacket);
tArea01.append("【消息来自】◆" + dPacket.getAddress().getHostAddress() + "◆"+"【说】:"
+ new String (buf,0,dPacket.getLength()) + "\r\n");
}
catch(Exception e)
{
if(s2.isClosed())
{
e.printStackTrace();
}
}
}
}
}).start() ;
}
public static void main(String arg[]){
new Adu() ;
}
public void Gui(){
this.setTitle("聊天窗口") ;
Container container = getContentPane() ;
container.setLayout(null) ;
tArea01 = new JTextArea() ;
tArea01.setBounds(0, 0, 550, 300) ;
tArea01.setBackground(Color.yellow) ;
tArea01.setLineWrap(true) ;
tArea02 = new JTextArea("Type \"大家都来围观我吧\" here") ;
tArea02.setBounds(0, 315, 550, 150) ;
tArea02.setInputVerifier(new InputVerifier() {
@Override
public boolean verify(JComponent input) {
JTextArea tArea = (JTextArea)input ;
return "大家都来围观我吧".equals(tArea.getText()) ;
}
}) ;
tArea02.setLineWrap(true) ;
sendButton = new JButton("发送") ;
sendButton.setBounds(250, 470, 75, 40) ;
closeButton = new JButton("关闭") ;
closeButton.setBounds(400, 470, 75, 40) ;
sp1 = new JScrollPane(tArea01) ;
sp1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS) ;
container.add(sp1) ;
sp2 = new JScrollPane(tArea02) ;
sp2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS) ;
container.add(sp2) ;
this.setSize(600, 550) ;
this.setLocation(200, 200) ;
this.setUndecorated(true);
this.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
container.add(tArea01) ;
container.add(tArea02) ;
container.add(sendButton) ;
container.add(closeButton) ;
this.setVisible(true) ;
tArea02.requestFocus() ;
}
} |