问题都写在注释里了,请参考注释
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
public class WindowDemo extends javax.swing.JFrame {
private JPanel jPanel1;
private JLabel username;
private JPasswordField passText;
private JDialog jDialog1;
private JButton submit;
private JLabel password;
private JTextField userText; /**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
WindowDemo inst = new WindowDemo();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}
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("准备实体化方法");
new socketsendss();//代码运行到这一步就不能运行了,也就是不能实例化这个类,如果单独运行这个类,是正常的
}
});
}
}
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{
public static void main(String[] args) throws Exception{
System.out.println("已经进入socketsend方法");
Socket s=new Socket("192.168.1.126",10010);
OutputStream out=s.getOutputStream();
out.write("data run...".getBytes());//这里发送了一个数组对应下面
System.out.println("已经发送");
InputStream in=s.getInputStream();
byte[] by=new byte[1024];
int len=in.read(by);
String str=new String(by,0,len);
if(str=="1"){//如果返回过来的数据是1,执行这一步,我明面发送的是这个字符串,但是为啥不执行这步却执行了else呢
Dialogs da=new Dialogs("正确","你提交的数据对的");
da.showDialog();
}else{
Dialogs da=new Dialogs("错啦","你提交的数据错啦");
da.showDialog();
}
s.close();
}
}class socketServers{
public static void main(String[] args) throws Exception{
ServerSocket ss=new ServerSocket(10010);
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);
OutputStream out=s.getOutputStream();
if(str=="data run..."){//如果接受的数据和现在这个相等,返回1,否则0
out.write("1".getBytes());
}else{
out.write("0".getBytes());
}
s.close();
ss.close();
}
}
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
public class WindowDemo extends javax.swing.JFrame {
private JPanel jPanel1;
private JLabel username;
private JPasswordField passText;
private JDialog jDialog1;
private JButton submit;
private JLabel password;
private JTextField userText; /**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
WindowDemo inst = new WindowDemo();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}
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("准备实体化方法");
new socketsendss();//代码运行到这一步就不能运行了,也就是不能实例化这个类,如果单独运行这个类,是正常的
}
});
}
}
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{
public static void main(String[] args) throws Exception{
System.out.println("已经进入socketsend方法");
Socket s=new Socket("192.168.1.126",10010);
OutputStream out=s.getOutputStream();
out.write("data run...".getBytes());//这里发送了一个数组对应下面
System.out.println("已经发送");
InputStream in=s.getInputStream();
byte[] by=new byte[1024];
int len=in.read(by);
String str=new String(by,0,len);
if(str=="1"){//如果返回过来的数据是1,执行这一步,我明面发送的是这个字符串,但是为啥不执行这步却执行了else呢
Dialogs da=new Dialogs("正确","你提交的数据对的");
da.showDialog();
}else{
Dialogs da=new Dialogs("错啦","你提交的数据错啦");
da.showDialog();
}
s.close();
}
}class socketServers{
public static void main(String[] args) throws Exception{
ServerSocket ss=new ServerSocket(10010);
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);
OutputStream out=s.getOutputStream();
if(str=="data run..."){//如果接受的数据和现在这个相等,返回1,否则0
out.write("1".getBytes());
}else{
out.write("0".getBytes());
}
s.close();
ss.close();
}
}
下面在system.out.println("");一下你就知道有没有运行这上一行了.
new socketsendss();这一句代码的意义何在? socketsendss 默认的构造方法.什么都没有做?运行了。也看不到任何提示.
if(str.equals("1")){//如果返回过来的数据是1,执行这一步,我明面发送的是这个字符串,但是为啥不执行这步却执行了else呢
还有下面的
if(str.equals("data run...")){//如果接受的数据和现在这个相等,返回1,否则0
package fortest;import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
public class WindowDemo extends javax.swing.JFrame {
private JPanel jPanel1;
private JLabel username;
private JPasswordField passText;
private JDialog jDialog1;
private JButton submit;
private JLabel password;
private JTextField userText; /**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
WindowDemo inst = new WindowDemo();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}
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("准备实体化方法");
try {
new Socketsendss();
} catch (Exception e) {
System.out.println("new Socketsendss()出错");
}//代码运行到这一步就不能运行了,也就是不能实例化这个类,如果单独运行这个类,是正常的
}
});
}
}
pack();
setSize(400, 300);
} catch (Exception e) {
throw new RuntimeException("发生错误啦");
}
}
}class Socketsendss{
public Socketsendss() throws Exception{
this.init();
}
public void init() throws Exception{
System.out.println("已经进入socketsend方法");
Socket s=new Socket("192.168.1.126",10010);
OutputStream out=s.getOutputStream();
out.write("data run...".getBytes());//这里发送了一个数组对应下面
System.out.println("已经发送");
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")){//如果返回过来的数据是1,执行这一步,我明面发送的是这个字符串,但是为啥不执行这步却执行了else呢
Dialogs da=new Dialogs("正确","你提交的数据对的");
da.showDialog();
}else{
Dialogs da=new Dialogs("错啦","你提交的数据错啦");
da.showDialog();
}
s.close();
}
}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 SocketServers{
public SocketServers() throws Exception{
this.init();
}
public void init() throws Exception{
ServerSocket ss=new ServerSocket(10010);
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);
OutputStream out=s.getOutputStream();
if(str.equals("data run...")){//如果接受的数据和现在这个相等,返回1,否则0
out.write("1".getBytes());
}else{
out.write("0".getBytes());
}
s.close();
ss.close();
}
}
在main方法内执行加上new Thread(new Runnable() {
@Override
public void run() {
try {
new SocketServers();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();