package gc;
import static gc.Login.pass;
import static gc.Login.uname;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
public final class Mymain extends javax.swing.JFrame {
public static String name = "";
public String msg;
XMPPConnection connection;
public Mymain() throws NullPointerException,IOException,XMPPException {
initComponents();
welcome.setText(name);
}
public Mymain(String username,String password) throws IOException,XMPPException,NullPointerException, InterruptedException, InvocationTargetException {
ConnectionConfiguration config = new ConnectionConfiguration("talk.google.com",5222, "gmail.com");
connection = new XMPPConnection(config);
connection.connect();
connection.login(username,password);
name = username;
Mymain setter = new Mymain();
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
new Mymain().setVisible(true);
} catch (NullPointerException | IOException | XMPPException ex) {
Logger.getLogger(Mymain.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
begin_chat();
}
public void begin_chat() throws IOException, XMPPException,NullPointerException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Chat chat = connection.getChatManager().createChat("rssrivandla@gmail.com", new MessageListener() {
@Override
public void processMessage(Chat chat, Message msg) {
if(msg.getType() == Message.Type.chat)
if (msg != null && msg.getBody() != null)
System.out.println(chat.getParticipant() + " says: "
+ (msg != null ? msg.getBody() : "NULL"));
}
});
while((msg = br.readLine())!=null) {
chat.sendMessage(msg);
}
if(msg.equals("bye"))
{
connection.disconnect();
}
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel2 = new javax.swing.JLabel();
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
welcome = new javax.swing.JLabel();
jSeparator1 = new javax.swing.JSeparator();
jLabel3 = new javax.swing.JLabel();
jLabel2.setText("jLabel2");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Ch@t Me!");
setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
setResizable(false);
jPanel1.setBackground(new java.awt.Color(0, 255, 204));
jLabel1.setFont(new java.awt.Font("Comic Sans MS", 1, 18)); // NOI18N
jLabel1.setText("Heya,");
welcome.setFont(new java.awt.Font("Calibri", 1, 18)); // NOI18N
jSeparator1.setBackground(new java.awt.Color(0, 0, 0));
jSeparator1.setForeground(new java.awt.Color(0, 0, 0));
jLabel3.setFont(new java.awt.Font("Comic Sans MS", 1, 18)); // NOI18N
jLabel3.setText("Buddies Online");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jSeparator1)
.addContainerGap())
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addGap(0, 61, Short.MAX_VALUE)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(welcome, javax.swing.GroupLayout.PREFERRED_SIZE, 389, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(67, 67, 67))))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(57, 57, 57)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(398, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap(19, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(welcome, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(293, 293, 293))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
pack();
}// </editor-fold>
public static void main(String args[]) {
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JPanel jPanel1;
private javax.swing.JSeparator jSeparator1;
public static javax.swing.JLabel welcome;
// End of variables declaration
}
为什么即使调用了initComponents(),帧也没有被打开?当我从参数化构造函数中删除begin_chat()时,框架正在显示...我需要框架与begin_chat函数一起显示...所以,任何人都会帮助我..
答案 0 :(得分:1)
当我从参数化构造函数中删除begin_chat()时,框架正在显示...
听起来你的聊天代码阻止了事件调度线程(EDT)。您需要此代码在单独的线程中执行,以便GUI可以自由重新绘制并响应用户事件。
阅读Concurrency上Swing教程中的部分以获取更多信息。您可能会使用SwingWorker来处理聊天代码。