飘遥诗涯 2011-12-15 20:34
浏览 162
已采纳

求帮忙改改,使能运行!!!

package client;

import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.AbstractButton;

public class Client extends JFrame {

private JTextField nameField1,mailField2,field;
private JRadioButton maleButton,womenButton;
private JComboBox sdept;
private ButtonGroup radioGroup;
private JButton button;
private JTextArea displayArea,displayArea2;
private DatagramSocket socket;
// set up GUI and DatagramSocket
public Client()
{ super( "客户应用" );
Container container = getContentPane();
container.setLayout(new FlowLayout());

Box box1=Box.createHorizontalBox();
JLabel label1=new JLabel("姓名:");
box1.add(label1);
nameField1= new JTextField(10);
box1.add(nameField1);
container.add(box1);

Box box2=Box.createHorizontalBox();
JLabel label2=new JLabel("性别:");
box2.add(label2);
maleButton=new JRadioButton("男",true);
box2.add(maleButton);
womenButton=new JRadioButton("女",false);
box2.add(womenButton);

radioGroup= new ButtonGroup();
radioGroup.add(maleButton);
radioGroup.add(womenButton);

container.add(box2);

JLabel label3=new JLabel("电子邮箱:");
container.add(label3);
mailField2=new JTextField(20);
container.add(mailField2);

Box box3=Box.createHorizontalBox();
JLabel label4=new JLabel("备注:");
box3.add(label4);
displayArea=new JTextArea(5,12);
box3.add(new JScrollPane( displayArea ));
button=new JButton("提交");
box3.add(button);
container.add(box3);

Box box4=Box.createHorizontalBox();

displayArea2=new JTextArea(5,13);
box4.add(displayArea2);
container.add(box4);
setSize( 300,500);
setVisible( true );

  button .addActionListener(
  new ActionListener() { 
     public void actionPerformed( ActionEvent event )
     {  // create and send packet
        try {
            // get message from textfield and convert to byte array
            String string=new String();
               string ="姓名:" +nameField1.getText() + "\n"+"性别:" +radioGroup.getElements().nextElement()+ "电子邮箱:"+mailField2.getText()+"备注:"+displayArea.getText();


           String message = string;
           byte data[] = message.getBytes();
            // create sendPacket
           DatagramPacket sendPacket = new DatagramPacket( data, 
              data.length, InetAddress.getLocalHost(), 5000 );
           socket.send( sendPacket ); // send packet
        }               
        catch ( IOException ioException ) {
           ioException.printStackTrace();
        }
     } // end actionPerformed
  } // end inner class

); // end call to addActionListener

// create DatagramSocket for sending and receiving packets
try {
socket = new DatagramSocket();
}
// catch problems creating DatagramSocket
catch( SocketException socketException ) {
socketException.printStackTrace();
System.exit( 1 );
}
}
// wait for packets to arrive from Server, display packet contents
private void waitForPackets()
{
while ( true ) { // loop forever

  // receive packet and display contents
  try {
     // set up packet
     byte data[] = new byte[ 100 ];
     DatagramPacket receivePacket = new DatagramPacket( 
        data, data.length );
     socket.receive( receivePacket ); // wait for packet
     // display packet contents
     displayMessage( "服务器收到的信息是:"+ new String( receivePacket.getData(), 
           0, receivePacket.getLength() ) +
        "\n长度为: " + receivePacket.getLength()+ 
        "\n\n对方主机: " + receivePacket.getAddress() + 
        "\n端口号: " + receivePacket.getPort() ); 
  } 
  // process problems receiving or displaying packet
  catch( IOException exception ) {
     displayMessage( exception.toString() + "\n" );
     exception.printStackTrace();
  }

} // end while
}
// displayArea in the event-dispatch thread
private void displayMessage( final String messageToDisplay )
{
// display message from event-dispatch thread of execution
SwingUtilities.invokeLater(
new Runnable() { // inner class to ensure GUI updates properly
public void run() // updates displayArea
{ displayArea2.append( messageToDisplay );

}
} // end inner class
);
}

public static void main( String args[] )
{
Client application = new Client();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
application.waitForPackets();
}

}

  • 写回答

4条回答 默认 最新

  • 不爱吃萝卜的兔子 2011-12-16 10:47
    关注

    [code="java"]
    string ="姓名:" +nameField1.getText() + "\n"+"性别:" +(radioGroup.getElements().nextElement().getModel().isSelected()? "男":"女")+ " 电子邮箱:"+mailField2.getText()+"备注:"+displayArea.getText();

    [/code]
    可以了!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办