GUI添加了组件但空白帧显示

时间:2016-07-31 11:57:58

标签: java swing sockets

当我按下Jbutton并调用serverChatform方法时,会显示一个空白帧。但是,当我单独运行serverChatform时,它会显示所有GUI组件。

private void sendActionPerformed(java.awt.event.ActionEvent evt) { 
this.dispose();
try {
serverChatform serverChatform= new serverChatform(); 
serverChatform.setVisible(true);
} catch (IOException ex) {
Logger.getLogger(home.class.getName()).log(Level.SEVERE, null, ex);
}
}

serverChatform.java(此代码在单独运行时运行,但在从另一个类方法调用时不运行)

package chichat;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class serverChatform extends JFrame implements ActionListener {
static ServerSocket server;
static Socket conn;
JPanel panel;
JTextField NewMsg;
JTextArea ChatHistory;
JButton Send;
DataInputStream dis;
DataOutputStream dos;
public serverChatform() throws UnknownHostException, IOException {
panel = new JPanel(); 
NewMsg = new JTextField();
ChatHistory = new JTextArea();
Send = new JButton("Send");
this.setSize(500, 500);
this.setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
panel.setLayout(null);
this.add(panel);
System.out.println("ksjfsdk");
ChatHistory.setBounds(20, 20, 450, 360);
panel.add(ChatHistory);
NewMsg.setBounds(20, 400, 340, 30);
panel.add(NewMsg);
Send.setBounds(375, 400, 95, 30);
panel.add(Send);
this.setTitle("Server");
ChatHistory.setVisible(true);
panel.setVisible(true);
Send.addActionListener(this);
//server = new ServerSocket(2000, 1 , InetAddress.getLocalHost());
server=new ServerSocket(2000,1);
ChatHistory.setText("Waiting for Client");
conn = server.accept(); 
System.out.println(conn);
ChatHistory.setText(ChatHistory.getText() + 'n' + "Client Found");
while (true) {
try {
DataInputStream dis = new DataInputStream(conn.getInputStream());
String string = dis.readUTF();
ChatHistory.setText(ChatHistory.getText() + 'n' + "Client:"
+ string);
} catch (Exception e1) {
ChatHistory.setText(ChatHistory.getText() + 'n'
+ "Message sending fail:Network Error");
try {
Thread.sleep(3000);
System.exit(0);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if ((e.getSource() == Send) && (NewMsg.getText() != "")) {
ChatHistory.setText(ChatHistory.getText() + 'n' + "ME:"
+ NewMsg.getText());
try {
DataOutputStream dos = new DataOutputStream(
conn.getOutputStream());
dos.writeUTF(NewMsg.getText());
} catch (Exception e1) {
try {
Thread.sleep(3000);
System.exit(0);
} catch (InterruptedException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
}
NewMsg.setText("");
}
}
public static void main(String[] args) throws UnknownHostException,
IOException {
new serverChatform();
}
}

serverChatForm显示所有gui组件,同时separetly运行该文件。

0 个答案:

没有答案