Netty和Java GUI

扫码查看
本文介绍了Netty和Java GUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个带有服务器和客户端的简单Netty应用程序,可以通过控制台进行交互.现在,我正在尝试添加GUI,以便客户端可以在不使用控制台的情况下查看/输入他们的消息.

I created a simple Netty Application with a server and client to interact through the console. Now I am trying to add a GUI so the client can view/and enter their messages w/o the console.

我认为在用于创建通道的同一类中创建GUI是不明智的.

I decided it would not be wise to create the GUI in the same class that is used to create the channel.

这是我的主要客户类别的一个示例.

Here is an example of my Main Client Class.

public void run() throws Exception {
    EventLoopGroup group = new NioEventLoopGroup();

    try {
        Bootstrap bootstrap = new Bootstrap()
            .group(group)
            .channel(NioSocketChannel.class)
            .handler(new ChatClientInitializer());

        Channel channel = bootstrap.connect(host, port).sync().channel();

        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

        while (true) {
            channel.writeAndFlush((in.readLine() + "\r\n"));
        }

    } finally {
        group.shutdownGracefully();
    }
}

如何创建GUI,以便当用户在JTextField中输入消息时将其传递给channel.writeAndFlush方法?

How do I create the GUI so that when the user enters a message in the JTextField it will be passed to the channel.writeAndFlush method?

是否在.run方法中创建GUI的实例.

Do I create an instance of the GUI in the .run method.

问题的第二部分,在处理程序类(下面的代码)中,如何将传入消息传递到GUI中的JTextArea?

Also the second part of my question, in my handler class (code below) how do I pass an incoming message to the JTextArea in my GUI?

这是非常基本的Handler类的示例.

Here is a sample of the the very basic Handler Class right now.

protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
    System.out.println(msg);

}

这里是我的GUI类,以供参考.

And for reference here is my GUI class.

public ClientGUI(){
    enterField = new JTextField();
    enterField.setEditable(true);
    enterField.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    sendMessage(event.getActionCommand());
                    enterField.setText("");
                }
            });
    add(enterField,BorderLayout.NORTH);

    displayArea = new JTextArea();
    add (new JScrollPane(displayArea), BorderLayout.CENTER);
    setSize(300,150);
    setVisible(true);
}

public void sendMessage(String message){
//      what to do here?
}

推荐答案

ClientGUI类必须具有对Channel的引用才能进行通信.假设您还引用了ClientGUI实例,并且Channel是在GUI初始化后创建的,则可以在ClientGUI中添加一些设置器:

ClientGUI class must have a reference to the Channel to communicate via. Assuming that you also have a reference to the ClientGUI instance somewhere and the Channel is created after GUI is initialized, you could add some setter to ClientGUI:

public class ClientGUI {
    private volatile Channel channel;

    public void setChannel(Channel channel) {
        this.channel = channel;
    }

    public void sentMessage(String msg) {
        channel.writeAndFlush(msg);
    }
    ...
}

这篇关于Netty和Java GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-16 20:54
查看更多