简体   繁体   English

摆动输出在第一次运行时不会出现

[英]swing output doesn't turn up at first run

I am trying to perform a simple client server communication using swing in netbeans.First time when I run, it shows a blank window. 我正在尝试使用netbeans中的swing执行简单的客户端服务器通信。第一次运行时,它显示一个空白窗口。 I again run it without closing the first output window, it shows the desired output. 我再次运行它而没有关闭第一个输出窗口,它显示了所需的输出。 I am attaching the code. 我附上了代码。

Thanks 谢谢

import java.io.*;
import java.net.*;

public class serverg extends javax.swing.JFrame {

ServerSocket ss;
Socket s;
BufferedReader in;
PrintWriter out;

public serverg() {
    initComponents();       
}

 void work() {
    try {

        String line;
        ss = new ServerSocket(3500);
        s=new Socket();
        jTextArea1.append("waiting for connection");
        s=ss.accept();
        in = new BufferedReader(new InputStreamReader(s.getInputStream()));
        out = new PrintWriter(s.getOutputStream());
        out.print("connection established");
        if((line=in.readLine())!=null)
        jTextArea1.append("Client : " + line );

    } catch (IOException r) {
    }

}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jScrollPane1 = new javax.swing.JScrollPane();
    jTextArea1 = new javax.swing.JTextArea();
    jTextField1 = new javax.swing.JTextField();
    jButton1 = new javax.swing.JButton();
    jLabel1 = new javax.swing.JLabel();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setName("Form"); // NOI18N

    jScrollPane1.setName("jScrollPane1"); // NOI18N

    /*try{
        jTextArea1.setEditable(false);
        //s=new Socket();
        jTextArea1.append("waiting for client..." );
        s=ss.accept();
        String line=in.readLine();
        jTextArea1.append("Client : " + line );
    }
    catch(IOException r){}*/
    jTextArea1.setColumns(20);
    jTextArea1.setRows(5);
    jTextArea1.setName("jTextArea1"); // NOI18N
    jScrollPane1.setViewportView(jTextArea1);

    jTextField1.setName("jTextField1"); // NOI18N

    org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(desktopapplication1.DesktopApplication1.class).getContext().getResourceMap(serverg.class);
    jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
    jButton1.setName("jButton1"); // NOI18N
    jButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(evt);
        }
    });

    jLabel1.setName("jLabel1"); // NOI18N

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                .addGroup(layout.createSequentialGroup()
                    .addGap(10, 10, 10)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(98, 98, 98)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)))
            .addContainerGap())
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addContainerGap())
    );

    pack();
}// </editor-fold>                        

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    String line = jTextField1.getText();
    out.println(line);
    jTextArea1.append("\nServer : " + line);
}                                        

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {

    java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
            serverg ob=new serverg();                
            ob.setVisible(true);                      
            ob. jTextArea1.append("waiting for connection"  );
            ob.work();              
        }
    });

}
// Variables declaration - do not modify                     
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private static javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField jTextField1;
// End of variables declaration                   
}

Your work() method is not a part of your Swing Application , hence your main() method is wrong, simply change your work() method to this, so that both the processes can run on their respective threads, by writing s=ss.accept(); 你的work()方法不是你的Swing Application的一部分,因此你的main()方法是错误的,只需将你的work()方法改为this,这样两个进程就可以在各自的线程上运行,写s=ss.accept(); , you blocked your Event Dispatcher Thread. ,您阻止了您的事件调度程序线程。

Moreover, what ever changes you wanted to make to the GUI must be done on the Event Dispatcher Thread, so you need to change your work() method likewise, see Concurrency in Swing for more information. 此外,您希望对GUI进行的更改必须在Event Dispatcher Thread上完成,因此您需要同样更改work()方法,有关详细信息,请参阅Swing中的Concurrency So revert your main method to this way, to sort things out. 因此,以这种方式恢复您的主要方法,以解决问题。

public static void main(String args[]) 
{
    final serverg ob=new serverg();  
    java.awt.EventQueue.invokeLater(new Runnable() 
    {
        public void run() 
        {                         
            ob.setVisible(true);                      
            ob. jTextArea1.append("waiting for connection"  );                        
        }       
    });
    ob.work(); 
}

Your if block inside work() method , must look like this, for this to work, you must declare your line variable as Instance Variable : 你的if work()方法中的if块必须如下所示,为此,你必须将你的line变量声明为Instance Variable:

if((line=in.readLine())!=null)
    SwingUtilities.invokeLater(new Runnable()
    {
        public void run()
        {
            jTextArea1.append("Client : " + line );
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM