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

问题描述

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呢?
解决方案四:
。。。。这个就可以运行啊,没有错啊。

时间: 2024-08-18 04:27:26

求帮忙改改,使能运行!!!的相关文章

c++-跪求帮忙,win7运行不了C++,在CSDN上下载之后只运行一次再新建工程就运行不了

问题描述 跪求帮忙,win7运行不了C++,在CSDN上下载之后只运行一次再新建工程就运行不了 RT谢了,拜托了····55555 刚在CSDN上下载的C++,说好的能在win7下运行,可是安装后只运行一次,再新建工程调试就错误 Compiling... c.cpp Linking... LINK : fatal error LNK1104: cannot open file "Debug/c.exe" 就显示这个····5555···肿么回事啊··· 解决方案 不知道您的版本是vc+

求c语言编写的经纬高转换成XYZ的程序,只要能简单运行的程序,悬赏20个币,求帮忙

问题描述 求c语言编写的经纬高转换成XYZ的程序,只要能简单运行的程序,悬赏20个币,求帮忙 求c语言编写的经纬高转换成XYZ的程序,只要能简单运行的程序,不要复杂的界面,悬赏20个币,求帮忙.程序要求能读取存储经纬高数据的文本,转换成XYZ存成文本,给力的话可以再加币,多谢多谢! 解决方案 纬度和高度都是可以直接转换的.只有经度,经度根据纬度可以换算,公式是cos(维度)*(赤道周长/360) 解决方案二: 以地球为严格的球体来做的话,以球心画出一个参考的三围坐标系,不难得到 #include

c语言-求帮忙看下为什么程序调用了西沟函数导致程序整个不能运行

问题描述 求帮忙看下为什么程序调用了西沟函数导致程序整个不能运行 写说明一下代码,代码主要是要在控制台模拟浏览器对tab和网页前进后退的一些操作 再说明一下问题,问题在于brwosertab类和webinformation类的西沟函数,一旦在这两个类的西沟函数里面写上delete 指针名字:整个程序就不能运行了,最关键的是我不太明白为什么程序要调用这两个函数,因为我感觉我都是动态对象啊.最后说明一下,我很菜,只有一点java编程的经验,所以第一次应对c++我真的需要一些帮助.谢谢你们了! 解决方

c语言-求能帮忙改改迷宫的代码

问题描述 求能帮忙改改迷宫的代码 走用链栈走迷宫,我的代码如下.一直调试不成功,希望能求救 #include #include #define M 10 #define N 10 typedef struct stack_type{ struct block_type *top; }stack_type; typedef struct block_type{ int i,j; int di; struct block_type *next; }block_type; int mg[M+1][N+

语言-新手求帮忙看下这段代码的数据溢出问题,没有C币理解下(我是在CodeBlock10.05下运行的)

问题描述 新手求帮忙看下这段代码的数据溢出问题,没有C币理解下(我是在CodeBlock10.05下运行的) #include #include #include unsigned int Ex_secret[30]={11,4,120,75,170,204,90,59,78,49,//用0~255的数表示8位2进制,一共240位 148,248,190,137,0,9,17,91,174,105,45,124,177,205,57,97,194,155,120,36}; unsigned in

mfc-WIN7上编译好的RELEASE MFC程序拿到WINXP系统上不能运行,求帮忙?

问题描述 WIN7上编译好的RELEASE MFC程序拿到WINXP系统上不能运行,求帮忙? 双击exe,会出现以上的错误,刚开始说缺少msvcr100.dll,我拷贝进去之后就出现以上的情况,于是调试,堆栈信息如下: 但是在我电脑上单步调试时直接从CWnd::CreateEx中的CreateWindowEx运行过去了,没有源代码,该函数在USER32.DLL中,这种情况怎么判断问题所在呢? 解决方案 与是否是 Win7 关系不大,主要是你的 WinXP 系统是不是没有安装开发环境?而你的运行,

c++-基于opencv的人脸识别,代码运行出错,求帮忙!

问题描述 基于opencv的人脸识别,代码运行出错,求帮忙! 代码显示是没有错的,运行显示有未处理的异常,求大神!急!!! 解决方案 求大神帮忙!如果熟悉C++语言的,再详谈啊!我在手机上不方便传代码! 解决方案二: #include #include #include using namespace std; void PrintMat(CvMat*); void FputMat(FILE , CvMat *); int main(int argc, char * argv[]) { /读入图

drawable-Android 求帮忙看看这段程序错在哪 在Android虚拟机上运行停止

问题描述 Android 求帮忙看看这段程序错在哪 在Android虚拟机上运行停止 package com.example.change1; import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOE

c语言-我的一段C语言程序在VC6.0上面运行不出来,貌似是头文件的问题,求帮忙

问题描述 我的一段C语言程序在VC6.0上面运行不出来,貌似是头文件的问题,求帮忙 #include"C:UsershpDesktopcomplex.h" main() { complex *a; int N=1024; //给a赋值 fft(a,N) } int fft(complex *a,int l) { const double pai=3.141592653589793; complex u,w,t; unsigned n=1,nv2,nm1,k,le,lei,ip; uns