我目前正在尝试为该公司开发一个简单的局域网聊天功能。但我仍然停留在这一点上,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);
}
}
重组后(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
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) {
}
}
}
这是我的方法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 :(得分:0)
您创建了第二个无参构造函数,并且没有人正在调用第二个构造函数。但是,您将启动线程的代码移动到第二个构造函数中,所以现在线程不再被启动()。
也许你想调用这个方法public void init() {
而不是public ChatClient() {
,然后在第一个有3个参数的构造函数的末尾调用init();
。