2015-12-22 96 views
0

我目前正在开发该公司的简单的局域网聊天功能。但我坚持在这一点上运行方法永远不会执行在客户端,以前工作正常。它重构了构造函数以便能够在系统托盘图标上双击可配置和可视框架后开始工作。请让我知道我出错的地方。我向您发送之前正在工作的代码以及现在不适用的代码。如果需要,我也可以发送服务器接口源代码和其他代码。Run方法从未得到执行

结构之前(ChatClient.java)

import java.awt.*; 
import javax.swing.*; 
import java.awt.event.*; 
import java.io.*; 
import java.net.*; 
import sun.audio.*; 
import java.awt.BorderLayout; 

public class ChatClient extends Thread implements ActionListener, KeyListener { 

    String lineSeparator = System.getProperty("line.separator"); 
    JFrame frame; 
    JPanel mainPanel; 
    JLabel label_1, label_2, label_3, label_4, label_5, charCnt; 
    JMenuBar menuBar; 
    JMenu tool, help; 
    JMenuItem save, log, exit, about, information; 
    JTextField field_2; 
    JTextArea area_1, area_2; 
    JCheckBox cb1; 
    JButton button_1, button_2, button_3, button_4, button_5, button_6, button_7; 
    JScrollPane scroll, scroll2; 
    Icon send = new ImageIcon("Resources/Image/send.gif"); 
    Icon sv = new ImageIcon("Resources/Image/save.png"); 
    Icon lg = new ImageIcon("Resources/Image/logoff.gif"); 
    Icon ext = new ImageIcon("Resources/Image/exit.png"); 
    Icon About = new ImageIcon("Resources/Image/about.png"); 
    Icon clear = new ImageIcon("Resources/Image/clear.png"); 
    Icon info = new ImageIcon("Resources/Image/help.png"); 
    Font small = new Font("Sans Serif", Font.ITALIC, 9); 
    String strg[] = new String[10]; 
    int arr, ctr2 = 0; 
    int charMax = 150; 
    boolean ignoreInput = false; 

    JList list; 
    DefaultListModel listModel; 

    Color background = new Color(241, 243, 248); 
    Color color = new Color(255, 255, 255); 

    Socket client; 

    String messageToAll, name; 
    BufferedReader fromServer; 
    PrintStream toServer; 

    Login login; 
    time2 obj2 = new time2(this); 
    Thread thr = new Thread(this); 

    @SuppressWarnings({"CallToThreadStartDuringObjectConstruction", "ResultOfObjectAllocationIgnored"}) 
    ChatClient(String name, String IP, Login login) { 

     super(name); 
     try { 
      UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); 
     } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) { 
     } 
     Image bd = Toolkit.getDefaultToolkit().getImage("Resources/Image/client.png"); 
     this.name = name; 
     this.login = login; 
     frame = new JFrame("Client - " + name); 
     frame.setIconImage(bd); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     mainPanel = new JPanel(); 
     menuBar = new JMenuBar(); 
     tool = new JMenu("Tools"); 
     help = new JMenu("Help"); 
     save = new JMenuItem("Save"); 
     log = new JMenuItem("Login as.."); 
     exit = new JMenuItem("Exit"); 
     about = new JMenuItem("About"); 
     information = new JMenuItem("Information"); 
     label_1 = new JLabel("Online Users"); 
     label_3 = new JLabel("Messages: "); 
     label_4 = new JLabel("Public Message"); 
     label_5 = new JLabel(""); 
     charCnt = new JLabel(""); 
     field_2 = new JTextField(29); 
     field_2.setToolTipText("Date & Time"); 
     field_2.setEditable(false); 
     field_2.setFocusable(false); 
     cb1 = new JCheckBox("private"); 
     cb1.setToolTipText("Private message"); 
     area_1 = new JTextArea(20, 10); 
     area_1.setFont(login.notify); 
     area_1.setLineWrap(true); 
     area_1.setEditable(false); 
     area_2 = new JTextArea(1, 10); 
     area_2.setLineWrap(true); 
     area_2.setToolTipText("type your message here.."); 
     scroll = new JScrollPane(area_1); 
     scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
     scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 
     scroll2 = new JScrollPane(area_2); 
     mainPanel.setLayout(new BorderLayout()); 
     listModel = new DefaultListModel(); 
     list = new JList(listModel); 
     list.setToolTipText("Client available"); 

     button_1 = new JButton("SEND"); 
     button_1.setToolTipText("send message"); 

     frame.getContentPane().add(mainPanel); 
     mainPanel.add(menuBar); 
     mainPanel.add(charCnt); 
     mainPanel.add(label_1); 
     mainPanel.add(label_3); 
     mainPanel.add(label_4); 
     mainPanel.add(label_5); 
     mainPanel.add(field_2); 
     mainPanel.add(scroll); 
     mainPanel.add(scroll2); 
     mainPanel.add(list); 
     mainPanel.add(button_1); 
     mainPanel.add(cb1); 
     cb1.setFont(login.notify); 
     button_1.setIcon(send); 

     mainPanel.setLayout(null); 
     menuBar.setBounds(0, 0, 600, 25); 
     //Users 
     label_1.setBounds(455, 5, 150, 20); 
     //Messages Box 
     //label_2.setBounds(245, 317, 150, 20); 
     //Messages 
     //label_3.setBounds(5, 335, 90, 25); 
     //Public Messages 
     label_4.setBounds(140, 5, 300, 20); 
     //time 
     label_5.setBounds(485, 297, 100, 25); 
     //Characters count 
     charCnt.setBounds(80, 360, 150, 15); 
     charCnt.setFont(small); 
     scroll.setBounds(5, 30, 390, 300); 
     scroll2.setBounds(5, 335, 450, 35); 
     list.setBounds(400, 30, 185, 260); 
     field_2.setBounds(479, 300, 110, 25); 
     cb1.setBounds(398, 300, 70, 20); 

     //button 
     button_1.setBounds(490, 336, 95, 27); 

     frame.setSize(600, 400); 
     frame.setResizable(false); 
     frame.setVisible(true); 
     frame.setLocationRelativeTo(null); 

     button_1.addActionListener(this); 
     button_1.addKeyListener(this); 
     button_1.setMnemonic(KeyEvent.VK_ENTER); 
     area_2.addKeyListener(this); 
     field_2.addKeyListener(this); 
     list.addKeyListener(this); 
     cb1.addActionListener(this); 
     cb1.addKeyListener(this); 
     cb1.setMnemonic(KeyEvent.VK_P); 

     try { 
      client = new Socket(IP, 1001); 
      toServer = new PrintStream(client.getOutputStream()); 
      fromServer = new BufferedReader(new InputStreamReader(client.getInputStream())); 
      toServer.println("##" + name); 
      thr.start(); 
     } catch (IOException e) { 
      area_1.setText("no server detected!"); 
      JOptionPane.showMessageDialog(null, "  No server running! " + lineSeparator + "Sorry, You've to log out.... "); 
      this.frame.dispose(); 
      System.exit(0); 
     } 

     area_2.requestFocus(); 
     obj2.start(); 

    } 

    @SuppressWarnings("ResultOfObjectAllocationIgnored") 
    public static void main(String arg[]) { 
     new Login(); 
    } 

    public void run() { 
      while (thr != null) { 

      try {createAndShowUI(); >>this code here executed properly. 

      } catch (IOException | HeadlessException e) { 
          JOptionPane.showMessageDialog(null, "Server has Closed!" + lineSeparator + "Sorry, you've to log out"); 
          System.exit(0); 
         } 
     } 

重组

import java.awt.*; 
import javax.swing.*; 
import java.io.*; 
import java.net.*; 
import java.net.InetAddress; 
import java.net.UnknownHostException; 

final class Login { 

    JFrame frame; 
    JPanel panel; 
    JLabel label_1, label_2, label_3, label_4; 
    JTextField field_1, field_2; 
    JPasswordField pass; 
    JButton button_1, button_2; 
    Font Default = new Font("Comic Sans MS", Font.PLAIN, 12); 
    Font notify = new Font("Comic Sans MS", Font.BOLD, 12); 
    Font small = new Font("Sans Serif", Font.ITALIC, 3); 
    //String IP; 
    Socket client; 
    InetAddress ipaddr; 
    String hostname; 

    PrintStream toServer; 

    ChatClient clientObj; 

    public String gethostString(){ 
     try { 
      ipaddr = InetAddress.getLocalHost(); 
      hostname = ipaddr.getHostName(); 
     } 
     catch (UnknownHostException e) { 
     } 
     return hostname; 
    } 

    @SuppressWarnings("CallToPrintStackTrace") 
    Login() { 
     String IP = "192.168.10.88"; 
     try { 
      ipaddr = InetAddress.getLocalHost(); 
      hostname = ipaddr.getHostName(); 
      clientObj = new ChatClient(gethostString(), IP, this); 
//   clientObj.createAndShowGUI(hostname); 
//   clientObj.createAndShowUI(); 

     } catch (UnknownHostException e) { 
      e.printStackTrace(); 
     } 
     Image bd = Toolkit.getDefaultToolkit().getImage("Image/login.png"); 
     try { 
      UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); 
     } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) { 
     } 
    } 
} 

后(ChatClient.java)

import java.awt.*; 
import javax.swing.*; 
import java.awt.event.*; 
import java.io.*; 
import java.net.*; 
import sun.audio.*; 
import java.awt.BorderLayout; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.swing.text.BadLocationException; 
import javax.swing.text.Style; 
import javax.swing.text.StyleConstants; 
import javax.swing.text.StyledDocument; 

public final class ChatClient extends Thread implements ActionListener, KeyListener { 

    String lineSeparator = System.getProperty("line.separator"); 
    JPanel mainPanel; 
    JLabel label_1, label_2, label_3, label_4, label_5, charCnt; 
    JMenuBar menuBar; 
    JMenu tool, help; 
    JMenuItem save, log, exit, about, information; 
    JTextField field_2; 
    JTextArea area_2; 
    JTextPane chatPane; 
    JCheckBox cb1; 
    JButton button_1, button_2, button_3, button_4, button_5, button_6, button_7; 
    JScrollPane scroll, scroll2, scroll3; 
    Icon send = new ImageIcon("Resources/Image/send.gif"); 
    Icon sv = new ImageIcon("Resources/Image/save.png"); 
    Icon lg = new ImageIcon("Resources/Image/logoff.gif"); 
    Icon ext = new ImageIcon("Resources/Image/exit.png"); 
    Icon About = new ImageIcon("Resources/Image/about.png"); 
    Icon clear = new ImageIcon("Resources/Image/clear.png"); 
    Icon info = new ImageIcon("Resources/Image/help.png"); 
    Font small = new Font("Sans Serif", Font.ITALIC, 9); 
    Font prv = new Font("Helvetica", Font.ITALIC, 14); 
    String strg[] = new String[10]; 
    int arr, ctr2 = 0; 
    int charMax = 150; 
    boolean ignoreInput = false; 

    JList list; 
    DefaultListModel listModel; 

    Color background = new Color(241, 243, 248); 
    Color color = new Color(255, 255, 255); 

    Socket client; 

    String messageToAll, name; 
    BufferedReader fromServer; 
    PrintStream toServer; 

    Login login; 
    time2 obj2 = new time2(this); 
    Thread thr = new Thread(this); 

    /** 
    * 
    * @param name 
    * @param IP 
    * @param login 
    */ 
    @SuppressWarnings({"CallToThreadStartDuringObjectConstruction", "ResultOfObjectAllocationIgnored"}) 

    public ChatClient(String name, String IP, Login login) { 

     super(name); 
     try { 
      createAndShowUI(name); 
      createAndShowGUI(name); 
      client = new Socket(IP, 1001); 
      toServer = new PrintStream(client.getOutputStream()); 
      fromServer = new BufferedReader(new InputStreamReader(client.getInputStream())); 
      toServer.println("##" + name); 
     } catch (IOException e) { 
      chatPane.setText("no server detected!"); 
      JOptionPane.showMessageDialog(null, "  No server running! " + lineSeparator + "Sorry, You've to log out.... "); 
      System.exit(0); 
     } 
    } 

    public ChatClient() { 
//  defaultItem1.addActionListener(exitListener); 
//  defaultItem2.addActionListener(restoreListener); 
//  this.login = login; 
     try { 
      UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); 
     } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) { 
     } 
     mainPanel = new JPanel(); 
     menuBar = new JMenuBar(); 
     tool = new JMenu("Tools"); 
     help = new JMenu("Help"); 
     save = new JMenuItem("Save"); 
     log = new JMenuItem("Login as.."); 
     exit = new JMenuItem("Exit"); 
     about = new JMenuItem("About"); 
     information = new JMenuItem("Information"); 
     label_1 = new JLabel("Online Users"); 
     label_3 = new JLabel("Messages: "); 
     label_4 = new JLabel("Public Message"); 
     label_5 = new JLabel(""); 
     charCnt = new JLabel(""); 
     field_2 = new JTextField(29); 
     field_2.setToolTipText("Date & Time"); 
     field_2.setEditable(false); 
     field_2.setFocusable(false); 
     cb1 = new JCheckBox("private"); 
     cb1.setToolTipText("Private message"); 
     chatPane = new JTextPane(); 
     area_2 = new JTextArea(1, 10); 
     area_2.setLineWrap(true); 
     area_2.setToolTipText("type your message here.."); 
     listModel = new DefaultListModel(); 
     list = new JList(listModel); 
     list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
     list.setToolTipText("Client available"); 
     list.setVisibleRowCount(5); 
     scroll = new JScrollPane(chatPane); 
     scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); 
     scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 
     scroll2 = new JScrollPane(area_2); 
     scroll2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); 
     scroll2.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 
     scroll3 = new JScrollPane(list); 
     scroll3.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); 
     scroll3.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 

     mainPanel.setLayout(new BorderLayout()); 

     button_1 = new JButton("SEND"); 
     button_1.setToolTipText("send message"); 
     mainPanel.add(charCnt); 
     mainPanel.add(label_1); 
     mainPanel.add(label_3); 
     mainPanel.add(label_4); 
     mainPanel.add(label_5); 
     mainPanel.add(field_2); 
     mainPanel.add(scroll); 
     mainPanel.add(scroll2); 
     mainPanel.add(scroll3); 
     mainPanel.add(button_1); 
     mainPanel.add(cb1); 
     cb1.setFont(prv); 
     button_1.setIcon(send); 

     mainPanel.setLayout(null); 
     menuBar.setBounds(0, 0, 600, 25); 
     //Users 
     label_1.setBounds(455, 5, 150, 20); 
     //Messages Box 
     //label_2.setBounds(245, 317, 150, 20); 
     //Messages 
     //label_3.setBounds(5, 335, 90, 25); 
     //Public Messages 
     label_4.setBounds(140, 5, 300, 20); 
     //time 
     label_5.setBounds(485, 297, 100, 25); 
     //Characters count 
     charCnt.setBounds(380, 358, 120, 15); 
     charCnt.setFont(small); 
     scroll.setBounds(5, 25, 390, 300); 
     //chatPane.setFocusable(false); 
     chatPane.setEditable(false); 
     scroll2.setBounds(5, 325, 485, 48); 
     scroll3.setBounds(400, 30, 185, 260); 

     field_2.setBounds(477, 300, 112, 25); 
     cb1.setBounds(398, 300, 70, 20); 

     button_1.setBounds(490, 326, 100, 30); 

     button_1.addActionListener(this); 
     button_1.addKeyListener(this); 
     button_1.setMnemonic(KeyEvent.VK_ENTER); 
     area_2.addKeyListener(this); 
     field_2.addKeyListener(this); 
     list.addKeyListener(this); 
     cb1.addActionListener(this); 
     cb1.addKeyListener(this); 
     cb1.setMnemonic(KeyEvent.VK_P); 

     try { 
      thr.start(); 
     } catch (Exception e) { 
     } 
     area_2.requestFocus(true); 
     obj2.start(); 
    } 
    @SuppressWarnings("ResultOfObjectAllocationIgnored") 
    public static void main(String arg[]) { 
     new Login(); 
    } 
    public void run() { 
     while (thr != null) { 
      try { 
       createAndShowUI(); >> this code never run to 

Login.java

这是我的方法createAndShowUI。

public void createAndShowUI(String name) { 
     this.name = name; 
     JFrame frame; 
     frame = new JFrame("Client - " + name); 
     Image bd = Toolkit.getDefaultToolkit().getImage("Resources/Image/client.png"); 
     frame.setIconImage(bd); 
     frame.getContentPane().add(new ChatClient().mainPanel); >> I call the constructor here to draw the frame. 
     frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); 
     frame.setSize(600, 400); 
     frame.setResizable(false); 
//  frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public JComponent getComponent() { 
     return mainPanel; 
    } 

任何帮助将非常感激。干杯!

+0

在哪里登录类,和你在做什么在登录()构造函数。因为,你必须在Login()内部启动线程来执行运行。 –

+0

好的,我添加了login.java。我在它的构造函数中调用ChatClient对象。我会尝试上传服务器和其他你需要的类来让它运行。无论如何感谢您的回复。干杯! –

+0

我试图与你们共享服务器源,但第一个帖子不允许我发布超过30000个字符。我需要另外5000个字符的服务器和其他类。我们有其他选择或分享方式吗?提前致谢。 –

回答

0

您创建了第二个无参数的构造函数,没有人调用该第二构造函数。但是,您将启动线程的代码移动到第二个构造函数中,所以现在线程不再被启动()。

也许你想调用这个方法public void init() {而不是public ChatClient() {,然后你有3个参数的第一个构造函数的末尾调用init();

+0

亲爱Bolwidt, 非常感谢您为您的快速响应。我试图把run方法放在3个参数构造函数中,甚至是主要方法,但没有运气。我尝试了你的建议,它实际上解决了这个问题!现在执行Run方法。但是其他的问题,现在我不能让我的框架运行良好,我实际创建方法来绘制帧,因为我需要为我以下提到召回它在系统托盘图标,双击。我想分享给你的片段。希望我仍然可以在第一篇文章中发布该片段。请帮帮我。再次感谢你!你是我的天使。 –

+0

@RaymondNatio如果这解决了问题,那么你应该接受答案。如果你发现新问题,你应该先尝试自己解决,否则发布一个新问题。 –

+0

好的@MarkRotteveel感谢您的建议。我会先试着解决它,如果不幸的话,我会在明天下午之前发布一个新问题。再次感谢。干杯。 –