问题描述
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 DatagramSocketpublic 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 contentsprivate 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()+ "nn对方主机: " + 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 threadprivate 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();}} 问题补充:dingjunfeng 写道
解决方案
string ="姓名:" +nameField1.getText() + "n"+"性别:" +(radioGroup.getElements().nextElement().getModel().isSelected()? "男":"女")+ " 电子邮箱:"+mailField2.getText()+"备注:"+displayArea.getText();可以了!
解决方案二:
楼主的代码有点乱,要改进!我帮你改了下,我测试是可以用的!希望对你有帮助!
解决方案三:
不过这个应该不全吧。你的类名是client那么server呢?
解决方案四:
。。。。这个就可以运行啊,没有错啊。