Swing 将输出写入另一个类中的textarea
嗨,我是java新手,我被困在这里,已经有一段时间了,我可以继续前进了;我已经为一个聊天系统创建了一个GUI界面(尽管非常粗糙,因为我使用了java帮助文件和很多我以前从未做过的事情)。但我有另一个独立的代码,根本没有GUI,所有输出都在命令提示符下。现在,我想将所有输出附加到我创建的GUI中。请帮助看一下下面的代码,并建议一些方法和步骤来帮助解决它。。。请注意,这不是大学的作业,我是一名毕业生,正在工作,所以我会在有时间的时候这样做,因为我相信了解java是一门伟大的知识。谢谢你抽出时间 这是我创建的聊天GUI类Swing 将输出写入另一个类中的textarea,swing,user-interface,textarea,chat,Swing,User Interface,Textarea,Chat,嗨,我是java新手,我被困在这里,已经有一段时间了,我可以继续前进了;我已经为一个聊天系统创建了一个GUI界面(尽管非常粗糙,因为我使用了java帮助文件和很多我以前从未做过的事情)。但我有另一个独立的代码,根本没有GUI,所有输出都在命令提示符下。现在,我想将所有输出附加到我创建的GUI中。请帮助看一下下面的代码,并建议一些方法和步骤来帮助解决它。。。请注意,这不是大学的作业,我是一名毕业生,正在工作,所以我会在有时间的时候这样做,因为我相信了解java是一门伟大的知识。谢谢你抽出时间 这是
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){};
}
}
import java.io.*;
导入java.net。*;
公共类多线程服务器{
//申报组:
//声明服务器的服务器套接字和客户端套接字
//声明输入和输出流
静态套接字clientSocket=null;
静态ServerSocket ServerSocket=null;
//此聊天服务器最多可接受10个客户端的连接
静态clientThread t[]=新clientThread[10];
公共静态void main(字符串参数[]){
//默认端口
int端口号=2222;
如果(参数长度<1)
{
System.out.println(“用法:java多线程聊天服务器\n”+
“现在使用端口号=”+端口号);
}否则{
端口号=整数.valueOf(args[0]).intValue();
}
//初始化部分:
//尝试在端口号上打开服务器套接字(默认2222)
//请注意,如果未选择,则无法选择小于1023的端口
//特权用户(root)
试一试{
serverSocket=新的serverSocket(端口号);
}
捕获(IOE异常)
{System.out.println(e);}
//从ServerSocket创建套接字对象以侦听和接受
//联系。
//此套接字的打开输入和输出流将在中创建
//客户端的线程,因为每个客户端都由中的服务器提供服务
//单丝
while(true){
试一试{
clientSocket=serverSocket.accept();
对于(int i=0;i编辑:重新阅读代码,注意到gui代码实际上包含聊天客户端的一个实例。您是否考虑过让聊天客户端事件可观察,然后将gui设置为这些事件的侦听器?哇,谢谢@Nick u刚才给了我一个绝妙的主意,我现在就开始工作,并将回复你很晚才知道进展和问题,但非常感谢,就像你在我脑子里抽动了一样:)