如何使用awt连接java代码和javaswing gui?

aydmsdu9  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(260)


我是java新手,正在开发这个聊天应用程序,我有一个客户端程序,我用JavaSwing编写了gui。。现在我不明白如何使用java awt连接2。。有人能帮我吗??
这是客户端程序

package client;
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ChatClient {
    private String host = "localhost";
    private int port=8000;
    private String uname;
    protected String username ="";
    private int initalport;
    private String clientmessage;
    protected Socket sock;
    public void execute() {

        try {
            Socket socket = new Socket(host, port);
            System.out.println("Connected to the chat server");
            new ReadThread(socket, this).start();
            new WriteThread(socket, this).start();
        } catch (IOException ex) {
            System.out.println("Error getting input stream");
        }
    }
    public static void main(String[] args) {
        ChatClient client = new ChatClient();
        client.execute();
    }
}

class ReadThread extends Thread {  //Reads from the server
    private BufferedReader reader;
    private Socket socket;
    private ChatClient client;
    private String response;

    public ReadThread(Socket socket, ChatClient client) {
        this.socket = socket;
        this.client = client;
        try {
            InputStream input = socket.getInputStream();
            reader = new BufferedReader(new InputStreamReader(input));
        } catch (IOException ex) {
            System.out.println("Error getting input stream");
        }
    }
    public void run() {
    while (true) {
            try {
              response = reader.readLine();
                System.out.print("\n");
                System.out.println(response);
                if (client.username != null) {
                    System.out.print("[" + client.username + "]: ");
                }else{
                    System.out.println("Please enter a valid username");
                }
            } catch (IOException ex) {
                System.out.println("Error reading from server");
                break;
            }
        }
    }
}

class WriteThread extends Thread {  //writes to the server
    private PrintWriter writer;
    private Socket socket;
    private ChatClient client;
  private String userName="";
    private String text="";
    public WriteThread(Socket socket, ChatClient client) {
        this.socket = socket;
        this.client = client;
        try {
            OutputStream output = socket.getOutputStream();
            writer = new PrintWriter(output, true);
        } catch (IOException ex) {
            System.out.println("Error getting output stream");
        }
    }

    public void run() {
        Console console = System.console();
      userName = console.readLine("\nEnter your name: ");
        client.username=userName;
        writer.println(userName);
        while(true) {
            text = console.readLine("[" + userName + "]: ");
            if(text.equals("bye")){
                break;
            }
            else{
            writer.println(text);
          }
        }
        try{
            socket.close();
        }catch(IOException ex){
            System.out.println("Error in closing the socket");
        }

    }
}

这是我用JavaSwing制作的gui

import javax.swing.*;
import java.awt.*;
public class ui {
public static void main(String[] args) {

JFrame f=new JFrame();//creating instance of JFrame

JTextArea area=new JTextArea();  //messages are displayed here
area.setBounds(10,10, 780,400);
area.setEditable(false);
f.add(area);

JTextArea textArea=new JTextArea(); //messages are typed here
textArea.setBounds(10,450,780,110);
f.add(textArea);

JButton b=new JButton("Send");// send button to send the messages
b.setBounds(840,450,95,110);

f.add(b);

textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);

JScrollPane scrolltxt1=new JScrollPane(textArea);
scrolltxt1.setBounds(10,450,780,110);
f.add(scrolltxt1);

JScrollPane scrolltxt2=new JScrollPane(area);
scrolltxt2.setBounds(10,10, 780,400);
f.add(scrolltxt2);

f.setIconImage(icon);
f.setTitle("chat");
f.setResizable(false);
f.setSize(1030,700);
f.setLayout(null);//using no layout managers
f.setVisible(true);//making the frame visible
}
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题