本文介绍了界面未加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hai,我正在尝试使用Nio通道在核心Java中创建一个聊天程序,其中服务器一次响应多个客户端.我创建了3个类,即client_login,client_form和nioserver.但是问题是,如果用户成功登录了用于聊天的框架,但是我创建的发送和退出按钮没有出现在框架中,并且无法关闭框架.这是我的client_form文件.
Hai,I am trying to create a chat program in core java using nio channels in which the server responds multiple clients at a time.I created 3 clases namely client_login,client_form and nioserver.But the problem is that if the the user login sucessfully the frame for chating apperes,but the send and exit button that i am created is not appeares in the frame as well as it is not possible to close the frame.here is my client_form file.
/**
*
* @author appuraj.kr
*/
public class Client_form extends javax.swing.JFrame {
/**
* Creates new form
*/
public Client_form(String uname,String hostname) throws IOException
/*initialise here*/
{
super(uname);
this.uname=uname;
this.hostname=hostname;
//setTitle(uname);
System.out.println(uname+""+hostname);
socketAddress = new InetSocketAddress(hostname,7789);
charset = Charset.forName("ISO-8859-1");
decoder = charset.newDecoder();
encoder = charset.newEncoder();
buffer = ByteBuffer.allocateDirect(1024);
buffer1 = ByteBuffer.allocateDirect(1024);
charBuffer = CharBuffer.allocate(1024);
channel = SocketChannel.open();
channel.configureBlocking(false);
channel.connect(socketAddress);
// Open Selector
selector = Selector.open();
// Register interest in when connection90
channel.register(selector, SelectionKey.OP_CONNECT | SelectionKey.OP_WRITE|SelectionKey.OP_READ);
setVisible(true);
initComponents();
// client_start();
setVisible(true);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
public void client_start(){
String line=null;
try {
while (selector.select(500) > 0)
{
// Get set of ready objects
Set readyKeys = selector.selectedKeys();
Iterator readyItor = readyKeys.iterator();
// Walk through set
while (readyItor.hasNext())
{
// Get key from set
SelectionKey key = (SelectionKey)readyItor.next();
// Remove current entry
readyItor.remove();
// Get channel
SocketChannel keyChannel = (SocketChannel)key.channel();
if (key.isConnectable())
{
//System.out.println(uname);
// Finish connection
System.out.println("server foundllllllllllllll");
System.out.println("connected");
//System.out.println("user name is"+uname);
if (keyChannel.isConnectionPending())
{
keyChannel.finishConnect();
}
buffer1.clear();
System.out.println("username"+uname);
buffer1=ByteBuffer.wrap(new String(uname).getBytes());
keyChannel.write(buffer1);
buffer1.clear();
try
{
Thread.sleep(1000);
buffer1.clear();
charBuffer.clear();
keyChannel.read(buffer1);
buffer1.flip();
decoder.decode(buffer1, charBuffer, false);
charBuffer.flip();
String data=charBuffer.toString();
System.out.println("inside readmmmmmmmmmmm"+data);
System.out.println("before passing appendtext"+data);
tamsg.append("hai "+data+ "\n");
buffer1.clear();
charBuffer.clear();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
} /* while(true)
{
line = br.readLine();
taMessages.append(line + "\n");
} // end of while */
} catch(Exception ex) {}
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
tamsg = new java.awt.TextArea();
tinput = new javax.swing.JTextField();
btnsnd = new javax.swing.JButton();
btnexit = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
tamsg.setEditable(false);
btnsnd.setText("Send");
btnsnd.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnsndActionPerformed(evt);
}
});
btnexit.setText("Exit");
btnexit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnexitActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(tamsg, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(tinput, javax.swing.GroupLayout.PREFERRED_SIZE, 266, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(btnsnd)
.addGap(26, 26, 26)
.addComponent(btnexit)
.addGap(0, 98, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(tamsg, javax.swing.GroupLayout.PREFERRED_SIZE, 239, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(tinput, javax.swing.GroupLayout.DEFAULT_SIZE, 40, Short.MAX_VALUE)
.addComponent(btnsnd)
.addComponent(btnexit))
.addContainerGap())
);
pack();
}// </editor-fold>
private void btnsndActionPerformed(java.awt.event.ActionEvent evt) {
String line=null;
String data=null;
line=tinput.getText();
buffer1.clear();
buffer1=ByteBuffer.wrap(new String(tinput.getText()).getBytes());
try
{
channel.write(buffer1);
tamsg.append(uname+": "+line+ "\n");
buffer1.clear();
channel.read(buffer1);
buffer1.flip();
buffer1.rewind();
//buffer1.flip();
buffer1.clear();
decoder.decode(buffer1, charBuffer, false);
charBuffer.flip();
data=charBuffer.toString();
System.out.println("server"+data);
tamsg.append("server:"+data+ "\n");
charBuffer.clear();
buffer1.clear();
}
catch (IOException ex)
{
Logger.getLogger(ChatClient.class.getName()).log(Level.SEVERE, null, ex);
}
tinput.setText("");
buffer1.clear();//add your handling code here:
}
private void btnexitActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0); // TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
// Variables declaration - do not modify
private javax.swing.JButton btnexit;
private javax.swing.JButton btnsnd;
private java.awt.TextArea tamsg;
private javax.swing.JTextField tinput;
// End of variables declaration
}
推荐答案
这篇关于界面未加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!