繁体   English   中英

如何在聊天客户端GUI中实现发送按钮

[英]How to implement a send button in a chat client GUI

我已经实现了一个聊天客户端。 到目前为止,它仅在cmd中完全可用,因为我不知道如何在聊天客户端GUI中实现发送按钮。

这是我的聊天客户端类:

class ChatClientClass {

    private String host;
    private int port;
    private Socket socket;
    private PrintWriter out;
    private BufferedReader in;
    private BufferedReader stdIn;

    ChatWindow chatWindow = new ChatWindow();

    public ChatClientClass(String host, int port) {
        try {   
            this.socket = new Socket(host, port);

            this.out = 
                new PrintWriter(socket.getOutputStream(), true);
            this.in = 
                new BufferedReader(
                    new InputStreamReader(socket.getInputStream()));
            this.stdIn =
                new BufferedReader(
                    new InputStreamReader(System.in));

            String userInput;
            while ((userInput = stdIn.readLine()) != null) {
                out.println(userInput);
                chatWindow.addText("You: " + userInput);
                String serverMessage = in.readLine();
                chatWindow.addText(serverMessage);
            }
        } catch (UnknownHostException e) {
            System.err.println("Couldn't not find host " + host);
            e.printStackTrace();
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to " + host);
            e.printStackTrace();
            System.exit(1);
        }
    }   
}

到目前为止,我只能在命令提示符下编写内容。 但是,我编写的内容以及服务器的答案将显示在GUI中。 addText(String text)方法将输入和输出添加到我的GUI中。

但是我不知道如何实现我的发送按钮。 一种简单的方法是,当我调用ActionListener类的构造函数时,只要发送对PrintWriter的引用和对GUI的引用,然后执行以下操作: thePrintWriter.println( [get the value of the text area that belongs to the send button] ) public void actionPerformed(ActionEvent e) -方法中thePrintWriter.println( [get the value of the text area that belongs to the send button] ) 但是由于我不能/不应该从我的聊天客户端类中调用ActionListener的构造函数,因此发送了这些引用。 那不可能,对吗?

我还考虑过将PrintWriter变量设为静态,还使包含要发送变量的消息的JTextArea变为静态,然后创建静态方法来访问这两个变量。 但是,当我这样做时,感觉就像我在做某件事。 而且我也无法正常工作。

那么应该如何在聊天客户端中实现发送按钮呢?

提前致谢!

如果您不熟悉GUI,请使用Java / Eclipse构建。 我建议您使用GUI生成器: http : //www.eclipse.org/windowbuilder/

它确实非常易于使用,并且您可以为应用程序制作简单的GUI。

对于您的问题,您需要在框架上添加一个按钮,并且需要添加一个动作侦听器,而如果您触发它,则可以执行所需的操作。

    import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;

public class ButtonAction {

    private static void createAndShowGUI()  {

        JFrame frame1 = new JFrame("JAVA");
        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JButton button = new JButton(" >> JavaProgrammingForums.com <<");
        //Add action listener to button



        button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
                //Execute when button is pressed
                 //Here call your sender fucntion 
           out.println(userInput);
           chatWindow.addText("You: " + userInput);
           String serverMessage = your_jtext.getText();
           chatWindow.addText(serverMessage);
                System.out.println("You clicked the button");
            }
        });      

        frame1.getContentPane().add(button);
        frame1.pack();
        frame1.setVisible(true);
    }


    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM