嗨,我是java的新手,我被困在这里,已经有一段时间了,我可以继续前进;我已经为聊天系统创建了一个GUI界面(虽然非常粗糙,因为我使用过java帮助文件,而且我之前从未做过很多)。但我有另一个代码,它独立,没有任何GUI,所有输出都在命令提示符。现在我想将所有输出附加到我创建的GUI。请帮助看看下面的代码,并建议方法和步骤,以帮助弄清楚...请这不是大学的任务,我是一个毕业生和工作所以我这样做,当我有时间,因为我相信知道java是一个很好的知识。谢谢你的时间。
这是我创建的聊天GUI类
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MainView extends JFrame {
protected JLabel msgLabel, bannerLabel;
protected JButton sendBtn;
protected JTextArea genMsg, frndLst;
protected JTextField msgF;
protected JMenuBar menubar;
protected JMenu loginmenu, aboutmenu;
protected JMenuItem loginitem, disconnectitem, seperatoritem, quititem, aboutitem;
protected Toolkit toolkit;
MultiThreadChatClient chatClient;
public MainView() {
toolkit = Toolkit.getDefaultToolkit();
if(toolkit.getScreenSize().getWidth() > 600)
setSize(600, 575);
else
setSize((int)toolkit.getScreenSize().getWidth(),(int toolkit.getScreenSize().getHeight() - 20);
setResizable(false);
Dimension dimension = getSize();
setLayout(new FlowLayout());
setTitle("FRESHER MARKETING COMPANY");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) { System.exit(0);}});
menubar = new JMenuBar();
loginmenu = new JMenu("Login");
loginitem = new JMenuItem("Login");
disconnectitem = new JMenuItem("Disconnect");
seperatoritem = new JMenuItem("---------------");
quititem = new JMenuItem("Quit");
loginmenu.add(loginitem);
loginmenu.add(disconnectitem);
loginmenu.add(seperatoritem);
loginmenu.add(quititem);
aboutmenu = new JMenu("Help ");
aboutitem = new JMenuItem("About ");
aboutmenu.add(aboutitem);
menubar.add(loginmenu);
menubar.add(aboutmenu);
setJMenuBar(menubar);
Container container = getContentPane();
container.setLayout(new FlowLayout());
// create an ImageIcon
ImageIcon banner =new ImageIcon("images\\defaultbanner.gif");
bannerLabel = new JLabel(banner);
container.add(bannerLabel);
// create General Message Screen
genMsg = new JTextArea(30,45);
genMsg.setEditable(false);
genMsg.setFont(new java.awt.Font("Times New Roman", 0, 12)); // NOI18N
genMsg.setLineWrap(true);
container.add( new JScrollPane( genMsg ));
// create Friend List View
frndLst = new JTextArea(30, 15);
frndLst.setFont(new java.awt.Font("Times New Roman", 0, 12)); // NOI18N
container.add( new JScrollPane( frndLst));
frndLst.setEditable(false);
frndLst.setLineWrap(true);
msgLabel = new JLabel ("Message:");
container.add(msgLabel);
// create Message Field
msgF = new JTextField(38);
msgF.setEnabled( true );
msgF.setText("");
msgF.requestFocus();
msgF.addActionListener(
new ActionListener()
{
// send message to client
public void actionPerformed( ActionEvent event )
{
// sendData( event.getActionCommand() );
}
} // end anonymous inner class
); // end call to addActionListener
container.add(msgF);
// create Send Button
sendBtn = new JButton ("Send");
container.add(sendBtn);
setVisible( true );
}
public static void main(String[] args)
{
MainView application = new MainView();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
这是我要追加的聊天多线程系统。
import java.io.*;
import java.net.*;
public class MultiThreadChatServer{
// Declaration section:
// declare a server socket and a client socket for the server
// declare an input and an output stream
static Socket clientSocket = null;
static ServerSocket serverSocket = null;
// This chat server can accept up to 10 clients' connections
static clientThread t[] = new clientThread[10];
public static void main(String args[]) {
// The default port
int port_number=2222;
if (args.length < 1)
{
System.out.println("Usage: java MultiThreadChatServer \n"+
"Now using port number="+port_number);
} else {
port_number=Integer.valueOf(args[0]).intValue();
}
// Initialization section:
// Try to open a server socket on port port_number (default 2222)
// Note that we can't choose a port less than 1023 if we are not
// privileged users (root)
try {
serverSocket = new ServerSocket(port_number);
}
catch (IOException e)
{System.out.println(e);}
// Create a socket object from the ServerSocket to listen and accept
// connections.
// Open input and output streams for this socket will be created in
// client's thread since every client is served by the server in
// an individual thread
while(true){
try {
clientSocket = serverSocket.accept();
for(int i=0; i<=9; i++){
if(t[i]==null)
{
(t[i] = new clientThread(clientSocket,t)).start();
break;
}
}
}
catch (IOException e) {
System.out.println(e);}
}
}
}
// This client thread opens the input and the output streams for a particular client,
// ask the client's name, informs all the clients currently connected to the
//server about the fact that a new client has joined the chat room,
// and as long as it receive data, echos that data back to all other clients.
// When the client leaves the chat room this thread informs also all the
// clients about that and terminates.
class clientThread extends Thread{
DataInputStream is = null;
PrintStream os = null;
Socket clientSocket = null;
clientThread t[];
public clientThread(Socket clientSocket, clientThread[] t){
this.clientSocket=clientSocket;
this.t=t;
}
public void run()
{
String line;
String name;
try{
is = new DataInputStream(clientSocket.getInputStream());
os = new PrintStream(clientSocket.getOutputStream());
os.println("Enter your name.");
name = is.readLine();
os.println("Hello "+name+" to our chat room.\nTo leave enter /quit in a new line");
for(int i=0; i<=9; i++)
if (t[i]!=null && t[i]!=this)
t[i].os.println("*** A new user "+name+" entered the chat room !!! ***" );
while (true) {
line = is.readLine();
if(line.startsWith("/quit")) break;
for(int i=0; i<=9; i++)
if (t[i]!=null) t[i].os.println("<"+name+"> "+line);
}
for(int i=0; i<=9; i++)
if (t[i]!=null && t[i]!=this)
t[i].os.println("*** The user "+name+" is leaving the chat room !!! ***" );
os.println("*** Bye "+name+" ***");
// Clean up:
// Set to null the current thread variable such that other client could
// be accepted by the server
for(int i=0; i<=9; i++)
if (t[i]==this) t[i]=null;
// close the output stream
// close the input stream
// close the socket
is.close();
os.close();
clientSocket.close();
}
catch(IOException e){};
}
}
任何建议都可以。是实现这一目标的步骤,使事情变得更容易的链接,代码片段...谢谢... 我想要一个关于如何将MultiThreadChatServer类的输出附加到MainView类中的textarea的示例
答案 0 :(得分:0)