2011-11-22 95 views
0

过去几天我一直在尝试自己解决这个问题,但我似乎无法得到解决方案......我该如何强制绘制JFrame和其中的所有内容?我有一个聊天程序(客户端/服务器方式),服务器类和客户端是相同的代码方式?但我似乎无法让客户一展身手!强制绘制一个jframe?

import java.awt.event.ActionEvent; 

public class Chat extends JFrame implements Runnable, ActionListener, WindowListener{ 



    private static final long serialVersionUID = 1L; 
    private JPanel contentPane; 
    private JTextField line; 

    // TCP Components 
    private Socket channel = null; 

    private JScrollPane scrollPane; 
    private String toBeSent=""; 
    private final String END_CHAT_SESSION = new Character((char)0).toString(); 
    private PrintWriter out; 
    private boolean channelIsStillOpen = true; 
    private Socket channel; 
    private static JTextArea chatText; 


    public Chat(Socket channel) { 

     this.channel=channel; 

     addWindowListener(this); 

     setTitle("Remote Administrator"); 
     setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); 
     setBounds(100, 100, 450, 300); 
     contentPane = new JPanel(); 
     contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
     setContentPane(contentPane); 

     line = new JTextField(); 
     line.setBounds(17, 207, 289, 40); 
     line.setColumns(10); 

     JButton send = new JButton("Send"); 

     send.addActionListener(this); 

     send.setBounds(312, 214, 105, 25); 
     contentPane.setLayout(null); 

     scrollPane = new JScrollPane(); 
     scrollPane.setBounds(17, 5, 399, 196); 
     scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); 
     scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); 
     contentPane.add(scrollPane); 

     chatText = new JTextArea(); 
     chatText.setLineWrap(true); 
     chatText.setEditable(false); 
     chatText.setEnabled(false); 
     scrollPane.setViewportView(chatText); 
     contentPane.add(line); 
     contentPane.add(send); 

    } 

    @Override 
    public void run() { 

     try { 
      channel=new Socket(address, port); 
      System.out.println("Chat Connection accepted"); 
     } catch (IOException e2) { 
      // TODO Auto-generated catch block 
      e2.printStackTrace(); 
     } 

     BufferedReader in = null; 
     try { 
      in = new BufferedReader(new InputStreamReader(channel.getInputStream())); 
     } catch (IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 

     out = null; 
     try { 
      out = new PrintWriter(channel.getOutputStream(),true); 
     } catch (IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 

     String s=""; 

     do { 
      // Send data 
      if (toBeSent.length()!= 0) { 
       out.println(toBeSent); 
       toBeSent=""; 
      } 

      // Receive data 
      try { 
       if (in.ready()) { 
        s = in.readLine(); 
        if ((s != null) && (s.length() != 0) && !s.equals(END_CHAT_SESSION)) { 
          chatText.append("INCOMING: " + s + "\n"); 
        } 
        if(s.equals(END_CHAT_SESSION)){ 
         chatText.append("Client disconnected."); 
        } 
       } 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } while(!s.equals(END_CHAT_SESSION)); 

     channelIsStillOpen=false; 

     line.setEditable(false); 
     line.setEnabled(false); 

     if(channel!=null){ 
      try { 
       channel.close(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      channel=null; 
     } 

    } 

    @Override 
    public void windowActivated(WindowEvent arg0) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void windowClosed(WindowEvent arg0) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void windowClosing(WindowEvent arg0) { 

     if(channelIsStillOpen){ 
      out.println(END_CHAT_SESSION); 
     } 

     dispose(); 

    } 

    @Override 
    public void windowDeactivated(WindowEvent arg0) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void windowDeiconified(WindowEvent arg0) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void windowIconified(WindowEvent arg0) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void windowOpened(WindowEvent arg0) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void actionPerformed(ActionEvent arg0) { 

     String s = line.getText(); 

     if (!s.equals("")) { 
      chatText.append("OUTGOING: " + s + "\n"); 

      // Send the string 
      toBeSent=s; 
     } 

     line.setText(""); 

    } 
} 

编辑:我甚至编辑了代码,以便所使用的类总是相同的!没有!

+0

我认为这属于StackOverflow –

回答

0

,我认为你是在构造函数的末尾缺少这样的:

this.setVisible(true); 

也,你为什么不设置? setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);,你为什么要保持应用程序打开?