开发者

Problem in JFrames

I am designing a messenger application. I have a server program running properly. On the client side, first i created GUI which consists of text pane and text field. Chat works properly. Then i modified client side to support login facility which provides a login text field, password text field & sign button. So after login is authenticated current JFrame is setVisible(false), and a new JFrame with chat window should be displayed and socket connections are made in parallel. Socket connections to the server are made properly but the required GUI ie a proper JFrame is not made. What might be the problem? Please..please help..

Here is my code..

import java.net.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.text.*;
import javax.swing.event.*;


class Swing16 extends JPanel implements ActionListener
{

    public static JButton enterbutton,signin,chatbutton;
    public static JTextField txt,loginname,password;
    public static JTextPane textpane;
    public static JScrollPane sp;
    public static JLabel retrylabel;


    public static String str;
    public static String newline = "\n";
    public static String ss = "regular";

    public static StyledDocument doc,doc1;
    public static Icon icon1,icon2,icon3,icon4,icon5,icon6;

    public static InetAddress inet,newinet;
    public static Socket s,news;

    public static InputStream in,newin;
    public static OutputStream chatout,newout;

    public static BufferedReader newbr,chat;

    public static PrintWriter chatout1,newout1;
    public static int start;
    public static int end;
    public static int flag = 0;
    public static JFrame frm;


    class connecthread implements Runnable
    {
        Thread t;

        public connecthread()
        {
            t = new Thread(this);
            t.start();
        }

        public void run()
        {
            Swing16.makechatconnection();
        }
    }

    class alwaysrun implements Runnable
    {
        Thread t;

        public alwaysrun()
        {
            t = new Thread(this);
            t.start();
        }

        public void run()
        {
            Swing16.alwaystorun();
        }
    }


    public Swing16()
    {


        if(flag == 0)
        {
            enterbutton = new JButton("Enter");
            add(enterbutton);

            enterbutton.addActionListener(this);

        }


        else if(flag == 1)
        {


            loginname = new JTextField(10);
            add(loginname);

            password = new JTextField(10);
            add(password);


            signin = new JButton("SIGN IN");
            add(signin);

            signin.addActionListener(this);
        }


        else if(flag == 2)
        {

            chatbutton = new JButton("CHAT");
            add(chatbutton);
            chatbutton.addActionListener(this);

        }

        else if(flag == 3)
        {

            retrylabel = new JLabel("AUTHENTICATION FAILED! RETRY");
            add(retrylabel);


            loginname = new JTextField(10);
            add(loginname);

            password = new JTextField(10);
            add(password);


            signin = new JButton("SIGN IN");
            add(signin);

            signin.addActionListener(this);
        }




        else
        {

            GridBagLayout g = new GridBagLayout();
            setLayout(g);

            GridBagConstraints a = new GridBagConstraints();

            a.fill = GridBagConstraints.BOTH;
            a.weightx = 1.0;
            a.weighty = 1.0;
            textpane = new JTextPane();
            add(textpane,a);

            sp = new JScrollPane(textpane);
            a.gridwidth = GridBagConstraints.REMAINDER;
            a.fill = GridBagConstraints.BOTH;
            a.weightx = 1.0;
            a.weighty = 1.0;
            add(sp,a);
            textpane.setEditable(false);

            a.fill = GridBagConstraints.HORIZONTAL;
            a.anchor = GridBagConstraints.FIRST_LINE_START;
            txt = new JTextField(50);
            add(txt,a);

            icon1 = new ImageIcon("smile.png");
            icon2 = new ImageIcon("frown.png");
            icon3 = new ImageIcon("gasp.png");
            icon4 = new ImageIcon("grin.png");
            icon5 = new ImageIcon("tongue.png");
            icon6 = new ImageIcon("wink.png");


            txt.addActionListener(this);

            textpane.setEditorKit(new StyledEditorKit());
            Swing16.initListener();

            }

    }



    public void actionPerformed(ActionEvent ae)
    {


            String t;
            t = ae.getActionCommand();

            if(t.equals("Enter"))
            {

                flag = 1;
                frm.dispose();

                Swing16.makeGUI();

                /*try
                {

                    newinet = InetAddress.getLocalHost();
                    news = new Socket(newinet,1125);

                    newin = news.getInputStream();
                    newout = news.getOutputStream();


                    newout1 = new PrintWriter(new BufferedWriter(new OutputStreamWriter(newout)),true);
                    newbr = new BufferedReader(new InputStreamReader(newin));


                }
                catch(UnknownHostException e)
                {
                    System.out.println("Error.....!!!" + e);
                }
                catch(IOException e)
                {
                    System.out.println("Error....!!!" + e);
                }*/

            }

            else if(t.equals("SIGN IN"))
            {
                String name, passwd,authenticationcode;
                name = loginname.getText();
                passwd = password.getText();


                frm.dispose();

                try
                {
                    /*newout1.println(name);
                    newout1.println(passwd);

                    authenticationcode = newbr.readLine();
                    if(authenticationcode.equals("V"))
                    {
                    */

                        flag = 2;

                        /*newbr.close();
                        newout1.close();

                        newin.close();
                        newout.close();


                        news.close();


                    }

                    else
                        flag = 3; */            
                }
                catch(Exception e)
                {
                    System.out.println(e + "I am exception in sign in block in actionPerformed funation");
                }


                Swing16.makeGUI();
            }



            else if(t.equals("CHAT"))
            {

                frm.dispose();
                flag = 4;

                Swing16.makeGUI();

                new connecthread();

                new alwaysrun();

            }


            else
            {
                try
                {
                doc1 = textpane.getStyledDocument();
                str = txt.getText();
                try 
                {

                    doc1.insertString(doc1.getLength(),"Me:" + str + newline,doc1.getStyle(ss));

                    String text=doc1.getText(start, end-start);

                                    int i= text.indexOf(":)");

                    int j= text.indexOf(":(");

                    int k = text.indexOf(":O");

                    int l = text.indexOf(":D");

                    int m = text.indexOf(":P");

                    int n = text.indexOf(";)");

                    while(i>=0) 
                    {
                                            final SimpleAttributeSet attrs=new SimpleAttributeSet(doc1.getCharacterElement(start+i).getAttributes());

                        if (StyleConstants.getIcon(attrs)==null)
                        {
                                                StyleConstants.setIcon(attrs, icon1);
                                                doc1.remove(start+i, 2);
                                                doc1.insertString(start+i,":)", attrs);
                                            }

                    i=text.indexOf(":)", i+2);
                                    }

                    while(j>=0) 
                    {
                                            final SimpleAttributeSet attrs=new SimpleAttributeSet(doc1.getCharacterElement(start+j).getAttributes());

                        if (StyleConstants.getIcon(attrs)==null)
                        {
                                                StyleConstants.setIcon(attrs, icon2);
                                                doc1.remove(start+j, 2);
                                                doc1.insertString(start+j,":(", attrs);
                                            }

                    j=text.indexOf(":(", j+2);
                                    }

                    while(k>=0) 
                    {
                                            final SimpleAttributeSet attrs=new SimpleAttributeSet(doc1.getCharacterElement(start+k).getAttributes());

                        if (StyleConstants.getIcon(attrs)==null)
                        {
                                                StyleConstants.setIcon(attrs, icon3);
                                                doc1.remove(start+k, 2);
                                                doc1.insertString(start+k,":O", attrs);
                                            }

                    k=text.indexOf(":O", k+2);
                                    }

                    while(l>=0) 
                    {
                                            final SimpleAttributeSet attrs=new SimpleAttributeSet(doc1.getCharacterElement(start+l).getAttributes());

                        if (StyleConstants.getIcon(attrs)==null)
                        {
                                                StyleConstants.setIcon(attrs, icon4);
                                                doc1.remove(start+l, 2);
                                                doc1.insertString(start+l,":D", attrs);
                                            }

                    l=text.indexOf(":D", l+2);
                                    }

                    while(m>=0) 
                    {
                                            final SimpleAttributeSet attrs=new SimpleAttributeSet(doc1.getCharacterElement(start+m).getAttributes());

                        if (StyleConstants.getIcon(attrs)==null)
                        {
                                                StyleConstants.setIcon(attrs, icon5);
                                                doc1.remove(start+m, 2);
                                                doc1.insertString(start+m,":P", attrs);
                                            }

                    m=text.indexOf(":P", m+2);
                                    }   

                    while(n>=0) 
                    {
                                            final SimpleAttributeSet attrs=new SimpleAttributeSet(doc1.getCharacterElement(start+n).getAttributes());

                        if (StyleConstants.getIcon(attrs)==null)
                        {
                                                StyleConstants.setIcon(attrs, icon6);
                                                doc1.remove(start+n, 2);
                                                doc1.insertString(start+n,":P", attrs);
                                            }

                    n=text.indexOf(":P", n+2);
                                    }   

                }
                catch(NullPointerException e)
                {
                    System.out.println("I am d culprit");
                }
                catch (Exception ble)
                {
                        System.out.println(ble);
                }


                txt.setText("");
                chatout1.println(str);
            }
            catch(NullPointerException e)
            {
                    System.out.println("I am the culprit");
            }
            catch(Exception e)
            {
                System.out.println(e);
            }       

        }

  }




    public static void initListener()
    {

        textpane.getDocument().addDocumentListener(new DocumentListener(){

        public void insertUpdate(DocumentEvent event) 
        {

            final DocumentEvent e=event;


            if (e.getDocument() instanceof StyledDocument)
            {
                            try
                {
                                    //doc=(StyledDocument)e.getDocument();
                                    start= Utilities.getRowStart(textpane,Math.max(0,e.getOffset()-1));
                                    end=Utilities.getWordStart(textpane,e.getOffset()+e.getLength());
                                }
                catch (BadLocationException e1) 
                {
                                    e1.printStackTrace();
                                }
                        }


             }

        public void removeUpdate(DocumentEvent e) 
        {

         }

        public void changedUpdate(DocumentEvent event) 
        {

        }
        });
    }


    public static void makeGUI()
    {
        frm = new JFrame("Chat Window");
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frm.add(new Swing16());
        frm.pack();
        frm.setVisible(true);
    }



    public static void makechatconnection()
    {
        try
        {

            inet = InetAddress.getLocalHost();
            s = new Socket(inet,1125);

            in = s.getInputStream();
            chatout = s.getOutputStream();


            chatout1 = new PrintWriter(new BufferedWriter(new OutputStreamWriter(chatout)),true);

            chat = new BufferedReader(new InputStreamReader(in));
            if(chat == null)
            System.out.println("sskksks");

            chatout1.println("i get transmitted there..");
        }
        catch(UnknownHostException e)
        {
            System.out.println("Error.....!!!" + e);
        }
        catch(IOException e)
        {
            System.out.println("Error....!!!" + e);
        }
    }



    public static void alwaystorun()
    {
        try
            {

                while(true)
                {
                    String ss1=null;
                    ss1 = chat.readLine();
                    if(chat == null)
                    System.out.println("hey");
                    System.out.println(ss1);

                    try 
                    {
                            doc1 = textpane.getStyledDocument();

                        doc1.insertString(doc1.getLength(), ss1 + newline,doc1.getStyle(ss));

                        String text=doc1.getText(start, end-start);

                                        int i=text.indexOf(":)");
                        int j=text.indexOf(":(");

                        int k = text.indexOf(":O");

                        int l = text.indexOf(":D");

                        int m = text.indexOf(":P");

                        int n = text.indexOf(";)");

                        while(i>=0) 
                        {
                                                final SimpleAttributeSet attrs=new SimpleAttributeSet(doc1.getCharacterElement(start+i).getAttributes());

                            if (StyleConstants.getIcon(attrs)==null)
                            {
                                                    StyleConstants.setIcon(attrs, icon1);
                                                    doc1.remove(start+i, 2);
                                                    doc1.insertString(start+i,":)", attrs);
                                                }

                            i=text.indexOf(":)", i+2);
                                        }

                        while(j>=0) 
                        {
                                            final SimpleAttributeSet attrs=new SimpleAttributeSet(doc1.getCharacterElement(start+j).getAttributes());

                        if (StyleConstants.getIcon(attrs)==null)
                        {
                                                StyleConstants.setIcon(attrs, icon2);
                                                doc1.remove(start+j, 2);
                                                doc1.insertString(start+j,":(", attrs);
                                            }

                    j=text.indexOf(":(", j+2);
                                    }

                    while(k>=0) 
                    {
                                            final SimpleAttributeSet attrs=new SimpleAttributeSet(doc1.getCharacterElement(start+k).getAttributes());

                        if (StyleConstants.getIcon(attrs)==null)
                        {
                                                StyleConstants.setIcon(attrs, icon3);
                                                doc1.remove(start+k, 2);
                                                doc1.insertString(start+k,":O", attrs);
                                            }

                    k=text.indexOf(":O", k+2);
                                    }

                    while(l>=0) 
                    {
                                            final SimpleAttributeSet attrs=new SimpleAttributeSet(doc1.getCharacterElement(start+l).getAttributes());

                        if (StyleConstants.getIcon(attrs)==null)
                        {
                                                StyleConstants.setIcon(attrs, icon4);
                                                doc1.remove(start+l, 2);
                                                doc1.insertString(start+l,":D", attrs);
                                            }

                    l=text.indexOf(":D", l+2);
                                    }

                    while(m>=0) 
                    {
                                            final SimpleAttributeSet attrs=new SimpleAttributeSet(doc1.getCharacterElement(start+m).getAttributes());

                        if (StyleConstants.getIcon(attrs)==null)
                        {
                                                StyleConstants.setIcon(attrs, icon5);
                                                doc1.remove(start+m, 2);
                                                doc1.insertString(start+m,":P", attrs);
                                            }

                    m=text.indexOf(":P", m+2);
                                    }   

                    while(n>=0) 
                    {
                                            final SimpleAttributeSet attrs=new SimpleAttributeSet(doc1.getCharacterElement(start+n).getAttributes());

                        if (StyleConstants.getIcon(attrs)==null)
                        {
                                                StyleConstants.setIcon(attrs, icon6);
                                                doc1.remove(start+n, 2);
                                                doc1.insertString(start+n,":P", attrs);
                                            }

                    n=text.index开发者_StackOverflow中文版Of(":P", n+2);
                                    }   


                    }
                    catch (BadLocationException ble)
                    {
                            System.err.println("Couldn't insert text into text pane.");
                    }


                }
            }

            catch(Exception e)
            {
                System.out.println(e + "Something bad happened.");
            }


    }
    public static void main(String args[])
    {
        try
        {



            SwingUtilities.invokeLater(new Runnable()
            {
                        public void run()
                {
                    Swing16.makeGUI();

                }
            }
            );




        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }
}


My guess is that you've got a concurrency issue, that you're not doing your socket connection and communication in a thread that is background to the Swing main thread, the EDT, but this is only a guess without more information and code. I suggest that you read up on Concurrency in Swing in the Oracle Java tutorials. A well-made SwingWorker object could work nicely here.

Edit 1:
Some issues I see with your code:

  • Your code should have no static fields but rather non-static instance variables.
  • Your code should have no static methods other than the main method which should create 1 or a few instances and then call the appropriate methods on these instances.
  • You should break this god-class down into several smaller classes so that you can divide and conquer. This makes debugging much easier, and our ability to analyze your code easier too.
  • You call a Swing15 method in the "connectthread inner class. Is this a typographical error?
  • You say that you have a NullPointerException. Which line throws this? By the way, you should not try/catch NullPointerExceptions. You find them and stamp out the problem that causes them. Get rid of that try/catch.
  • Given all those static methods and fields, I'm betting that you're coming to Java from another non-OOP programming language. I strongly urge you to get some books on OOPs programming in Java as this will help you immensely.


I guess that your Socket is listening - and by that is blocking your application, so the new JFrame aren't created.

I would recommend you to start an Akka Actor (Java) and do the Socket connection within the actor. When the actor receives messages, you post them on the Event Dispatch Thread using SwingUtilities.invokeLater().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜