首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

求高手帮忙看下代码,错哪了

2013-04-05 
求高手帮忙看下代码,哪里错了问题都写在注释里了,请高手帮忙看下import java.awt.*import java.awt.event

求高手帮忙看下代码,哪里错了
问题都写在注释里了,请高手帮忙看下


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


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class WindowDemo extends javax.swing.JFrame {
private JPanel jPanel1;
private JLabel username;
public JPasswordField passText;
private JDialog jDialog1;
private JButton submit;
private JLabel password;
public JTextField userText;

/**
* Auto-generated main method to display this JFrame
 * @throws Exception 
*/
public static void main(String[] args) throws Exception {

SwingUtilities.invokeLater(new Runnable() {
public void run() {
WindowDemo inst = new WindowDemo();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
new socketServers();
}

public WindowDemo() {
super();
initGUI();
}

private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
getContentPane().setLayout(null);
{
jPanel1 = new JPanel();
jPanel1.setLayout(null);
getContentPane().add(jPanel1);
jPanel1.setBounds(0, 0, 392, 266);
{
username = new JLabel();
jPanel1.add(username);
username.setText("\u8d26\u53f7:");
username.setBounds(73, 53, 32, 15);
}
{
userText = new JTextField(83);
jPanel1.add(userText);
userText.setBounds(123, 50, 175, 22);

}
{
password = new JLabel();
jPanel1.add(password);
password.setText("\u5bc6\u7801:");
password.setBounds(73, 102, 30, 15);
}
{
passText = new JPasswordField();
jPanel1.add(passText);
passText.setBounds(121, 99, 177, 22);
}
{
submit = new JButton();
jPanel1.add(submit);
submit.setText("\u767b\u5f55");
submit.setBounds(150, 176, 83, 22);
submit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
System.out.println(userText.getText());//这里可以正常获取到数据

try {
new socketsendss();
} catch (Exception e) {


throw new RuntimeException("啊哦,错啦");
}
}
});
}
}
pack();
setSize(400, 300);
} catch (Exception e) {
throw new RuntimeException("发生错误啦");
}
}
}


class Dialogs{
private JDialog dia;
private String info;
private String title;
private JLabel latxt;
private JButton bt;
Dialogs(String info,String title){
this.info=info;
this.title=title;
}

public void showDialog(){
dia =new JDialog();
dia.setBounds(300, 300, 200, 100);
dia.setTitle(this.title);
dia.setLayout(null);

latxt=new JLabel();
latxt.setText(this.info);
latxt.setBounds(50, 0, 100, 30);

bt=new JButton();
bt.setText("确定");
bt.setBounds(50, 30, 80, 30);


dia.add(latxt);
dia.add(bt);
dia.setVisible(true);
{
bt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
dia.dispose();
}
});
}
}
}


class socketsendss{
socketsendss() throws Exception{
String usernames=new WindowDemo().userText.getText();//这里无法获取到值
String passwords=new String(new WindowDemo().passText.getPassword());//这里无法获取到值
String sendStr=usernames+"&"+passwords;
System.out.println("需要发送的数据:"+usernames);
Socket s=new Socket("192.168.1.126",10131);
OutputStream out=s.getOutputStream();
out.write(sendStr.getBytes());
InputStream in=s.getInputStream();
byte[] by=new byte[1024];
int len=in.read(by);
String str=new String(by,0,len);
if(str.equals("1")){
Dialogs da=new Dialogs("正确","你提交的数据对的");
da.showDialog();
}else{
Dialogs da=new Dialogs("错啦","你提交的数据错啦");
da.showDialog();
}

s.close();
}
}

class socketServers{
socketServers()throws Exception{
ServerSocket ss=new ServerSocket(10131);
while(true){
Socket s=ss.accept();
InputStream in=s.getInputStream();
byte[] by=new byte[1024];
int len=in.read(by);
String str=new String(by,0,len);
System.out.println(str);
String[] strarr=str.split("&");

System.out.println("接收到的值:"+strarr[0]+"---"+strarr[1]);
OutputStream out=s.getOutputStream();
if(strarr[0].equals("admin")){
out.write("1".getBytes());
}else{
out.write("0".getBytes());
}
}
//s.close();//这2行 为啥不能写呢,一写都错
//ss.close();
}
}


[解决办法]
package fortest;

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

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.


 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class WindowDemo extends javax.swing.JFrame {
private JPanel jPanel1;
private JLabel username;
public JPasswordField passText;
private JDialog jDialog1;
private JButton submit;
private JLabel password;
public JTextField userText;

/**
 * Auto-generated main method to display this JFrame
 * 
 * @throws Exception
 */
public static void main(String[] args) throws Exception {

SwingUtilities.invokeLater(new Runnable() {
public void run() {
WindowDemo inst = new WindowDemo();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
new socketServers();
}

public WindowDemo() {
super();
initGUI();
}

private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
getContentPane().setLayout(null);
{
jPanel1 = new JPanel();
jPanel1.setLayout(null);
getContentPane().add(jPanel1);
jPanel1.setBounds(0, 0, 392, 266);
{
username = new JLabel();
jPanel1.add(username);
username.setText("\u8d26\u53f7:");
username.setBounds(73, 53, 32, 15);
}
{
userText = new JTextField(83);
jPanel1.add(userText);
userText.setBounds(123, 50, 175, 22);

}
{
password = new JLabel();
jPanel1.add(password);
password.setText("\u5bc6\u7801:");
password.setBounds(73, 102, 30, 15);
}
{
passText = new JPasswordField();
jPanel1.add(passText);
passText.setBounds(121, 99, 177, 22);
}
{
submit = new JButton();
jPanel1.add(submit);
submit.setText("\u767b\u5f55");
submit.setBounds(150, 176, 83, 22);
submit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
System.out.println(userText.getText());// 这里可以正常获取到数据

try {
new socketsendss(userText.getText(), passText
.getPassword());
} catch (Exception e) {
throw new RuntimeException("啊哦,错啦");
}
}
});
}
}
pack();
setSize(400, 300);
} catch (Exception e) {
throw new RuntimeException("发生错误啦");
}
}
}

class Dialogs {
private JDialog dia;
private String info;
private String title;
private JLabel latxt;
private JButton bt;

Dialogs(String info, String title) {
this.info = info;
this.title = title;


}

public void showDialog() {
dia = new JDialog();
dia.setBounds(300, 300, 200, 100);
dia.setTitle(this.title);
dia.setLayout(null);

latxt = new JLabel();
latxt.setText(this.info);
latxt.setBounds(50, 0, 100, 30);

bt = new JButton();
bt.setText("确定");
bt.setBounds(50, 30, 80, 30);

dia.add(latxt);
dia.add(bt);
dia.setVisible(true);
{
bt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
dia.dispose();
}
});
}
}
}

class socketsendss {
socketsendss(String userText, char[] passText) throws Exception {
String usernames = userText;// 这里无法获取到值
String passwords = new String(passText);// 这里无法获取到值
String sendStr = usernames + "&" + passwords;
System.out.println("需要发送的数据:" + usernames);
Socket s = new Socket("192.168.0.105", 10131);
OutputStream out = s.getOutputStream();
out.write(sendStr.getBytes());
InputStream in = s.getInputStream();
byte[] by = new byte[1024];
int len = in.read(by);
String str = new String(by, 0, len);
if (str.equals("1")) {
Dialogs da = new Dialogs("正确", "你提交的数据对的");
da.showDialog();
} else {
Dialogs da = new Dialogs("错啦", "你提交的数据错啦");
da.showDialog();
}

s.close();
}
}

class socketServers {
socketServers() throws Exception {
ServerSocket ss = new ServerSocket(10131);

Socket s = null;
InputStream in = null;
while (true) {
s = ss.accept();
in = s.getInputStream();
byte[] by = new byte[1024];
int len = in.read(by);
String str = new String(by, 0, len);
System.out.println(str);
String[] strarr = str.split("&");
if (strarr[0].equals("bye")) {// 在用户名处输入“bye”视为关闭操作,当然只是举个例子,什么时候关闭要看你的需求
System.out.println("客户端已关闭!服务器即将关闭");
break;
}
System.out.println("接收到的值:" + strarr[0] + "---" + strarr[1]);
OutputStream out = s.getOutputStream();
if (strarr[0].equals("admin")) {
out.write("1".getBytes());
} else {
out.write("0".getBytes());
}
}
s.close();// 这2行 为啥不能写呢,一写都错
ss.close();
}
}

热点排行