V薇 2021-04-11 20:09 采纳率: 0%
浏览 162

java jframe的跳转问题

 

我把一个项目的几个功能分开编写,在整合时发现了以下问题,希望有大佬能帮帮忙!非常感谢每个朋友提出的一些意见,我都会去尝试!问题如下:

登录成功后,会跳转至一个功能界面,点击功能键后则会有相应的功能界面

问题:功能界面能打开“服务器”的功能窗口,但是里面的东西显示不出来,而且整个程序就死掉了(其他功能都能正常运行,对应的客户端也可以正常运行)

1、单独打开功能界面可以正常运行,如下图

2、功能选择界面入下图 

3、选择“聊天室开启”按钮后(此时整个程序都操作不了了)

跳转的代码如下

chatButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				
				new Servertest();
			}
});

服务器的完整代码如下

public class Servertest extends JFrame{
	
	private static final int PORT = 8888;
	private JTextArea Ta = new JTextArea();
	private JTextField tfField = new JTextField(20);
	private JButton sendButton = new JButton("发送");
	private JPanel btnTool = new JPanel();
	private JButton startBtn = new JButton("启动");
	private JButton stopBtn = new JButton("停止");
	private boolean isStart = false;
	private static ServerSocket ss = null;
	private Socket s = null;
	
	private DataInputStream dis = null;
	private DataOutputStream dos = null;
	
	private ArrayList<ClientConn> cclist = new ArrayList<ClientConn>();
	
	public Servertest() {
		
		this.setTitle("服务器端");
		this.add(Ta,BorderLayout.CENTER);
		
		Ta.setEditable(false);
		Ta.setOpaque(true);
		
		btnTool.add(tfField);
		btnTool.add(sendButton);
		btnTool.add(startBtn);
		btnTool.add(stopBtn);
		
		this.add(btnTool,BorderLayout.SOUTH);
		
		this.setBounds(0,0,500,500);
		
		
		Ta.append("请启动服务器"+"\n");
		
		this.addWindowListener(new WindowAdapter() {
			
			@Override
			public void windowClosing(WindowEvent e) {
				isStart = false;
				
				try {
					
					if(ss != null)
						ss.close();
					Ta.append("服务器端已关闭。。。");
					System.exit(0);
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			
		});
		
		startBtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				
				try {
					if (ss == null) {
						ss = new ServerSocket(PORT);
					}
					
					isStart = true;
					Ta.append("服务器已启动"+"\n");
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
			}
		});
		
		sendButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				String string = tfField.getText();
				
				sendmessage(string);
				
			}
		});
		
		
		this.setVisible(true);
		
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		
		System.out.println("完成1");
		try {
			start();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
	}
	

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new Servertest();

	}
	
	public void sendmessage(String string) {
		
		try {
			
			dos = new DataOutputStream(s.getOutputStream());
			tfField.setText("");
			Ta.append("我:"+ string + "\n");
			
			//遍历cclist    客户端上接收信息是多线程的接
			Iterator<ClientConn> it = cclist.iterator();
			while(it.hasNext()) {
				ClientConn o = it.next();

				System.out.println("发送至客户端" + o + ":" + string);
				o.send(ss.getInetAddress()+string+"\n");
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	//服务器的启动
	public void start() throws IOException{
		
		
		ss = new ServerSocket(PORT);
		isStart = true;
		
		//多个连接
		while (isStart) {
			
			//无法整合的问题所在
			
			s = ss.accept();
			cclist.add(new ClientConn(s));
			
			Ta.append("一个客户端连接服务器:" + s.getInetAddress() + "/" + s.getPort()+"\n");
			
			
		}
		
	}
	
	
	//服务器上的连接对象
	class ClientConn implements Runnable{
		
		Socket s = null;
		
		public ClientConn(Socket s) {
			
			this.s = s;
			
			(new Thread(this)).start();
			
		}
		
		public void run() {
			
			try {
				
				DataInputStream dis = new DataInputStream(s.getInputStream());
				//循环接受
				while (isStart) {
					
					//utf为原子操作
					String string = dis.readUTF();
					Ta.append(s.getInetAddress() + "|" + s.getPort() + "说:" + string + "\n");
					String strsend = (s.getInetAddress() + "|" + s.getPort() + "说:" + string + "\n");
					
					//遍历cclist    客户端上接收信息是多线程的接
					Iterator<ClientConn> it = cclist.iterator();
					while(it.hasNext()) {
						ClientConn o = it.next();

						o.send(strsend);
					}
					
				}
				
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
		//给每个用户发数据
		public void send(String str) {
			
			try {
				
				DataOutputStream dos = new DataOutputStream(this.s.getOutputStream());
				dos.writeUTF(str);
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}		
		}	
	}

}
  • 写回答

2条回答 默认 最新

  • 关注

    你这个程序有点问题,启动服务器要启动一个线程,否则就在死循环里面了,导致界面出不来。

    参考仿QQ即时通讯软件项目实训:https://edu.csdn.net/course/detail/3317

    评论

报告相同问题?

悬赏问题

  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi