为什么当我点击Enter my application冻结时执行我的程序?
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import java.io.*;
import java.net.*;
public class KnockKnockUI{
private Socket kkSocket = null;
private PrintWriter out = null;
private BufferedReader in = null;
JTextArea clientArea;
JTextArea serverArea;
public KnockKnockUI(){
establishConnection();
initComponents();
}
public void initComponents(){
JFrame frame = new JFrame("Chat");
frame.setVisible(true);
frame.setSize(320,189);
frame.getContentPane().setLayout(null);
serverArea = new JTextArea();
serverArea.setLineWrap(true);
serverArea.setBounds(10, 11, 284, 45);
frame.getContentPane().add(serverArea);
clientArea = new JTextArea();
clientArea.setBounds(10, 67, 284, 46);
frame.getContentPane().add(clientArea);
JButton btnEnter = new JButton("Enter");
btnEnter.setBounds(96, 124, 89, 23);
frame.getContentPane().add(btnEnter);
btnEnter.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try{
in.readLine();
}catch(Exception e){
}
String fromUser = clientArea.getText();
try {
fromUser = in.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
serverArea.setText(fromUser);
}
});
}
public void establishConnection(){
//Establishes Connection
try {
kkSocket = new Socket("localhost", 4444);
//
out = new PrintWriter(kkSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: taranis.");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to: taranis.");
System.exit(1);
}
}
public static void main(String[] args){
KnockKnockUI ui = new KnockKnockUI();
}
}
这是一个使用Java的简单聊天客户端,还有其他我没有包含的类。比如Server类和Protocol类
答案 0 :(得分:11)
当您按Enter键时,将执行以下操作:
try {
in.readLine();
} catch (Exception e) {
}
String fromUser = clientArea.getText();
try {
fromUser = in.readLine();
} catch (IOException e) {
e.printStackTrace();
}
serverArea.setText(fromUser);
这个代码块正在Event Dispatch Thread上执行,它处理所有 Swing事件。您对in.readLine()
的来电将会阻止,直到您的Socket
输入一行,导致未处理的事件进行备份,从而导致您的程序无响应。如果您想执行耗时的任务,请查看SwingWorker
(SwingWorker
tutorial)。
旁注:在不打印堆栈跟踪的情况下捕获Exception
几乎总是一个坏主意。如果不是不可能的话,它可能会使您的代码难以捕获。
答案 1 :(得分:0)
下面框架的setSize表示添加所有组件后
public void initComponents(){
JFrame frame = new JFrame("Chat");
frame.getContentPane().setLayout(null);
serverArea = new JTextArea();
serverArea.setLineWrap(true);
serverArea.setBounds(10, 11, 284, 45);
frame.getContentPane().add(serverArea);
clientArea = new JTextArea();
clientArea.setBounds(10, 67, 284, 46);
frame.getContentPane().add(clientArea);
JButton btnEnter = new JButton("Enter");
btnEnter.setBounds(96, 124, 89, 23);
frame.getContentPane().add(btnEnter);
btnEnter.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try{
in.readLine();
}catch(Exception e){
}
String fromUser = clientArea.getText();
try {
fromUser = in.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
serverArea.setText(fromUser);
}
});
frame.setVisible(true);
frame.setSize(320,189);
}