问题1:程序打开原样是图1的样子,什么都不显示,只有把程序最大化,然后最小化才恢复正常,请问是什么原样
问题2:BufferedReader缓冲区和BufferedWriter缓冲区的readLine()方法是阻塞方法?如果发送端没有发送结束标记,接收端是不是就一直停在readLine方法
问题3:
Client 类用来从文本框里读取字符串传输,文本区域没有作用
Server 类用来接收Client发送的数据,界面都是一样的,只接收没有发送功能
class WindowByClient extends JFrame implements ActionListener{
JButton send;
JTextField text;
JTextArea area;
Socket client;
BufferedWriter out;
WindowByClient(){
super("发送程序");
this.setLayout(new FlowLayout());
this.setSize(300,300);
this.setLocationRelativeTo(null);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
init();
}
void init(){
send = new JButton("发送");
text=new JTextField(10);
area=new JTextArea(9,16);
JScrollPane jsp=new JScrollPane(area);
area.setEditable(false);
this.add(send);
this.add(text);
this.add(jsp);
try {
client=new Socket("127.0.0.1",9999);
out=new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
} catch (IOException e) {}
send.addActionListener(this);
}
void sendMethod(){
try {
out.write(text.getText());
out.flush();
} catch (IOException e) {}
}
public void actionPerformed(ActionEvent arg) {
if(arg.getSource()==send){
sendMethod();
}
}
}
public class Client {
public static void main(String[] args) {
new WindowByClient();
}
}
class WindowByServer extends JFrame{
JButton send;
JTextField text;
JTextArea area;
ServerSocket server;
Socket s;
BufferedReader in;
WindowByServer(){
super("发送程序");
this.setLayout(new FlowLayout());
this.setSize(300,300);
this.setLocationRelativeTo(null);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
init();
}
void init(){
send = new JButton("发送");
text=new JTextField(10);
area=new JTextArea(9,16);
JScrollPane jsp=new JScrollPane(area);
area.setEditable(false);
this.add(send);
this.add(text);
this.add(jsp);
try {
server=new ServerSocket();
s=server.accept();
in=new BufferedReader(new InputStreamReader(s.getInputStream()));
} catch (IOException e) {}
receiveMethod();
}
void receiveMethod(){
String str=null;
try {
while(true){
while((str=in.readLine())!=null){
if(!str.equals(null)){
area.append(str);
}
}
}
} catch (IOException e) {}
}
}
public class Server {
public static void main(String[] args) {
new WindowByServer();
}
}
Exception in thread "main" java.lang.NullPointerException
at Demo_1.WindowByServer.receiveMethod(Server.java:58)
at Demo_1.WindowByServer.init(Server.java:52)
at Demo_1.WindowByServer.<init>(Server.java:32)
at Demo_1.Server.main(Server.java:69)
请问 while((str=in.readLine())!=null) 这句话为什么会报错