关于eclipse运行和调试结果不同的问题

来源:互联网 发布:优美的句子 知乎 编辑:程序博客网 时间:2024/04/28 00:50
import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.io.*;import java.net.*;public class QQMain extends JFrame implements Runnable,WindowListener,ActionListener{private Socket s;public void setSocket(Socket s){this.s=s;Thread t=new Thread(this);t.start();}  JTextField txtMess=new JTextField();  JComboBox cmbUser=new JComboBox();  JTextArea txtContent=new JTextArea();QQMain(){  this.setSize(500,600);    //new    JButton btnSend=new JButton("发送");    JScrollPane spContent =new JScrollPane(txtContent);    JPanel a=new JPanel();  a.setLayout(new GridLayout(1,2));  a.add(cmbUser);  a.add(btnSend);    JPanel b=new JPanel();  b.setLayout(new GridLayout(2,1));  b.add(txtMess);  b.add(a);    this.setLayout(new BorderLayout());  this.add(b,BorderLayout.NORTH);  this.add(spContent,BorderLayout.CENTER);    btnSend.addActionListener(this);  this.addWindowListener(this);    try{  File f=new File("D:/work/QQ聊天记录.qq");  FileReader fr=new FileReader(f);  BufferedReader br=new BufferedReader(fr);  while(br.ready()){  txtContent.append(br.readLine()+"\n");  }  }catch(Exception e){}  }public void actionPerformed(ActionEvent arg0) {}public void windowActivated(WindowEvent arg0) {}public void windowClosed(WindowEvent arg0) {}public void windowClosing(WindowEvent arg0) {}public void windowDeactivated(WindowEvent arg0) {}public void windowDeiconified(WindowEvent arg0) {}public void windowIconified(WindowEvent arg0) {}public void windowOpened(WindowEvent arg0) {}public void run() {try {InputStream is=s.getInputStream();InputStreamReader isr=new InputStreamReader(is);BufferedReader br=new BufferedReader(isr);while(true){String message=br.readLine();String type=message.split("%")[0];String mess=message.split("%")[1];if(type.equals("add")){cmbUser.addItem(mess);}if(type.equals("exit")){cmbUser.removeItem(mess);}if(type.equals("mess")){txtContent.append(mess+"\n");}}} catch (IOException e) {e.printStackTrace();}}}__________________________________________import java.io.*;import java.net.*;import java.util.*;import java.sql.*;public class QQServer{public static void main(String[] args){try{ServerSocket ss=new ServerSocket(8000);HashMap<String,Socket> hm=new HashMap<String,Socket>();while(true){System.out.println("窗口8000正在等待...");Socket s=ss.accept();MyService t=new MyService();t.setSocket(s);t.setHashMap(hm);t.start();}}catch(Exception e){e.printStackTrace();}}}class MyService extends Thread{private Socket s;private HashMap<String,Socket> hm;public void setSocket(Socket s){this.s=s;}public void setHashMap(HashMap hm){this.hm=hm;}public void run(){try{InputStream is=s.getInputStream();InputStreamReader isr=new InputStreamReader(is);BufferedReader br=new BufferedReader(isr);OutputStream os=s.getOutputStream();OutputStreamWriter osw=new OutputStreamWriter(os);PrintWriter pw=new PrintWriter(osw,true);String uandp=br.readLine();String u=uandp.split("%")[0];String p=uandp.split("%")[1];Class.forName("com.mysql.jdbc.Driver");Connection cn=DriverManager.getConnection("jdbc:mysql://localhost:3306/qq","root","1234");PreparedStatement ps=cn.prepareStatement("select * from user where username=? and password=?");ps.setString(1, u);ps.setString(2, p);ResultSet rs=ps.executeQuery();if(rs.next()){pw.println("ok");//将别人的名字发送给自己for(String tu:hm.keySet()){pw.println("add%"+tu);}//将自己的名字发送给别人for(Socket ts:hm.values()){OutputStream tos=ts.getOutputStream();OutputStreamWriter tosw=new OutputStreamWriter(tos);PrintWriter tpw=new PrintWriter(tosw,true);tpw.println("add%"+u);}//将自己的存入HashMap中hm.put(u, s);}else{pw.println("err");}}catch(Exception e){}}}________________________________-import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.net.*;import java.io.*;public class QQLogin extends JFrame implements ActionListener{QQLogin(){this.setSize(250,125);JLabel lbUser=new JLabel("用户名");JLabel lbPass=new JLabel("密码");JButton btnLogin =new JButton("登录");JButton btnReg=new JButton("注册");JButton btnCancel=new JButton("取消");JPanel a=new JPanel();a.setLayout(new GridLayout(2,2));a.add(lbUser);a.add(txtUser);a.add(lbPass);a.add(txtPass);JPanel b=new JPanel();b.setLayout(new FlowLayout());b.add(btnLogin);b.add(btnReg);b.add(btnCancel);this.setLayout(new BorderLayout());this.add(a,BorderLayout.NORTH);this.add(b, BorderLayout.CENTER);btnLogin.addActionListener(this);btnReg.addActionListener(this);btnCancel.addActionListener(this);}JTextField txtUser=new JTextField();JPasswordField txtPass=new JPasswordField();public static void main(String[] args){QQLogin w=new QQLogin();w.setVisible(true);}public void actionPerformed(ActionEvent e) {if(e.getActionCommand().equals("登录")){try{Socket s=new Socket("localhost",8000);OutputStream os=s.getOutputStream();OutputStreamWriter osw=new OutputStreamWriter(os);PrintWriter pw=new PrintWriter(osw,true);pw.println(txtUser.getText()+"%"+txtPass.getText());InputStream is=s.getInputStream();InputStreamReader isr=new InputStreamReader(is);BufferedReader br=new BufferedReader(isr);String message=br.readLine();if(message.equals("ok")){this.setVisible(false);QQMain w=new QQMain();w.setVisible(true);w.setSocket(s);}else{JOptionPane.showMessageDialog(this, "对不起,验证失败");}}catch(Exception e1){e1.printStackTrace();}}}}————————————————————————————————-下拉框显示已登录的用户,思路就是把已经登录的用户和他们的Socket存入HashMap中,问题在于调试的时候可以正确显示,而按运行的时候却总是会下拉框显示缺一个用户名或者二个。。。调试的时候我看了过程,每步都是正确的,然后下拉框显示也会正确显示。。我的理解是一个Socket一个线程,Socket里存了什么东西不懂,反正每个都是不一样的,然后可以通过不同的Socket来getInputStream()或者getOutputStream(),使不同线程都可以接收到信息。说的逻辑有点乱,总之大概就是调试时能够显示下拉框,运行时不能正确显示。。
                                             
0 0
原创粉丝点击